From 487671a0242e3e8603f03a866b358a4fe1e20b62 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Mon, 27 Oct 2014 11:30:42 +0000 Subject: [PATCH] Added back() tests --- test/test_queue.cpp | 344 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 344 insertions(+) create mode 100644 test/test_queue.cpp diff --git a/test/test_queue.cpp b/test/test_queue.cpp new file mode 100644 index 00000000..2b63f11b --- /dev/null +++ b/test/test_queue.cpp @@ -0,0 +1,344 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. + +Copyright(c) 2014 jwellbelove + +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 "../queue.h" + +namespace +{ + SUITE(TestQueue) + { + //************************************************************************* + TEST(Size) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.push(3); + + CHECK_EQUAL(3, queue.size()); + } + + //************************************************************************* + TEST(Capacity) + { + etl::queue queue; + + CHECK_EQUAL(4, queue.capacity()); + } + + //************************************************************************* + TEST(Clear) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.clear(); + CHECK_EQUAL(0, queue.size()); + } + + //************************************************************************* + TEST(Empty) + { + etl::queue queue; + + CHECK(queue.empty()); + + queue.push(1); + + CHECK(!queue.empty()); + } + + //************************************************************************* + TEST(Full) + { + etl::queue queue; + + CHECK(!queue.full()); + + queue.push(1); + queue.push(2); + queue.push(3); + queue.push(4); + + CHECK(queue.full()); + } + + //************************************************************************* + TEST(Push) + { + etl::queue queue; + + queue.push(1); + CHECK_EQUAL(1, queue.size()); + + queue.push(2); + CHECK_EQUAL(2, queue.size()); + + CHECK_EQUAL(1, queue.front()); + + queue.pop(); + CHECK_EQUAL(2, queue.front()); + } + + //************************************************************************* + TEST(PushVoid) + { + etl::queue queue; + + queue.push() = 1; + CHECK_EQUAL(1, queue.size()); + + queue.push() = 2; + CHECK_EQUAL(2, queue.size()); + + CHECK_EQUAL(1, queue.front()); + + queue.pop(); + CHECK_EQUAL(2, queue.front()); + } + + //************************************************************************* + TEST(PushExcess) + { + etl::queue queue; + + bool gotException = false; + + try + { + for (size_t i = 0; i < queue.capacity() + 1; ++i) + { + queue.push(1); + } + } + catch (etl::queue_full_exception&) + { + gotException = true; + } + catch (...) + { + CHECK(false); + } + + CHECK(gotException); + } + + //************************************************************************* + TEST(Pop) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.pop(); + CHECK_EQUAL(1, queue.size()); + } + + //************************************************************************* + TEST(Front) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.push(3); + + CHECK_EQUAL(1, queue.front()); + + queue.pop(); + CHECK_EQUAL(2, queue.front()); + + queue.pop(); + CHECK_EQUAL(3, queue.front()); + } + + //************************************************************************* + TEST(FrontConst) + { + etl::queue queue; + const etl::queue& constQueue = queue; + + queue.push(1); + queue.push(2); + queue.push(3); + + CHECK_EQUAL(1, constQueue.front()); + + queue.pop(); + CHECK_EQUAL(2, constQueue.front()); + + queue.pop(); + CHECK_EQUAL(3, constQueue.front()); + } + + //************************************************************************* + TEST(FrontException) + { + etl::queue queue; + + bool gotException = false; + + try + { + queue.front(); + } + catch (etl::queue_empty_exception&) + { + gotException = true; + } + catch (...) + { + CHECK(false); + } + + CHECK(gotException); + } + + //************************************************************************* + TEST(Back) + { + etl::queue queue; + + queue.push(1); + CHECK_EQUAL(1, queue.back()); + + queue.push(2); + CHECK_EQUAL(2, queue.back()); + + queue.push(3); + CHECK_EQUAL(3, queue.back()); + } + + //************************************************************************* + TEST(BackConst) + { + etl::queue queue; + const etl::queue& constQueue = queue; + + queue.push(1); + CHECK_EQUAL(1, constQueue.back()); + + queue.push(2); + CHECK_EQUAL(2, constQueue.back()); + + queue.push(3); + CHECK_EQUAL(3, constQueue.back()); + } + + //************************************************************************* + TEST(BackException) + { + etl::queue queue; + + bool gotException = false; + + try + { + queue.back(); + } + catch (etl::queue_empty_exception&) + { + gotException = true; + } + catch (...) + { + CHECK(false); + } + + CHECK(gotException); + } + + //************************************************************************* + TEST(CheckMultiplePush) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.push(3); + + bool pass = true; + + if (queue.front() != 1) + { + pass = false; + } + + queue.pop(); + + if (queue.front() != 2) + { + pass = false; + } + + queue.pop(); + + if (queue.front() != 3) + { + pass = false; + } + + CHECK(pass); + } + + //************************************************************************* + TEST(CheckMultiplePushVoid) + { + etl::queue queue; + + queue.push() = 1; + queue.push() = 2; + queue.push() = 3; + + bool pass = true; + + if (queue.front() != 1) + { + pass = false; + } + + queue.pop(); + + if (queue.front() != 2) + { + pass = false; + } + + queue.pop(); + + if (queue.front() != 3) + { + pass = false; + } + + CHECK(pass); + } + }; +} \ No newline at end of file