utl/test/tests/queue.cpp

189 lines
5.8 KiB
C++

/*!
* \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);
}
}