diff --git a/include/cont/edeque.h b/include/cont/edeque.h index b27b23a..63197ef 100644 --- a/include/cont/edeque.h +++ b/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 > diff --git a/include/cont/equeue.h b/include/cont/equeue.h new file mode 100644 index 0000000..2ccedd0 --- /dev/null +++ b/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 + * + *
License
+ * 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. + *
+ */ + +#ifndef TBX_CONT_EQUQUE_H_ +#define TBX_CONT_EQUQUE_H_ + +#include +#include + +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 > +class equeue : public edeque { + public: + // meta-identity types + using equeue_t = equeue; + using base_type = edeque; + + // 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 + constexpr equeue(It&& ...it) noexcept : base_type(std::forward(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 > +Data_t& operator<< (Data_t& it, equeue& q) { + it = q.pop(); + return it; +} + +} + +#endif /* TBX_CONT_EQUQUE_H_ */ diff --git a/include/cont/queue.h b/include/cont/queue.h index c07ddfe..7c6e4bd 100644 --- a/include/cont/queue.h +++ b/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 class queue : public deque { @@ -89,34 +89,51 @@ class queue : public deque { 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 +constexpr Data_t& operator<< (Data_t& it, queue& q) { + it = q.pop(); + return it; } +} // namespace tbx + #endif /* TBX_CONT_QUQUE_H_ */ diff --git a/test/tests/edeque.cpp b/test/tests/edeque.cpp index aea7246..3a1c1a7 100644 --- a/test/tests/edeque.cpp +++ b/test/tests/edeque.cpp @@ -60,10 +60,16 @@ namespace Tdeque { Edeque e3(Edeque::size_match::EQ, 7, vfun); edeque e4(edeque::size_match::EQ, 2, vfoo{}); + edeque q1; + edeque q2(edeque::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) { diff --git a/test/tests/equeue.cpp b/test/tests/equeue.cpp new file mode 100644 index 0000000..9be36c9 --- /dev/null +++ b/test/tests/equeue.cpp @@ -0,0 +1,181 @@ +/*! + * \file equeue.cpp + * \brief + * Unit tests for equeue + * + * \copyright Copyright (C) 2020 Christos Choutouridis + * + *
License
+ * 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. + *
+ * + */ +#include +#include + +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; + 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 e4(equeue::size_match::EQ, 2, vfoo{}); + + equeue q1; + equeue q2(equeue::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; + + 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; + 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 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); + } + +} diff --git a/test/tests/queue.cpp b/test/tests/queue.cpp index bb441ed..7c161f7 100644 --- a/test/tests/queue.cpp +++ b/test/tests/queue.cpp @@ -39,12 +39,17 @@ namespace Tqueue { TEST(Tqueue, contruct) { queue q1; queue q2{1, 2, 3, 4, 5, 6, 7, 8}; + queue 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 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 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 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); } }