Browse Source

DEV: add deque variants to utl

master
parent
commit
813bc0977a
6 changed files with 1649 additions and 0 deletions
  1. +299
    -0
      include/utl/container/edeque.h
  2. +146
    -0
      include/utl/container/equeue.h
  3. +144
    -0
      include/utl/container/queue.h
  4. +555
    -0
      test/tests/edeque.cpp
  5. +317
    -0
      test/tests/equeue.cpp
  6. +188
    -0
      test/tests/queue.cpp

+ 299
- 0
include/utl/container/edeque.h View File

@@ -0,0 +1,299 @@
/*!
* \file container/edeque.h
* \brief
* A deque with event based callables
*
* \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 utl_container_edeque_h__
#define utl_container_edeque_h__
#include <utl/core/impl.h>
#include <utl/container/deque.h>
#include <functional>
#include <utility>
#include <type_traits>
namespace utl {
/*!
* \class edeque
* \brief
* A statically allocated deque with size and data matching event based callables.
*
* The edeque inherits deque and provide the callable functionality as a wrapper.
*
* There are two types of events.
* - Size based events, which are cleared as soon as they served. These events are checked
* every time the deque change its size. If the criteria match we call the callable of type
* \c Fn
* - Data based events, which are permanently. These events are checked every time an item is
* pushed or popped from the deque. If the criteria match we call call the callable of type
* \c Fn
*
* \tparam Data_t The char-like queued item type. Usually \c char
* \tparam N The size of edeque
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
* \tparam Fn The type of Callable
* \note
* SemiAtomic means it is safe to access different ends from different threads. For example one thread can
* push only from front and another can pop from back to implement a queue.
*/
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
class edeque : public deque<Data_t, N, SemiAtomic> {
public:
// meta-identity types
using type = edeque<Data_t, N, SemiAtomic, Fn>;
using base_type = deque<Data_t, N, SemiAtomic>;
using callable_t = Fn;
using range_t = typename base_type::range_t;
// 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 Public types
//! @{
public:
//! \enum match_mode
//! The mode of match operation
enum class match_mode { SIZE, DATA };
//! \enum size_match
//! The type of matching for size based match
enum class size_match { DISABLED =0, EQ, NE, LT, LE, GT, GE };
//! \enum data_match
//! The type of matching for data based match
enum class data_match { DISABLED =0, MATCH_PUSH, MATCH_POP, MISMATCH_PUSH, MISMATCH_POP};
// TODO: trigger mode for one-shot or repeated functionality
// enum class trigger_mode { ONE_SHOT, REPEATED };
//! \struct size_trigger
//! Size trigger data type
struct size_trigger {
size_match type;
size_t size;
};
//! \struct data_trigger
//! Data trigger data type
struct data_trigger {
data_match type;
Data_t value;
};
//! \union trigger
//! \brief
//! A union for the common types.
//! There is only one mode. Either "size" with \ref size_match type and a size to match,
//! or "data" with \ref data_match type and a value to match
union trigger {
size_trigger tsize;
data_trigger tdata;
};
//! @}
//! \name Constructor / Destructor
//! @{
public:
//! Default constructor
constexpr edeque () noexcept :
base_type() { }
//! Size trigger constructor
constexpr edeque (size_match match, size_t size, callable_t&& fn) noexcept :
base_type(),
mode_{match_mode::SIZE},
callback_{std::forward<callable_t>(fn)} {
trigger_.tsize.type = match;
trigger_.tsize.size = size;
}
//! Data trigger constructor
constexpr edeque (data_match match, Data_t value, callable_t&& fn) noexcept :
base_type(),
mode_{match_mode::DATA},
callback_{std::forward<callable_t>(fn)} {
trigger_.tdata.type = match;
trigger_.tdata.value = value;
}
//! @}
//! \name Public interface
//! @{
//! \brief
//! Manually checks the size trigger and calls it we have match.
//! \return True if the callable has called.
bool check_trigger () noexcept {
return check_trigger_size_();
}
//! \brief
//! Manually set (or alters) the \c size trigger. This function does not fire the
//! \ref check_trigger()
//! \param match The match type
//! \param size The size for with we check against
//! \param fn The callable to call on match
void set_trigger (size_match match, size_t size, callable_t&& fn) noexcept {
mode_ = match_mode::SIZE;
trigger_.tsize.type = match;
trigger_.tsize.size = size;
callback_ = std::forward<callable_t>(fn);
}
//! \brief
//! Manually set (or alters) the \c data trigger. This function does not fire the
//! \ref check_trigger()
//! \param match The match type
//! \param value The value for with we check against
//! \param fn The callable to call on match
void set_trigger (data_match match, Data_t value, callable_t&& fn) noexcept {
mode_ = match_mode::DATA;
trigger_.tdata.type = match;
trigger_.tdata.value= value;
callback_ = std::forward<callable_t>(fn);
}
//! \brief Manually clears the trigger
void clear_trigger () noexcept {
mode_ = match_mode{};
trigger_ = trigger{};
callback_ = callable_t{};
}
//! @}
//! \name Base class uses and overwrites
//! @{
void push_front (const Data_t& it) noexcept {
base_type::push_front(it);
check_trigger_push_async_(it);
}
void push_back (const Data_t& it) noexcept {
base_type::push_back(it);
check_trigger_push_async_(it);
}
Data_t pop_front () noexcept {
Data_t t = base_type::pop_front();
check_trigger_pop_async_(t);
return t;
}
Data_t pop_back () noexcept {
Data_t t = base_type::pop_back();
check_trigger_pop_async_(t);
return t;
}
//! @}
//! \name Private functionality
//! @{
private:
//! \brief
//! Manually checks the size trigger and calls it we have match.
//! \return True if the callable has called.
bool check_trigger_size_ () {
bool match;
switch (trigger_.tsize.type) {
default:
case size_match::DISABLED: match = false; break;
case size_match::EQ: match = (base_type::size() == trigger_.tsize.size); break;
case size_match::NE: match = (base_type::size() != trigger_.tsize.size); break;
case size_match::LT: match = (base_type::size() < trigger_.tsize.size); break;
case size_match::LE: match = (base_type::size() <= trigger_.tsize.size); break;
case size_match::GT: match = (base_type::size() > trigger_.tsize.size); break;
case size_match::GE: match = (base_type::size() >= trigger_.tsize.size); break;
}
if (match) {
callback_();
clear_trigger();
}
return match;
}
//! \brief
//! Manually checks the data trigger on push and calls it we have match.
//! \param it The item to check against
//! \return True if the callable has called.
bool check_trigger_push_value_ (const Data_t& it) {
bool match;
switch (trigger_.tdata.type) {
default:
case data_match::DISABLED: match = false; break;
case data_match::MATCH_PUSH: match = (it == trigger_.tdata.value); break;
case data_match::MISMATCH_PUSH: match = (it != trigger_.tdata.value); break;
}
if (match)
callback_();
return match;
}
//! \brief
//! Manually checks the data trigger on pop and calls it we have match.
//! \param it The item to check against
//! \return True if the callable has called.
bool check_trigger_pop_value_ (const Data_t& it) {
bool match;
switch (trigger_.tdata.type) {
default:
case data_match::DISABLED: match = false; break;
case data_match::MATCH_POP: match = (it == trigger_.tdata.value); break;
case data_match::MISMATCH_POP: match = (it != trigger_.tdata.value); break;
}
if (match)
callback_();
return match;
}
//! Wrapper for both triggers at push
bool check_trigger_push_async_ (const Data_t& it) {
switch (mode_) {
default:
case match_mode::SIZE: return check_trigger_size_();
case match_mode::DATA: return check_trigger_push_value_(it);
}
}
//! Wrapper for both triggers at pop
bool check_trigger_pop_async_ (const Data_t& it) {
switch (mode_) {
default:
case match_mode::SIZE: return check_trigger_size_();
case match_mode::DATA: return check_trigger_pop_value_(it);
}
}
//! @}
private:
match_mode mode_{};
trigger trigger_{};
callable_t callback_{};
};
} // namespace utl
#endif /* utl_container_edeque_h__ */

+ 146
- 0
include/utl/container/equeue.h View File

@@ -0,0 +1,146 @@
/*!
* \file container/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 utl_container_equeue_h__
#define utl_container_equeue_h__
#include <utl/core/impl.h>
#include <utl/container/edeque.h>
namespace utl {
/*!
* \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 SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
* \tparam Fn The type of Callable
* \note
* SemiAtomic means it is safe to for one thread to push only from front and another can pop.
*/
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
class equeue : public edeque<Data_t, N, SemiAtomic, Fn> {
public:
// meta-identity types
using equeue_t = equeue<Data_t, N, SemiAtomic, Fn>;
using base_type = edeque<Data_t, N, SemiAtomic, Fn>;
using range_t = typename base_type::range_t;
// 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() { }
//! Forward constructor
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) noexcept {
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 () noexcept {
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) noexcept {
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) noexcept {
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 SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
* \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, bool SemiAtomic =false, typename Fn = std::function<void()>>
Data_t& operator<< (Data_t& it, equeue<Data_t, N, SemiAtomic, Fn>& q) noexcept {
it = q.pop();
return it;
}
} // namespace utl
#endif /* utl_container_equeue_h__ */

+ 144
- 0
include/utl/container/queue.h View File

@@ -0,0 +1,144 @@
/*!
* \file container/queue.h
* \brief
* A statically allocated queue based on deque.
*
* \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 utl_container_queue_h__
#define utl_container_queue_h__
#include <utl/core/impl.h>
#include <utl/container/deque.h>
namespace utl {
/*!
* \class queue
* \brief
* A statically allocated queue based on deque.
*
* We use the \ref deque::push_back() and \ref deque::pop_front() pair from deque'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 deque::front() and \ref deque::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 queue
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
* \note
* SemiAtomic means it is safe to for one thread to push only from front and another can pop.
*/
template <typename Data_t, size_t N, bool SemiAtomic =false>
class queue : public deque<Data_t, N, SemiAtomic> {
public:
// meta-identity types
using queue_t = queue<Data_t, N, SemiAtomic>;
using base_type = deque<Data_t, N, SemiAtomic>;
using range_t = typename base_type::range_t;
// 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 queue () noexcept : base_type() { }
//! fill contructor
constexpr queue(const Data_t& value) noexcept : base_type(value) { }
//! Initializer list contructor
template <typename ...It>
constexpr queue(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
constexpr void push (const Data_t& it) noexcept {
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
constexpr Data_t pop () noexcept {
return base_type::pop_front();
}
//! \brief Push an item in the back of the queue
//! \param it The item to push
constexpr queue_t& operator<< (const Data_t& it) noexcept {
push(it);
return *this;
}
//! \brief Pop an item from the front of the queue
//! \param it The item to write to
constexpr queue_t& operator>> (Data_t& it) noexcept {
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
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
*
* \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, bool SemiAtomic =false>
constexpr Data_t& operator<< (Data_t& it, queue<Data_t, N, SemiAtomic>& q) noexcept {
it = q.pop();
return it;
}
} // namespace utl
#endif /* utl_container_queue_h__ */

+ 555
- 0
test/tests/edeque.cpp View File

@@ -0,0 +1,555 @@
/*!
* \file deque.cpp
* \brief
* Unit tests for edeque
*
* \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 <utl/container/edeque.h>
#include <gtest/gtest.h>
#include <functional>
namespace Tedeque {
using namespace utl;
int global_flag =0;
// Callable mocks
void vfun(void) { ++global_flag; }
struct vfoo {
void operator() (void) { ++global_flag; }
};
TEST (Tedeque, construct) {
using Edeque = edeque<int, 8>;
struct T { int a,b; };
int local{};
Edeque e1(Edeque::size_match::GE, 3, [](){
++global_flag;
});
Edeque e2(Edeque::size_match::GE, 3, [&](){
++local;
});
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) {
using Edeque = edeque<int, 8>;
Edeque e1(Edeque::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_back(7);
EXPECT_EQ (7, e1.front());
EXPECT_EQ (7, e1.back());
EXPECT_EQ (7, e1.pop_front());
e1.push_front(42);
EXPECT_EQ (42, e1.front());
EXPECT_EQ (42, e1.back());
EXPECT_EQ (42, e1.pop_back());
e1.push_back(1);
e1.push_back(2);
e1.push_back(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
}
TEST (Tedeque, set_clear_check_trigger) {
using Edeque = edeque<int, 8>;
bool flag{};
Edeque e1(Edeque::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(Edeque::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]
Edeque e2(Edeque::data_match::MATCH_PUSH, 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(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
e2.push_back(42); // push 42, trigger
EXPECT_EQ (true, flag);
flag = false;
e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
EXPECT_EQ (true, flag);
}
TEST (Tedeque, size_triggers) {
using Edeque = edeque<int, 8>;
bool flag{};
// size_match::GE (size()>= 2)
Edeque ee(Edeque::size_match::GE, 2, [&](){ flag = true; });
flag = false;
ee.clear();
ee.push_back(1); // 1
EXPECT_EQ (false, flag);
ee.push_back(2); // 2, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(3); // 3, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::GT (size()> 1)
flag = false;
ee.clear();
ee.set_trigger(Edeque::size_match::GT, 1, [&](){ flag = true; });
ee.push_back(1); // 1
EXPECT_EQ (false, flag);
ee.push_back(2); // 2, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(3); // 3, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::LE (size()<= 1)
flag = false;
ee.clear();
ee.push_back(1); // 1
ee.push_back(2); // 2
ee.push_back(3); // 3
ee.set_trigger(Edeque::size_match::LE, 1, [&](){ flag = true; });
ee.pop_front(); // 2
EXPECT_EQ (false, flag);
ee.pop_front(); // 1, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.pop_front(); // 0, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::LT (size()< 2)
flag = false;
ee.clear();
ee.push_back(1); // 1
ee.push_back(2); // 2
ee.push_back(3); // 3
ee.set_trigger(Edeque::size_match::LT, 2, [&](){ flag = true; });
ee.pop_front(); // 2
EXPECT_EQ (false, flag);
ee.pop_front(); // 1, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.pop_front(); // 0, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::EQ (size()== 2)
flag = false;
ee.clear();
ee.set_trigger(Edeque::size_match::EQ, 2, [&](){ flag = true; });
ee.push_back(1); // 1
EXPECT_EQ (false, flag);
ee.push_back(2); // 2, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(3); // 3
ee.pop_front(); // 2, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::NE (size()!= 0)
flag = false;
ee.clear();
ee.set_trigger(Edeque::size_match::NE, 0, [&](){ flag = true; });
EXPECT_EQ (false, flag);
ee.push_back(1); // 1, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(2); // 2, no-trigger
EXPECT_EQ (false, flag);
}
TEST (Tedeque, data_triggers) {
using Edeque = edeque<int, 8>;
bool flag{};
// data_match::MATCH_PUSH (item == 42)
Edeque ee(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
flag = false;
ee.push_back(7); // 7
EXPECT_EQ (false, flag);
ee.push_back(42); // push:42, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.pop_back(); // pop:42, no-trigger
EXPECT_EQ (false, flag);
ee.push_back(42); // push:42, re-trigger
EXPECT_EQ (true, flag);
// data_match::MATCH_POP (item == 42)
flag = false;
ee.clear_trigger();
ee.set_trigger(Edeque::data_match::MATCH_POP, 42, [&](){ flag = true; });
ee.push_back(7); // 7
EXPECT_EQ (false, flag);
ee.push_back(42); // push:42, no-trigger
EXPECT_EQ (false, flag);
ee.pop_back(); // pop:42, trigger
EXPECT_EQ (true, flag);
// data_match::MISMATCH_PUSH (item != 42)
flag = false;
ee.clear();
ee.clear_trigger();
ee.push_back(7); // 7
ee.set_trigger(Edeque::data_match::MISMATCH_PUSH, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
ee.push_back(42); // 42, no-trigger
EXPECT_EQ (false, flag);
ee.push_back(0); // 0, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(1); // 1, re-trigger
EXPECT_EQ (true, flag);
// data_match::MISMATCH_POP (item != 42)
flag = false;
ee.clear();
ee.clear_trigger();
ee.push_back(7); // ->7
ee.pop_back(); // <-7
ee.set_trigger(Edeque::data_match::MISMATCH_POP, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
ee.push_back(42); // ->42, no-trigger
EXPECT_EQ (false, flag);
ee.push_back(0); // ->0, no-trigger
EXPECT_EQ (false, flag);
ee.pop_back(); // pop:0, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(0);
ee.pop_back(); // pop:0, re-trigger
EXPECT_EQ (true, flag);
}
// atomic
TEST (Tedeque, construct_atomic) {
using Edeque = edeque<int, 8, true>;
struct T { int a,b; };
int local{};
Edeque e1(Edeque::size_match::GE, 3, [](){
++global_flag;
});
Edeque e2(Edeque::size_match::GE, 3, [&](){
++local;
});
Edeque e3(Edeque::size_match::EQ, 7, vfun);
edeque<T, 8> e4(edeque<T, 8>::size_match::EQ, 2, vfoo{});
edeque<int, 8, true> q1;
edeque<int, 8, true> q2(edeque<int, 8, true>::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_atomic) {
using Edeque = edeque<int, 8, true>;
Edeque e1(Edeque::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_back(7);
EXPECT_EQ (7, e1.front());
EXPECT_EQ (7, e1.back());
EXPECT_EQ (7, e1.pop_front());
e1.push_front(42);
EXPECT_EQ (42, e1.front());
EXPECT_EQ (42, e1.back());
EXPECT_EQ (42, e1.pop_back());
e1.push_back(1);
e1.push_back(2);
e1.push_back(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
}
TEST (Tedeque, set_clear_check_trigger_atomic) {
using Edeque = edeque<int, 8, true>;
bool flag{};
Edeque e1(Edeque::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(Edeque::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]
Edeque e2(Edeque::data_match::MATCH_PUSH, 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(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
e2.push_back(42); // push 42, trigger
EXPECT_EQ (true, flag);
flag = false;
e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
EXPECT_EQ (true, flag);
}
TEST (Tedeque, size_triggers_atomic) {
using Edeque = edeque<int, 8, true>;
bool flag{};
// size_match::GE (size()>= 2)
Edeque ee(Edeque::size_match::GE, 2, [&](){ flag = true; });
flag = false;
ee.clear();
ee.push_back(1); // 1
EXPECT_EQ (false, flag);
ee.push_back(2); // 2, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(3); // 3, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::GT (size()> 1)
flag = false;
ee.clear();
ee.set_trigger(Edeque::size_match::GT, 1, [&](){ flag = true; });
ee.push_back(1); // 1
EXPECT_EQ (false, flag);
ee.push_back(2); // 2, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(3); // 3, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::LE (size()<= 1)
flag = false;
ee.clear();
ee.push_back(1); // 1
ee.push_back(2); // 2
ee.push_back(3); // 3
ee.set_trigger(Edeque::size_match::LE, 1, [&](){ flag = true; });
ee.pop_front(); // 2
EXPECT_EQ (false, flag);
ee.pop_front(); // 1, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.pop_front(); // 0, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::LT (size()< 2)
flag = false;
ee.clear();
ee.push_back(1); // 1
ee.push_back(2); // 2
ee.push_back(3); // 3
ee.set_trigger(Edeque::size_match::LT, 2, [&](){ flag = true; });
ee.pop_front(); // 2
EXPECT_EQ (false, flag);
ee.pop_front(); // 1, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.pop_front(); // 0, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::EQ (size()== 2)
flag = false;
ee.clear();
ee.set_trigger(Edeque::size_match::EQ, 2, [&](){ flag = true; });
ee.push_back(1); // 1
EXPECT_EQ (false, flag);
ee.push_back(2); // 2, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(3); // 3
ee.pop_front(); // 2, no-trigger cleared
EXPECT_EQ (false, flag);
// size_match::NE (size()!= 0)
flag = false;
ee.clear();
ee.set_trigger(Edeque::size_match::NE, 0, [&](){ flag = true; });
EXPECT_EQ (false, flag);
ee.push_back(1); // 1, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(2); // 2, no-trigger
EXPECT_EQ (false, flag);
}
TEST (Tedeque, data_triggers_atomic) {
using Edeque = edeque<int, 8, true>;
bool flag{};
// data_match::MATCH_PUSH (item == 42)
Edeque ee(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
flag = false;
ee.push_back(7); // 7
EXPECT_EQ (false, flag);
ee.push_back(42); // push:42, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.pop_back(); // pop:42, no-trigger
EXPECT_EQ (false, flag);
ee.push_back(42); // push:42, re-trigger
EXPECT_EQ (true, flag);
// data_match::MATCH_POP (item == 42)
flag = false;
ee.clear_trigger();
ee.set_trigger(Edeque::data_match::MATCH_POP, 42, [&](){ flag = true; });
ee.push_back(7); // 7
EXPECT_EQ (false, flag);
ee.push_back(42); // push:42, no-trigger
EXPECT_EQ (false, flag);
ee.pop_back(); // pop:42, trigger
EXPECT_EQ (true, flag);
// data_match::MISMATCH_PUSH (item != 42)
flag = false;
ee.clear();
ee.clear_trigger();
ee.push_back(7); // 7
ee.set_trigger(Edeque::data_match::MISMATCH_PUSH, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
ee.push_back(42); // 42, no-trigger
EXPECT_EQ (false, flag);
ee.push_back(0); // 0, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(1); // 1, re-trigger
EXPECT_EQ (true, flag);
// data_match::MISMATCH_POP (item != 42)
flag = false;
ee.clear();
ee.clear_trigger();
ee.push_back(7); // ->7
ee.pop_back(); // <-7
ee.set_trigger(Edeque::data_match::MISMATCH_POP, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
ee.push_back(42); // ->42, no-trigger
EXPECT_EQ (false, flag);
ee.push_back(0); // ->0, no-trigger
EXPECT_EQ (false, flag);
ee.pop_back(); // pop:0, trigger
EXPECT_EQ (true, flag);
flag = false;
ee.push_back(0);
ee.pop_back(); // pop:0, re-trigger
EXPECT_EQ (true, flag);
}
}

+ 317
- 0
test/tests/equeue.cpp View File

@@ -0,0 +1,317 @@
/*!
* \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 <utl/container/equeue.h>
#include <gtest/gtest.h>
namespace Tequeue {
using namespace utl;
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_PUSH, 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_PUSH, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
e2.push_back(42); // push 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);
}
// atomic
// Test construction
TEST(Tequeue, contruct_atomic) {
using Equeue = equeue<int, 8, true>;
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, true> q1;
equeue<int, 8, true> q2(equeue<int, 8, true>::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_atomic) {
using Equeue = equeue<int, 8, true>;
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_atomic) {
using Equeue = equeue<int, 8, true>;
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_PUSH, 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_PUSH, 42, [&](){ flag = true; });
EXPECT_EQ (false, flag); // no spurious triggers
e2.push_back(42); // push 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_atomic) {
equeue<int, 8, true> 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);
}
}

+ 188
- 0
test/tests/queue.cpp View File

@@ -0,0 +1,188 @@
/*!
* \file queue.cpp
* \brief
* Unit tests for queue
*
* \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 <utl/container/queue.h>
#include <gtest/gtest.h>
namespace Tqueue {
using namespace utl;
// Test construction
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());
}
// base class functionality check
TEST(Tqueue, base_class) {
queue<int, 8> q1;
// Access of base class functionality
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (0UL, q1.size());
EXPECT_EQ (true, q1.empty());
EXPECT_EQ (false, q1.full());
q1.push(42);
EXPECT_EQ (42, q1.front());
EXPECT_EQ (42, q1.back());
EXPECT_EQ (42, q1.pop());
q1.push(1);
q1.push(2);
q1.push(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
}
// stream push-pop
TEST(Tqueue, stream_push_pop) {
queue<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);
}
// Test construction
TEST(Tqueue, contruct_atomic) {
queue<int, 8, true> q1;
queue<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
queue<int, 8, true> 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());
}
// base class functionality check
TEST(Tqueue, base_class_atomic) {
queue<int, 8, true> q1;
// Access of base class functionality
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (0UL, q1.size());
EXPECT_EQ (true, q1.empty());
EXPECT_EQ (false, q1.full());
q1.push(42);
EXPECT_EQ (42, q1.front());
EXPECT_EQ (42, q1.back());
EXPECT_EQ (42, q1.pop());
q1.push(1);
q1.push(2);
q1.push(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
}
// stream push-pop
TEST(Tqueue, stream_push_pop_atomic) {
queue<int, 8, true> 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);
}
}

Loading…
Cancel
Save