Browse Source

A queue based on deque added

master
parent
commit
ee9a3dfca1
2 changed files with 241 additions and 0 deletions
  1. +122
    -0
      include/cont/queue.h
  2. +119
    -0
      test/tests/queue.cpp

+ 122
- 0
include/cont/queue.h View File

@@ -0,0 +1,122 @@
/*!
* \file cont/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 TBX_CONT_QUQUE_H_
#define TBX_CONT_QUQUE_H_
#include <core/core.h>
#include <cont/deque.h>
namespace tbx {
/*!
* \class deque
* \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 deque
*/
template <typename Data_t, size_t N>
class queue : public deque<Data_t, N> {
public:
// meta-identity types
using queue_t = queue<Data_t, N>;
using base_type = deque<Data_t, N>;
// 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
queue () noexcept : base_type() { }
//! fill contructor
queue(const Data_t& value) noexcept : base_type(value) { }
//! Initializer list contructor
template <typename ...It>
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
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
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) {
it = pop();
return *this;
}
//! @}
};
}
#endif /* TBX_CONT_QUQUE_H_ */

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

@@ -0,0 +1,119 @@
/*!
* \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 <cont/queue.h>
#include <gtest/gtest.h>
namespace Tqueue {
using namespace tbx;
// Test construction
TEST(Tqueue, contruct) {
queue<int, 8> q1;
queue<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
EXPECT_EQ (8UL, q1.capacity());
EXPECT_EQ (8UL, q2.capacity());
}
// simple push-pop functionality
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_back(7);
EXPECT_EQ (7, q1.front());
EXPECT_EQ (7, q1.back());
EXPECT_EQ (7, q1.pop_front());
q1.push_front(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());
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 operations
TEST(Tqueue, capacity) {
queue<int, 8> q1;
// stream 5 items
q1 << 1 << 2 << 3 << 4 << 5;
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
// get all back
int it;
for (int check_it=1 ; check_it<=5 ; ++check_it) {
q1 >> it;
EXPECT_EQ (check_it, it);
}
}
}

Loading…
Cancel
Save