Просмотр исходного кода

DEV: a queue with event based callables added

master
Christos Choutouridis 3 лет назад
Родитель
Сommit
28bf870959
6 измененных файлов: 390 добавлений и 49 удалений
  1. +1
    -1
      include/cont/edeque.h
  2. +144
    -0
      include/cont/equeue.h
  3. +27
    -10
      include/cont/queue.h
  4. +6
    -0
      test/tests/edeque.cpp
  5. +181
    -0
      test/tests/equeue.cpp
  6. +31
    -38
      test/tests/queue.cpp

+ 1
- 1
include/cont/edeque.h Просмотреть файл

@@ -56,7 +56,7 @@ namespace tbx {
* \c Fn
*
* \tparam Data_t The char-like queued item type. Usually \c char
* \tparam N The size of deque
* \tparam N The size of edeque
* \tparam Fn The type of Callable
*/
template <typename Data_t, size_t N, typename Fn = std::function<void()>>


+ 144
- 0
include/cont/equeue.h Просмотреть файл

@@ -0,0 +1,144 @@
/*!
* \file cont/equeue.h
* \brief
* A queue with event based callables based on edeque.
*
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
*
* <dl class=\"section copyright\"><dt>License</dt><dd>
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* </dd></dl>
*/
#ifndef TBX_CONT_EQUQUE_H_
#define TBX_CONT_EQUQUE_H_
#include <core/core.h>
#include <cont/edeque.h>
namespace tbx {
/*!
* \class equeue
* \brief
* A statically allocated queue based on edeque with size and data matching
* event based callables.
*
* We use the \ref edeque::push_back() and \ref edeque::pop_front() pair from edeque's
* functionality, so at the \c push the increment performed after the insertion.
* Similarly at the \c pop the decrement performed before the exctraction. This way also
* the \ref edeque::front() and \ref edeque::back() stay the same ;)
*
* We also provide stream operators.
*
* \tparam Data_t The char-like queued item type. Usually \c char
* \tparam N The size of edeque
* \tparam Fn The type of Callable
*/
template <typename Data_t, size_t N, typename Fn = std::function<void()>>
class equeue : public edeque<Data_t, N, Fn> {
public:
// meta-identity types
using equeue_t = equeue<Data_t, N>;
using base_type = edeque<Data_t, N, Fn>;
// STL
using value_type = typename base_type::value_type;
using reference = typename base_type::reference;
using const_reference = typename base_type::const_reference;
using pointer = typename base_type::pointer;
using const_pointer = typename base_type::const_pointer;
using iterator = typename base_type::iterator;
using const_iterator = typename base_type::const_iterator;
using reverse_iterator = typename base_type::reverse_iterator;
using const_reverse_iterator= typename base_type::const_reverse_iterator;
//! \name Constructor / Destructor
//! @{
public:
//! Default constructor
constexpr equeue () noexcept : base_type() { }
//! fill contructor
constexpr equeue(const Data_t& value) noexcept : base_type(value) { }
//! Initializer list contructor
template <typename ...It>
constexpr equeue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
//! @}
//! \name Member access
//! @{
public:
//! \brief Push an item in the back of the queue
//! \param it The item to push
void push (const Data_t& it) {
base_type::push_back(it);
}
//! \brief Extract an item from the front of the queue and remove it from the queue
//! \param it The item to push
Data_t pop () {
return base_type::pop_front();
}
//! \brief Push an item in the back of the queue
//! \param it The item to push
equeue_t& operator<< (const Data_t& it) {
push(it);
return *this;
}
//! \brief Push an item in the back of the queue
//! \param it The item to push
equeue_t& operator>> (Data_t& it) {
it = pop();
return *this;
}
//! @}
};
/*!
* \brief
* Pop an item from the front of the queue.
*
* This definition enables the "data << equeue" syntax for pop operation
*
* \tparam Data_t The char-like queued item type. Usually \c char
* \tparam N The size of queue
* \tparam Fn The type of Callable
*
* \param it The item to write to
* \param q The queue to read from
* \return Reference to the returned item
*/
template <typename Data_t, size_t N, typename Fn = std::function<void()>>
Data_t& operator<< (Data_t& it, equeue<Data_t, N, Fn>& q) {
it = q.pop();
return it;
}
}
#endif /* TBX_CONT_EQUQUE_H_ */

+ 27
- 10
include/cont/queue.h Просмотреть файл

@@ -37,7 +37,7 @@
namespace tbx {
/*!
* \class deque
* \class queue
* \brief
* A statically allocated queue based on deque.
*
@@ -49,7 +49,7 @@ namespace tbx {
* We also provide stream operators.
*
* \tparam Data_t The char-like queued item type. Usually \c char
* \tparam N The size of deque
* \tparam N The size of queue
*/
template <typename Data_t, size_t N>
class queue : public deque<Data_t, N> {
@@ -89,34 +89,51 @@ class queue : public deque<Data_t, N> {
public:
//! \brief Push an item in the back of the queue
//! \param it The item to push
void push (const Data_t& it) {
constexpr void push (const Data_t& it) {
base_type::push_back(it);
}
//! \brief Extract an item from the front of the queue and remove it from the queue
//! \param it The item to push
Data_t pop () {
constexpr Data_t pop () {
return base_type::pop_front();
}
//! \brief Push an item in the back of the queue
//! \param it The item to push
queue_t& operator<< (const Data_t& it) {
constexpr queue_t& operator<< (const Data_t& it) {
push(it);
return *this;
}
//! \brief Push an item in the back of the queue
//! \param it The item to push
queue_t& operator>> (Data_t& it) {
//! \brief Pop an item from the front of the queue
//! \param it The item to write to
constexpr queue_t& operator>> (Data_t& it) {
it = pop();
return *this;
}
//! @}
};
/*!
* \brief
* Pop an item from the front of the queue.
*
* This definition enables the "data << queue" syntax for pop operation
*
* \tparam Data_t The char-like queued item type. Usually \c char
* \tparam N The size of queue
*
* \param it The item to write to
* \param q The queue to read from
* \return Reference to the returned item
*/
template <typename Data_t, size_t N>
constexpr Data_t& operator<< (Data_t& it, queue<Data_t, N>& q) {
it = q.pop();
return it;
}
} // namespace tbx
#endif /* TBX_CONT_QUQUE_H_ */

+ 6
- 0
test/tests/edeque.cpp Просмотреть файл

@@ -60,10 +60,16 @@ namespace Tdeque {
Edeque e3(Edeque::size_match::EQ, 7, vfun);
edeque<T, 8> e4(edeque<T, 8>::size_match::EQ, 2, vfoo{});
edeque<int, 8> q1;
edeque<int, 8> q2(edeque<int, 8>::size_match::DISABLED, 0, nullptr);
EXPECT_EQ (8UL, e1.capacity());
EXPECT_EQ (8UL, e2.capacity());
EXPECT_EQ (8UL, e3.capacity());
EXPECT_EQ (8UL, e4.capacity());
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (8UL, q2.capacity());
}
TEST (Tedeque, base_class) {


+ 181
- 0
test/tests/equeue.cpp Просмотреть файл

@@ -0,0 +1,181 @@
/*!
* \file equeue.cpp
* \brief
* Unit tests for equeue
*
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
*
* <dl class=\"section copyright\"><dt>License</dt><dd>
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* </dd></dl>
*
*/
#include <cont/equeue.h>
#include <gtest/gtest.h>
namespace Tequeue {
using namespace tbx;
int global_flag =0;
// Callable mocks
void vfun(void) { ++global_flag; }
struct vfoo {
void operator() (void) { ++global_flag; }
};
// Test construction
TEST(Tequeue, contruct) {
using Equeue = equeue<int, 8>;
struct T { int a,b; };
int local{};
Equeue e1(Equeue::size_match::GE, 3, [](){
++global_flag;
});
Equeue e2(Equeue::size_match::GE, 3, [&](){
++local;
});
Equeue e3(Equeue::size_match::EQ, 7, vfun);
equeue<T, 8> e4(equeue<T, 8>::size_match::EQ, 2, vfoo{});
equeue<int, 8> q1;
equeue<int, 8> q2(equeue<int, 8>::size_match::DISABLED, 0, nullptr);
EXPECT_EQ (8UL, e1.capacity());
EXPECT_EQ (8UL, e2.capacity());
EXPECT_EQ (8UL, e3.capacity());
EXPECT_EQ (8UL, e4.capacity());
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (8UL, q2.capacity());
}
// simple push-pop functionality
TEST(Tequeue, base_class) {
using Equeue = equeue<int, 8>;
Equeue e1(Equeue::size_match::GE, 3, [](){
++global_flag;
});
// Access of base class functionality
EXPECT_EQ (8UL, e1.capacity());
EXPECT_EQ (0UL, e1.size());
EXPECT_EQ (true, e1.empty());
EXPECT_EQ (false, e1.full());
e1.push(42);
EXPECT_EQ (42, e1.front());
EXPECT_EQ (42, e1.back());
EXPECT_EQ (42, e1.pop());
e1.push(1);
e1.push(2);
e1.push(3);
int check_it=1;
for (auto it = e1.begin() ; it != e1.end() ; ++it)
EXPECT_EQ(*it, check_it++);
EXPECT_EQ(4, check_it); // run through all
}
// trigger functionality
TEST (Tequeue, set_clear_check_trigger) {
using Equeue = equeue<int, 8>;
bool flag{};
Equeue e1(Equeue::size_match::GE, 1, [&](){ flag = true; });
flag = false;
e1.clear_trigger();
EXPECT_EQ (false, flag);
e1.push_back(1); // 1, no-trigger cleared
EXPECT_EQ (false, flag);
flag = false;
e1.clear();
e1.clear_trigger();
EXPECT_EQ (false, flag); // no spurious triggers
e1.push_back(1); // 1
e1.push_back(2); // 2
e1.set_trigger(Equeue::size_match::GE, 1, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
e1.check_trigger(); // manual trigger
EXPECT_EQ (true, flag);
flag = false;
e1.check_trigger(); // manual trigger attempt
EXPECT_EQ (false, flag); // [SIZE triggers are auto clear]
Equeue e2(Equeue::data_match::MATCH, 42, [&](){ flag = true; });
flag = false;
e2.clear_trigger();
EXPECT_EQ (false, flag);
e2.push_back(42); // push 42, no-trigger cleared
EXPECT_EQ (false, flag);
e2.set_trigger(Equeue::data_match::MATCH, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
e2.pop_back(); // pop 42, trigger
EXPECT_EQ (true, flag);
flag = false;
e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
EXPECT_EQ (true, flag);
}
// stream push-pop
TEST(Tequeue, stream_push_pop) {
equeue<int, 8> q1;
q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (8UL, q1.size());
EXPECT_EQ (false, q1.empty());
EXPECT_EQ (true, q1.full());
q1 << 9; // try to insert in full queue
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (8UL, q1.size());
EXPECT_EQ (false, q1.empty());
EXPECT_EQ (true, q1.full());
int check_it=1;
for (auto it = q1.begin() ; it != q1.end() ; ++it)
EXPECT_EQ(*it, check_it++);
EXPECT_EQ(9, check_it); // run through all
for (int i =1 ; i <= 8 ; ++i) {
check_it << q1;
EXPECT_EQ(i, check_it);
}
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (0UL, q1.size());
EXPECT_EQ (true, q1.empty());
EXPECT_EQ (false, q1.full());
q1 >> check_it;
EXPECT_EQ (int{}, check_it);
}
}

+ 31
- 38
test/tests/queue.cpp Просмотреть файл

@@ -39,12 +39,17 @@ namespace Tqueue {
TEST(Tqueue, contruct) {
queue<int, 8> q1;
queue<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
queue<int, 8> q3{1, 2, 3, 4, 5};
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (0UL, q1.size());
EXPECT_EQ (8UL, q2.capacity());
EXPECT_EQ (8UL, q2.size());
EXPECT_EQ (8UL, q3.capacity());
EXPECT_EQ (5UL, q3.size());
}
// simple push-pop functionality
// base class functionality check
TEST(Tqueue, base_class) {
queue<int, 8> q1;
@@ -55,34 +60,10 @@ namespace Tqueue {
EXPECT_EQ (true, q1.empty());
EXPECT_EQ (false, q1.full());
q1.push_back(7);
EXPECT_EQ (7, q1.front());
EXPECT_EQ (7, q1.back());
EXPECT_EQ (7, q1.pop_front());
q1.push_front(42);
q1.push(42);
EXPECT_EQ (42, q1.front());
EXPECT_EQ (42, q1.back());
EXPECT_EQ (42, q1.pop_back());
q1.push_back(1);
q1.push_back(2);
q1.push_back(3);
int check_it=1;
for (auto it = q1.begin() ; it != q1.end() ; ++it)
EXPECT_EQ(*it, check_it++);
EXPECT_EQ(4, check_it); // run through all
}
// push-pop
TEST(Tqueue, front_back) {
queue<int, 8> q1;
q1.push(7);
EXPECT_EQ (7, q1.front());
EXPECT_EQ (7, q1.back());
EXPECT_EQ (7, q1.pop());
EXPECT_EQ (42, q1.pop());
q1.push(1);
q1.push(2);
@@ -92,28 +73,40 @@ namespace Tqueue {
for (auto it = q1.begin() ; it != q1.end() ; ++it)
EXPECT_EQ(*it, check_it++);
EXPECT_EQ(4, check_it); // run through all
}
// stream operations
TEST(Tqueue, capacity) {
// stream push-pop
TEST(Tqueue, stream_push_pop) {
queue<int, 8> q1;
// stream 5 items
q1 << 1 << 2 << 3 << 4 << 5;
q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (8UL, q1.size());
EXPECT_EQ (false, q1.empty());
EXPECT_EQ (true, q1.full());
q1 << 9; // try to insert in full queue
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (8UL, q1.size());
EXPECT_EQ (false, q1.empty());
EXPECT_EQ (true, q1.full());
int check_it=1;
for (auto it = q1.begin() ; it != q1.end() ; ++it)
EXPECT_EQ(*it, check_it++);
EXPECT_EQ (6, check_it); // run through all
EXPECT_EQ(9, check_it); // run through all
// get all back
int it;
for (int check_it=1 ; check_it<=5 ; ++check_it) {
q1 >> it;
EXPECT_EQ (check_it, it);
for (int i =1 ; i <= 8 ; ++i) {
check_it << q1;
EXPECT_EQ(i, check_it);
}
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (0UL, q1.size());
EXPECT_EQ (true, q1.empty());
EXPECT_EQ (false, q1.full());
q1 >> check_it;
EXPECT_EQ (int{}, check_it);
}
}

Загрузка…
Отмена
Сохранить