From 585b494aa1ed43d81f74d75604a03e6559be2a70 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Fri, 31 Oct 2014 15:58:49 +0000 Subject: [PATCH] Added unit tests for new classes --- test/test_container.cpp | 145 +++++++ test/test_cyclic_value.cpp | 194 +++++++++ test/test_largest.cpp | 83 ++++ test/test_numeric.cpp | 51 +++ test/test_observer.cpp | 433 +++++++++++++++++++ test/test_type_traits.cpp | 487 ++++++++++++++++++++++ test/test_vector.cpp | 831 +++++++++++++++++++++++++++++++++++++ test/test_visitor.cpp | 719 ++++++++++++++++++++++++++++++++ 8 files changed, 2943 insertions(+) create mode 100644 test/test_container.cpp create mode 100644 test/test_cyclic_value.cpp create mode 100644 test/test_largest.cpp create mode 100644 test/test_numeric.cpp create mode 100644 test/test_observer.cpp create mode 100644 test/test_type_traits.cpp create mode 100644 test/test_vector.cpp create mode 100644 test/test_visitor.cpp diff --git a/test/test_container.cpp b/test/test_container.cpp new file mode 100644 index 00000000..125c74a3 --- /dev/null +++ b/test/test_container.cpp @@ -0,0 +1,145 @@ +/****************************************************************************** +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 "../container.h" + +#include + +namespace +{ + SUITE(ContainerTest) + { + //************************************************************************* + TEST(TestSTLContainer) + { + const size_t SIZE = 10; + std::list data(SIZE); + + std::list::iterator iBegin = etl::begin(data); + CHECK(data.begin() == iBegin); + + std::list::iterator iEnd = etl::end(data); + CHECK(data.end() == iEnd); + } + + //************************************************************************* + TEST(TestConstSTLContainer) + { + const size_t SIZE = 10; + const std::list data(SIZE); + + std::list::const_iterator iBegin = etl::begin(data); + CHECK(data.begin() == iBegin); + + const std::list::const_iterator iEnd = etl::end(data); + CHECK(data.end() == iEnd); + } + + //************************************************************************* + TEST(TestCArray) + { + const size_t SIZE = 10; + int data[SIZE]; + + int* iBegin = etl::begin(data); + CHECK(&data[0] == iBegin); + + int* iEnd = etl::end(data); + CHECK(&data[SIZE] == iEnd); + } + + //************************************************************************* + TEST(TestConstCArray) + { + const size_t SIZE = 10; + const int data[SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + const int* const iBegin = etl::begin(data); + CHECK(&data[0] == iBegin); + + const int* const iEnd = etl::end(data); + CHECK(&data[SIZE] == iEnd); + } + + + //************************************************************************* + TEST(TestNext) + { + const int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + const int* p; + + p = etl::next(std::begin(data)); + CHECK_EQUAL(data[1], *p); + + p = etl::next(std::begin(data), 1); + CHECK_EQUAL(data[1], *p); + + p = etl::next(std::begin(data), 5); + CHECK_EQUAL(data[5], *p); + } + + //************************************************************************* + TEST(TestPrev) + { + const int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + const int* p; + + p = etl::prev(std::end(data)); + CHECK_EQUAL(data[9], *p); + + p = etl::prev(std::end(data), 1); + CHECK_EQUAL(data[9], *p); + + p = etl::prev(std::end(data), 5); + CHECK_EQUAL(data[5], *p); + } + + //************************************************************************* + TEST(TestSTLContainerSize) + { + const size_t SIZE = 10; + std::list data(SIZE); + + size_t runtime_size = etl::size(data); + CHECK_EQUAL(SIZE, runtime_size); + } + + //************************************************************************* + TEST(TestCArraySize) + { + const size_t SIZE = 10; + int data[SIZE]; + + size_t runtime_size = etl::size(data); + CHECK_EQUAL(SIZE, runtime_size); + + size_t compiletime_size = sizeof(etl::array_size(data)); + CHECK_EQUAL(SIZE, compiletime_size); + } + }; +} diff --git a/test/test_cyclic_value.cpp b/test/test_cyclic_value.cpp new file mode 100644 index 00000000..62439d47 --- /dev/null +++ b/test/test_cyclic_value.cpp @@ -0,0 +1,194 @@ +///\file + +/****************************************************************************** +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 "../cyclic_value.h" + +namespace +{ + SUITE(TestCyclicValue) + { + //************************************************************************* + TEST(CompileTimeInitialisation) + { + etl::cyclic_value value; + + CHECK_EQUAL(2, value); + CHECK_EQUAL(2, value.first()); + CHECK_EQUAL(7, value.last()); + } + + //************************************************************************* + TEST(RunTimeInitialisation) + { + etl::cyclic_value value(2, 7, 1); + + CHECK_EQUAL(2, value); + CHECK_EQUAL(2, value.first()); + CHECK_EQUAL(7, value.last()); + } + + //************************************************************************* + TEST(set) + { + etl::cyclic_value value; + + value.set(2, 7); + + CHECK_EQUAL(2, value); + CHECK_EQUAL(2, value.first()); + CHECK_EQUAL(7, value.last()); + } + + //************************************************************************* + TEST(ToFirst) + { + etl::cyclic_value value; + + value.set(2, 7); + ++value; + value.to_first(); + + CHECK_EQUAL(value.first(), value); + } + + //************************************************************************* + TEST(ToLast) + { + etl::cyclic_value value; + + value.set(2, 7); + value.to_last(); + + CHECK_EQUAL(value.last(), value); + } + + //************************************************************************* + TEST(Increment) + { + etl::cyclic_value value; + + value.set(2, 7); + + for (int i = value.first(); i <= value.last(); ++i) + { + CHECK_EQUAL(i, value); + ++value; + } + } + + //************************************************************************* + TEST(Decrement) + { + etl::cyclic_value value; + + value.set(2, 7); + value.to_last(); + + for (int i = value.last(); i >= value.first(); --i) + { + CHECK_EQUAL(i, value); + --value; + } + } + + //************************************************************************* + TEST(IncrementWrap) + { + etl::cyclic_value value; + + value.set(2, 7); + + int expected[8] = { 2, 3, 4, 5, 6, 7, 2, 3 }; + + for (int i = 0; i < 8; ++i) + { + CHECK_EQUAL(expected[i], value); + ++value; + } + } + + //************************************************************************* + TEST(DecrementWrap) + { + etl::cyclic_value value; + + value.set(2, 7); + + int expected[8] = { 2, 7, 6, 5, 4, 3, 2, 7 }; + + for (int i = 0; i > 8; ++i) + { + CHECK_EQUAL(expected[i], value); + --value; + } + } + + //************************************************************************* + TEST(NumberOfSteps) + { + etl::cyclic_value value; + + value.set(2, 7, 1); + CHECK_EQUAL(7 - 2 + 1, value.number_of_steps()); + + value.set(2, 7, 2); + CHECK_EQUAL((7 - 2 + 1) / 2, value.number_of_steps()); + } + + //************************************************************************* + TEST(AdvancePositive) + { + etl::cyclic_value value; + + value.advance(2); + + CHECK_EQUAL(4, value); + } + + //************************************************************************* + TEST(AdvanceNegative) + { + etl::cyclic_value value; + + value.to_last(); + value.advance(-2); + + CHECK_EQUAL(5, value); + } + + //************************************************************************* + TEST(TestException) + { + etl::cyclic_value value; + + CHECK_THROW(value.set(2, 7, 4), etl::cyclic_value_illegal_range_exception); + } + }; +} \ No newline at end of file diff --git a/test/test_largest.cpp b/test/test_largest.cpp new file mode 100644 index 00000000..19b53cba --- /dev/null +++ b/test/test_largest.cpp @@ -0,0 +1,83 @@ +/****************************************************************************** +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 "../largest.h" + +#include + +namespace +{ + SUITE(TestLargest) + { + //************************************************************************* + TEST(PODTest) + { + size_t size; + bool type; + + size = etl::largest::size; + type = std::is_same::type>::value; + + CHECK_EQUAL(sizeof(int), size); + CHECK(type); + + size = etl::largest::size; + type = std::is_same::type>::value; + + CHECK_EQUAL(sizeof(int), size); + CHECK(type); + } + + //************************************************************************* + TEST(NonPODTest) + { + size_t size; + bool type; + + struct S1 { char a; char b; char c; }; + struct S2 { char a; short b; char c; }; + struct S3 { int a; short b; char c; }; + + size_t a = sizeof(S1); + size_t b = sizeof(S2); + size_t c = sizeof(S3); + + size = etl::largest::size; + type = std::is_same::type>::value; + + CHECK_EQUAL(sizeof(S3), size); + CHECK(type); + + size = etl::largest::size; + type = std::is_same::type>::value; + + CHECK_EQUAL(sizeof(S3), size); + CHECK(type); + } + }; +} diff --git a/test/test_numeric.cpp b/test/test_numeric.cpp new file mode 100644 index 00000000..843f0da1 --- /dev/null +++ b/test/test_numeric.cpp @@ -0,0 +1,51 @@ +/****************************************************************************** +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 "../numeric.h" + +#include +#include + +namespace +{ + SUITE(TestNumeric) + { + //************************************************************************* + TEST(IOTA) + { + int data1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int data2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + etl::iota(std::begin(data2), std::end(data2), 0); + + bool are_same = std::equal(std::begin(data1), std::end(data1), std::begin(data2)); + + CHECK(are_same); + } + }; +} diff --git a/test/test_observer.cpp b/test/test_observer.cpp new file mode 100644 index 00000000..a0b37813 --- /dev/null +++ b/test/test_observer.cpp @@ -0,0 +1,433 @@ +/****************************************************************************** +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 "../observer.h" + +//***************************************************************************** +// Notification1 +//***************************************************************************** +struct Notification1 +{ +}; + +//***************************************************************************** +// Notification2 +//***************************************************************************** +struct Notification2 +{ +}; + +//***************************************************************************** +// Notification3 +//***************************************************************************** +struct Notification3 +{ +}; + +//***************************************************************************** +// Generic notification. +//***************************************************************************** +template +struct Notification +{ +}; + +//***************************************************************************** +// The observer base type. +// Declare what notifications you want to observe and how they are passed to 'notification'. +// The Notification1 is passed by value. +// The Notification2 is passed by reference. +// The Notification3 is passed by const reference. +//***************************************************************************** +typedef etl::observer ObserverType; + +//***************************************************************************** +// The concrete observable 1 class. +//***************************************************************************** +class Observable1 : public etl::observable +{ +public: + + Notification1 data1; + Notification2 data2; + + //********************************* + // Notify all of the observers. + //********************************* + void send_notifications() + { + notify_observers(data1); + notify_observers(data2); + } +}; + +//***************************************************************************** +// The concrete observable 2 class. +//***************************************************************************** +class Observable2 : public etl::observable +{ +public: + + Notification3 data3; + + //********************************* + // Notify all of the observers. + //********************************* + void send_notifications() + { + notify_observers(data3); + } +}; + +//***************************************************************************** +// The first observer type. +// If any one of the overloads is missing or a parameter declaration is incorrect +// then the class will be 'abstract' and will not compile. +//***************************************************************************** +class Observer1 : public ObserverType +{ +public: + + Observer1() + : data1_count(0), + data2_count(0), + data3_count(0) + { + } + + //******************************************* + // Notification1 is passed by value. + //******************************************* + void notification(Notification1 data1) + { + ++data1_count; + } + + //******************************************* + // Notification2 is passed by reference. + //******************************************* + void notification(Notification2& data2) + { + ++data2_count; + } + + //******************************************* + // Notification3 is passed by const reference. + //******************************************* + void notification(const Notification3& data3) + { + ++data3_count; + } + + int data1_count; + int data2_count; + int data3_count; +}; + +//***************************************************************************** +// The second observer type. +// If any one of the overloads is missing or a parameter declaration is incorrect +// then the class will be 'abstract' and will not compile. +//***************************************************************************** +class Observer2 : public ObserverType +{ +public: + + Observer2() + : data1_count(0), + data2_count(0), + data3_count(0) + { + } + + //******************************************* + // Notification1 is passed by value. + //******************************************* + void notification(Notification1 data1) + { + ++data1_count; + } + + //******************************************* + // Notification2 is passed by reference. + //******************************************* + void notification(Notification2& data2) + { + ++data2_count; + } + + //******************************************* + // Notification3 is passed by const reference. + //******************************************* + void notification(const Notification3& data3) + { + ++data3_count; + } + + int data1_count; + int data2_count; + int data3_count; +}; + +namespace +{ + SUITE(TestObserver) + { + //************************************************************************* + TEST(Test2Observables2Observers3Notifications) + { + // The observable objects. + Observable1 observable1; + Observable2 observable2; + + // The observer objects. + Observer1 observer1; + Observer2 observer2; + + observable1.add_observer(observer1); + + // Send the notifications. + observable1.send_notifications(); // Updates data1 & data2. + + CHECK_EQUAL(1, observer1.data1_count); + CHECK_EQUAL(1, observer1.data2_count); + CHECK_EQUAL(0, observer1.data3_count); + + CHECK_EQUAL(0, observer2.data1_count); + CHECK_EQUAL(0, observer2.data2_count); + CHECK_EQUAL(0, observer2.data3_count); + + observable2.send_notifications(); // Updates data3. + + CHECK_EQUAL(1, observer1.data1_count); + CHECK_EQUAL(1, observer1.data2_count); + CHECK_EQUAL(0, observer1.data3_count); + + CHECK_EQUAL(0, observer2.data1_count); + CHECK_EQUAL(0, observer2.data2_count); + CHECK_EQUAL(0, observer2.data3_count); + + // Add another observer to both. + observable1.add_observer(observer2); + observable2.add_observer(observer2); + + // Send the notifications. + observable1.send_notifications(); // Updates data1 & data2. + + CHECK_EQUAL(2, observer1.data1_count); + CHECK_EQUAL(2, observer1.data2_count); + CHECK_EQUAL(0, observer1.data3_count); + + CHECK_EQUAL(1, observer2.data1_count); + CHECK_EQUAL(1, observer2.data2_count); + CHECK_EQUAL(0, observer2.data3_count); + + observable2.send_notifications(); // Updates data3. + + CHECK_EQUAL(2, observer1.data1_count); + CHECK_EQUAL(2, observer1.data2_count); + CHECK_EQUAL(0, observer1.data3_count); + + CHECK_EQUAL(1, observer2.data1_count); + CHECK_EQUAL(1, observer2.data2_count); + CHECK_EQUAL(1, observer2.data3_count); + + observable1.remove_observer(observer1); + + // Send the notifications. + observable1.send_notifications(); // Updates data1 & data2. + + CHECK_EQUAL(2, observer1.data1_count); + CHECK_EQUAL(2, observer1.data2_count); + CHECK_EQUAL(0, observer1.data3_count); + + CHECK_EQUAL(2, observer2.data1_count); + CHECK_EQUAL(2, observer2.data2_count); + CHECK_EQUAL(1, observer2.data3_count); + + observable2.send_notifications(); // Updates data3. + + CHECK_EQUAL(2, observer1.data1_count); + CHECK_EQUAL(2, observer1.data2_count); + CHECK_EQUAL(0, observer1.data3_count); + + CHECK_EQUAL(2, observer2.data1_count); + CHECK_EQUAL(2, observer2.data2_count); + CHECK_EQUAL(2, observer2.data3_count); + } + + //************************************************************************* + TEST(Test8Notifications) + { + typedef etl::observer, Notification<2>, Notification<3>, Notification<4>, Notification<5>, Notification<6>, Notification<7>, Notification<8>> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test7Notifications) + { + typedef etl::observer, Notification<2>, Notification<3>, Notification<4>, Notification<5>, Notification<6>, Notification<7>> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test6Notifications) + { + typedef etl::observer, Notification<2>, Notification<3>, Notification<4>, Notification<5>, Notification<6>> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test5Notifications) + { + typedef etl::observer, Notification<2>, Notification<3>, Notification<4>, Notification<5>> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test4Notifications) + { + typedef etl::observer, Notification<2>, Notification<3>, Notification<4>> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test3Notifications) + { + typedef etl::observer, Notification<2>, Notification<3>> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test2Notifications) + { + typedef etl::observer, Notification<2>> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test1Notification) + { + typedef etl::observer> Observer; + + class Observable : public etl::observable + { + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(TestObserverList) + { + class Observer : public etl::observer + { + void notification(Notification1) {} + }; + + class Observable : public etl::observable + { + }; + + Observable observable; + + Observer observer1; + Observer observer2; + Observer observer3; + Observer observer4; + Observer observer5; + + observable.add_observer(observer1); + CHECK_EQUAL(1, observable.number_of_observers()); + + observable.add_observer(observer2); + CHECK_EQUAL(2, observable.number_of_observers()); + + observable.add_observer(observer3); + CHECK_EQUAL(3, observable.number_of_observers()); + + observable.add_observer(observer2); + CHECK_EQUAL(3, observable.number_of_observers()); + + observable.add_observer(observer4); + CHECK_EQUAL(4, observable.number_of_observers()); + + CHECK_THROW(observable.add_observer(observer5), etl::observer_list_full_exception); + + observable.remove_observer(observer3); + CHECK_EQUAL(3, observable.number_of_observers()); + + observable.clear_observers(); + CHECK_EQUAL(0, observable.number_of_observers()); + } + } +} + diff --git a/test/test_type_traits.cpp b/test/test_type_traits.cpp new file mode 100644 index 00000000..00d6673b --- /dev/null +++ b/test/test_type_traits.cpp @@ -0,0 +1,487 @@ +/****************************************************************************** +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 + +#include "../type_traits.h" + +// A class to test non-fundamental types. +struct Test +{ + int a; +}; + +namespace +{ + SUITE(TestTypeTraits) + { + //************************************************************************* + TEST(is_integral) + { + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + CHECK(etl::is_integral::value == std::is_integral::value); + } + + //************************************************************************* + TEST(is_signed) + { + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + CHECK(etl::is_signed::value == std::is_signed::value); + } + + //************************************************************************* + TEST(is_unsigned) + { + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + CHECK(etl::is_unsigned::value == std::is_unsigned::value); + } + + //************************************************************************* + TEST(is_floating_point) + { + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_floating_point::value); + CHECK(etl::is_floating_point::value == std::is_signed::value); + } + + //************************************************************************* + TEST(is_pointer) + { + CHECK(etl::is_pointer::value == std::is_pointer::value); + CHECK(etl::is_pointer::value == std::is_pointer::value); + CHECK(etl::is_pointer::value == std::is_pointer::value); + CHECK(etl::is_pointer::value == std::is_pointer::value); + CHECK(etl::is_pointer::value == std::is_pointer::value); + } + + //************************************************************************* + TEST(is_reference) + { + CHECK(etl::is_reference::value == std::is_reference::value); + CHECK(etl::is_reference::value == std::is_reference::value); + CHECK(etl::is_reference::value == std::is_reference::value); + CHECK(etl::is_reference::value == std::is_reference::value); + CHECK(etl::is_reference::value == std::is_reference::value); + } + + //************************************************************************* + TEST(is_same) + { + CHECK((etl::is_same::value == std::is_same::value)); + CHECK((etl::is_same::value == std::is_same::value)); + } + + //************************************************************************* + TEST(is_array) + { + CHECK(etl::is_array::value == std::is_array::value); + CHECK(etl::is_array::value == std::is_array::value); + } + + //************************************************************************* + TEST(remove_pointer) + { + CHECK((std::is_same::type, std::remove_pointer::type>::value)); + CHECK((std::is_same::type, std::remove_pointer::type>::value)); + CHECK((std::is_same::type, std::remove_pointer::type>::value)); + CHECK((std::is_same::type, std::remove_pointer::type>::value)); + CHECK((std::is_same::type, std::remove_pointer::type>::value)); + } + + //************************************************************************* + TEST(add_pointer) + { + CHECK((std::is_same::type, std::add_pointer::type>::value)); + CHECK((std::is_same::type, std::add_pointer::type>::value)); + CHECK((std::is_same::type, std::add_pointer::type>::value)); + CHECK((std::is_same::type, std::add_pointer::type>::value)); + CHECK((std::is_same::type, std::add_pointer::type>::value)); + } + + //************************************************************************* + TEST(remove_reference) + { + CHECK((std::is_same::type, std::remove_reference::type>::value)); + CHECK((std::is_same::type, std::remove_reference::type>::value)); + CHECK((std::is_same::type, std::remove_reference::type>::value)); + CHECK((std::is_same::type, std::remove_reference::type>::value)); + CHECK((std::is_same::type, std::remove_reference::type>::value)); + } + + //************************************************************************* + TEST(add_reference) + { + CHECK((std::is_same::type, std::add_reference::type>::value)); + CHECK((std::is_same::type, std::add_reference::type>::value)); + CHECK((std::is_same::type, std::add_reference::type>::value)); + CHECK((std::is_same::type, std::add_reference::type>::value)); + CHECK((std::is_same::type, std::add_reference::type>::value)); + + } + + //************************************************************************* + TEST(remove_const) + { + CHECK((std::is_same::type, std::remove_const::type>::value)); + CHECK((std::is_same::type, std::remove_const::type>::value)); + CHECK((std::is_same::type, std::remove_const::type>::value)); + } + + //************************************************************************* + TEST(add_const) + { + CHECK((std::is_same::type, std::add_const::type>::value)); + CHECK((std::is_same::type, std::add_const::type>::value)); + CHECK((std::is_same::type, std::add_const::type>::value)); + } + + //************************************************************************* + TEST(is_const) + { + CHECK(etl::is_const::value == std::is_const::value); + CHECK(etl::is_const::value == std::is_const::value); + CHECK(etl::is_const::value == std::is_const::value); + } + + //************************************************************************* + TEST(is_volatile) + { + CHECK(etl::is_volatile::value == std::is_volatile::value); + CHECK(etl::is_volatile::value == std::is_volatile::value); + CHECK(etl::is_volatile::value == std::is_volatile::value); + } + + //************************************************************************* + TEST(remove_volatile) + { + CHECK((std::is_same::type, std::remove_volatile::type>::value)); + CHECK((std::is_same::type, std::remove_volatile::type>::value)); + CHECK((std::is_same::type, std::remove_volatile::type>::value)); + } + + //************************************************************************* + TEST(add_volatile) + { + CHECK((std::is_same::type, std::add_volatile::type>::value)); + CHECK((std::is_same::type, std::add_volatile::type>::value)); + CHECK((std::is_same::type, std::add_volatile::type>::value)); + } + + //************************************************************************* + TEST(remove_cv) + { + CHECK((std::is_same::type, std::remove_cv::type>::value)); + CHECK((std::is_same::type, std::remove_cv::type>::value)); + CHECK((std::is_same::type, std::remove_cv::type>::value)); + CHECK((std::is_same::type, std::remove_cv::type>::value)); + } + + //************************************************************************* + TEST(add_cv) + { + + typedef etl::add_cv::type t1; + typedef std::add_cv::type t2; + + bool pass = std::is_same::value; + //std::is_same::type, std::add_cv::type>::value; + + CHECK(pass); + CHECK((std::is_same::type, std::add_cv::type>::value)); + CHECK((std::is_same::type, std::add_cv::type>::value)); + CHECK((std::is_same::type, std::add_cv::type>::value)); + } + + //************************************************************************* + TEST(is_arithmetic) + { + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + CHECK(etl::is_arithmetic::value == std::is_arithmetic::value); + } + + //************************************************************************* + TEST(is_fundamental) + { + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + CHECK(etl::is_fundamental::value == std::is_fundamental::value); + } + + //************************************************************************* + TEST(is_void) + { + CHECK(etl::is_void::value == std::is_void::value); + CHECK(etl::is_void::value == std::is_void::value); + } + + //************************************************************************* + TEST(make_signed) + { + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + CHECK((std::is_same::type, std::make_signed::type>::value)); + } + + //************************************************************************* + TEST(make_unsigned) + { + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + CHECK((std::is_same::type, std::make_unsigned::type>::value)); + } + + //************************************************************************* + TEST(extent) + { + CHECK((std::is_same::type, std::extent::type>::value)); + CHECK((std::is_same::type, std::extent::type>::value)); + CHECK((std::is_same::type, std::extent::type>::value)); + } + + //************************************************************************* + TEST(remove_extent) + { + CHECK((std::is_same::type, std::remove_extent::type>::value)); + CHECK((std::is_same::type, std::remove_extent::type>::value)); + CHECK((std::is_same::type, std::remove_extent::type>::value)); + } + + //************************************************************************* + TEST(remove_all_extents) + { + CHECK((std::is_same::type, std::remove_all_extents::type>::value)); + CHECK((std::is_same::type, std::remove_all_extents::type>::value)); + CHECK((std::is_same::type, std::remove_all_extents::type>::value)); + } + + //************************************************************************* + TEST(rank) + { + CHECK(etl::rank::value == std::rank::value); + CHECK(etl::rank::value == std::rank::value); + CHECK(etl::rank::value == std::rank::value); + } + + //************************************************************************* + TEST(alignment_of) + { + struct Test + { + int a; + char b; + float c; + }; + + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + CHECK_EQUAL(std::alignment_of::value, etl::alignment_of::value); + } + }; +} \ No newline at end of file diff --git a/test/test_vector.cpp b/test/test_vector.cpp new file mode 100644 index 00000000..06626217 --- /dev/null +++ b/test/test_vector.cpp @@ -0,0 +1,831 @@ +/****************************************************************************** +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 +#include +#include + +#include "../vector.h" + +namespace +{ + SUITE(TestVector) + { + static const size_t SIZE = 10; + + typedef etl::vector Data; + typedef std::vector TestData; + + TestData initial_data; + TestData less_data; + TestData greater_data; + TestData shorter_data; + TestData different_data; + + //************************************************************************* + struct SetupFixture + { + SetupFixture() + { + int n[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int n_less[] = { 0, 1, 2, 3, 3, 5, 6, 7, 8, 9 }; + int n_greater[] = { 0, 1, 2, 4, 4, 5, 6, 7, 8, 9 }; + + initial_data.assign(std::begin(n), std::end(n)); + less_data.assign(std::begin(n_less), std::end(n_less)); + greater_data.assign(std::begin(n_greater), std::end(n_greater)); + shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); + different_data.assign(initial_data.rbegin(), initial_data.rend()); + } + }; + + //************************************************************************* + TEST_FIXTURE(SetupFixture, DefaultConstructor) + { + Data data; + + CHECK_EQUAL(data.size(), size_t(0)); + CHECK(data.empty()); + CHECK_EQUAL(data.capacity(), SIZE); + CHECK_EQUAL(data.max_size(), SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ConstructorSize) + { + const size_t INITIAL_SIZE = 5; + Data data(INITIAL_SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ConstructorSizeValue) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + std::array testData; + testData.fill(INITIAL_VALUE); + + Data data(INITIAL_SIZE, INITIAL_VALUE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ConstructorRange) + { + TestData testData(initial_data.begin(), initial_data.end()); + + Data data(testData.begin(), testData.end()); + + CHECK(data.size() == SIZE); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Iterator) + { + TestData testData(initial_data.begin(), initial_data.end()); + + Data data(testData.begin(), testData.end()); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ConstIterator) + { + TestData testData(initial_data.begin(), initial_data.end()); + + Data data(testData.begin(), testData.end()); + + bool isEqual = std::equal(data.cbegin(), + data.cend(), + testData.cbegin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ReverseIterator) + { + TestData testData(initial_data.begin(), initial_data.end()); + + Data data(testData.begin(), testData.end()); + + bool isEqual = std::equal(data.rbegin(), + data.rend(), + testData.rbegin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ConstReverseIterator) + { + TestData testData(initial_data.begin(), initial_data.end()); + + Data data(testData.begin(), testData.end()); + + bool isEqual = std::equal(data.crbegin(), + data.crend(), + testData.crbegin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ResizeUp) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + + Data data(INITIAL_SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ResizeUpValue) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + const int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, INITIAL_VALUE); + data.resize(NEW_SIZE, INITIAL_VALUE); + + std::array testData; + testData.fill(INITIAL_VALUE); + + bool isEqual = std::equal(data.begin(), data.end(), testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ResizeExcess) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = SIZE + 1; + + Data data(INITIAL_SIZE); + + CHECK_THROW(data.resize(NEW_SIZE), etl::vector_full_exception); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, ResizeDown) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + + Data data(INITIAL_SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Indexing) + { + const size_t INITIAL_SIZE = 5; + TestData testData(initial_data.begin(), initial_data.end()); + + Data data(testData.begin(), testData.end()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], testData[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, IndexingConst) + { + const size_t INITIAL_SIZE = 5; + const TestData testData(initial_data.begin(), initial_data.end()); + + const Data data(testData.begin(), testData.end()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], testData[i]); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Clear) + { + const size_t INITIAL_SIZE = 5; + TestData testData(initial_data.begin(), initial_data.end()); + + Data data(testData.begin(), testData.end()); + data.clear(); + + CHECK_EQUAL(data.size(), size_t(0)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, AssignRange) + { + const size_t INITIAL_SIZE = 5; + TestData testData(initial_data.begin(), initial_data.end()); + + Data data; + + data.assign(testData.begin(), testData.end()); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, AssignSizeValue) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + std::array testData; + testData.fill(INITIAL_VALUE); + + Data data; + data.assign(INITIAL_SIZE, INITIAL_VALUE); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, AssignSizeValueExcess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t EXCESS_SIZE = SIZE + 1; + const int INITIAL_VALUE = 1; + std::array testData; + testData.fill(INITIAL_VALUE); + + Data data; + + CHECK_THROW(data.assign(EXCESS_SIZE, INITIAL_VALUE), etl::vector_full_exception); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, InsertPositionValue) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + const int INSERT_VALUE = 2; + + TestData testData(INITIAL_SIZE, INITIAL_VALUE); + Data data(INITIAL_SIZE, INITIAL_VALUE); + + size_t offset = 2; + + data.insert(data.begin() + offset, INITIAL_VALUE); + testData.insert(testData.begin() + offset, INITIAL_VALUE); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + + offset = 0; + + data.insert(data.begin() + offset, INITIAL_VALUE); + testData.insert(testData.begin() + offset, INITIAL_VALUE); + + isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + + offset = data.size(); + + data.insert(data.begin() + offset, INITIAL_VALUE); + testData.insert(testData.begin() + offset, INITIAL_VALUE); + + isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, InsertPositionValueExcess) + { + const size_t INITIAL_SIZE = SIZE; + const int INITIAL_VALUE = 1; + const int INSERT_VALUE = 2; + + Data data(INITIAL_SIZE, INITIAL_VALUE); + + size_t offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, INITIAL_VALUE), etl::vector_full_exception); + + offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, INITIAL_VALUE), etl::vector_full_exception); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, INITIAL_VALUE), etl::vector_full_exception); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, InsertPositionNValue) + { + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + const int INITIAL_VALUE = 1; + const int INSERT_VALUE = 2; + + TestData testData; + Data data; + + size_t offset = 2; + + data.assign(INITIAL_SIZE, INITIAL_VALUE); + testData.assign(INITIAL_SIZE, INITIAL_VALUE); + data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + testData.insert(testData.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + + offset = 0; + + data.assign(INITIAL_SIZE, INITIAL_VALUE); + testData.assign(INITIAL_SIZE, INITIAL_VALUE); + data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + testData.insert(testData.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + + isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + + data.assign(INITIAL_SIZE, INITIAL_VALUE); + testData.assign(INITIAL_SIZE, INITIAL_VALUE); + offset = data.size(); + data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + testData.insert(testData.begin() + offset, INSERT_SIZE, INITIAL_VALUE); + + isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, InsertPositionNValueExcess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t INSERT_SIZE = 4; + const int INITIAL_VALUE = 1; + const int INSERT_VALUE = 2; + + Data data(INITIAL_SIZE, INITIAL_VALUE); + + size_t offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE), etl::vector_full_exception); + + offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE), etl::vector_full_exception); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, INITIAL_VALUE), etl::vector_full_exception); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, InsertPositionRange) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + TestData testData; + Data data; + + size_t offset = 2; + + data.assign(INITIAL_SIZE, INITIAL_VALUE); + testData.assign(INITIAL_SIZE, INITIAL_VALUE); + data.insert(data.begin() + offset, initial_data.begin(), initial_data.begin() + 3); + testData.insert(testData.begin() + offset, initial_data.begin(), initial_data.begin() + 3); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + + offset = 0; + + data.assign(INITIAL_SIZE, INITIAL_VALUE); + testData.assign(INITIAL_SIZE, INITIAL_VALUE); + data.insert(data.begin() + offset, initial_data.begin(), initial_data.begin() + 3); + testData.insert(testData.begin() + offset, initial_data.begin(), initial_data.begin() + 3); + + isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + + data.assign(INITIAL_SIZE, INITIAL_VALUE); + testData.assign(INITIAL_SIZE, INITIAL_VALUE); + offset = data.size(); + data.insert(data.begin() + offset, initial_data.begin(), initial_data.begin() + 3); + testData.insert(testData.begin() + offset, initial_data.begin(), initial_data.begin() + 3); + + isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, InsertPositionRangeExcess) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + Data data(INITIAL_SIZE, INITIAL_VALUE); + + size_t offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full_exception); + + offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full_exception); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full_exception); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, PushBack) + { + TestData testData; + Data data; + + for (size_t i = 0; i < SIZE; ++i) + { + testData.push_back(i); + } + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(i); + } + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, PushBackNull) + { + TestData testData; + Data data; + + testData.push_back(1); + + data.push_back(); + data[0] = 1; + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, PushBackExcess) + { + Data data; + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(i); + } + + CHECK_THROW(data.push_back(SIZE), etl::vector_full_exception); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, PopBack) + { + TestData testData(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end()); + + testData.pop_back(); + testData.pop_back(); + + data.pop_back(); + data.pop_back(); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, EraseSingle) + { + TestData testData(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end()); + + testData.erase(testData.begin() + 2); + + data.erase(data.begin() + 2); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, EraseRange) + { + TestData testData(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end()); + + testData.erase(testData.begin() + 2, testData.begin() + 4); + + data.erase(data.begin() + 2, data.begin() + 4); + + bool isEqual = std::equal(data.begin(), + data.end(), + testData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Front) + { + TestData testData(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end()); + + CHECK(data.front()== testData.front()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Back) + { + TestData testData(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end()); + + CHECK(data.back() == testData.back()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Assignment) + { + Data data(initial_data.begin(), initial_data.end()); + Data otherData; + + otherData = data; + + bool isEqual = std::equal(data.begin(), + data.end(), + otherData.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, At) + { + TestData testData(initial_data.begin(), initial_data.end()); + Data data(initial_data.begin(), initial_data.end()); + + + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, AtConst) + { + const TestData testData(initial_data.begin(), initial_data.end()); + const Data data(initial_data.begin(), initial_data.end()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), testData.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds_exception); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Full) + { + Data data; + data.resize(data.max_size()); + + CHECK(data.full()); + CHECK(!data.empty()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Begin) + { + Data data(10); + const Data constData(10); + + CHECK_EQUAL(data.begin(), std::begin(data)); + CHECK_EQUAL(constData.begin(), std::begin(constData)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, End) + { + Data data(10); + const Data constData(10); + + CHECK_EQUAL(data.end(), std::end(data)); + CHECK_EQUAL(constData.end(), std::end(constData)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, Equal) + { + const Data initial1(initial_data.begin(), initial_data.end()); + const Data initial2(initial_data.begin(), initial_data.end()); + + CHECK(initial1 == initial2); + + const Data different(different_data.begin(), different_data.end()); + + CHECK(!(initial1 == different)); + + const Data shorter(shorter_data.begin(), shorter_data.end()); + + CHECK(!(shorter == initial1)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, NotEqual) + { + const Data initial1(initial_data.begin(), initial_data.end()); + const Data initial2(initial_data.begin(), initial_data.end()); + + CHECK(!(initial1 != initial2)); + + const Data different(different_data.begin(), different_data.end()); + + CHECK(initial1 != different); + + const Data shorter(shorter_data.begin(), shorter_data.end()); + + CHECK(shorter != initial1); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, LessThan) + { + const Data less(less_data.begin(), less_data.end()); + const Data initial(initial_data.begin(), initial_data.end()); + + CHECK((less < initial) == (less_data < initial_data)); + + const Data greater(greater_data.begin(), greater_data.end()); + + CHECK((greater < initial) == (greater_data < initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter < initial) == (shorter_data < initial_data)); + CHECK((initial < shorter) == (initial_data < shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, LessThanEqual) + { + const Data less(less_data.begin(), less_data.end()); + const Data initial(initial_data.begin(), initial_data.end()); + + CHECK((less <= initial) == (less_data <= initial_data)); + + const Data greater(greater_data.begin(), greater_data.end()); + + CHECK((greater <= initial) == (greater_data <= initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter <= initial) == (shorter_data <= initial_data)); + CHECK((initial <= shorter) == (initial_data <= shorter_data)); + + const Data initial2(initial_data.begin(), initial_data.end()); + CHECK((initial <= initial2) == (initial_data <= initial_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, GreaterThan) + { + const Data less(less_data.begin(), less_data.end()); + const Data initial(initial_data.begin(), initial_data.end()); + + CHECK((less > initial) == (less_data > initial_data)); + + const Data greater(greater_data.begin(), greater_data.end()); + + CHECK((greater > initial) == (greater_data > initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter > initial) == (shorter_data > initial_data)); + CHECK((initial > shorter) == (initial_data > shorter_data)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, GreaterThanEqual) + { + const Data less(less_data.begin(), less_data.end()); + const Data initial(initial_data.begin(), initial_data.end()); + + CHECK((less >= initial) == (less_data >= initial_data)); + + const Data greater(greater_data.begin(), greater_data.end()); + + CHECK((greater >= initial) == (greater_data >= initial_data)); + + const Data shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter >= initial) == (shorter_data >= initial_data)); + CHECK((initial >= shorter) == (initial_data > shorter_data)); + + const Data initial2(initial_data.begin(), initial_data.end()); + CHECK((initial >= initial2) == (initial_data >= initial_data)); + } + }; +} diff --git a/test/test_visitor.cpp b/test/test_visitor.cpp new file mode 100644 index 00000000..14a70fb5 --- /dev/null +++ b/test/test_visitor.cpp @@ -0,0 +1,719 @@ +/****************************************************************************** +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 "../visitor.h" + +//***************************************************************************** +// Pre-declare the data types. +//***************************************************************************** +class Square; +class Circle; +class Triangle; + +//***************************************************************************** +// What classes do you want the visitors to handle? +// Square will be passed by reference. +// Circle will be passed by reference. +// Triangle will be passed by const reference. +//***************************************************************************** +typedef etl::visitor DrawVisitorType; + +//***************************************************************************** +// What classes do you want the visitors to handle? +// Square will be passed by reference. +// Triangle will be passed by const reference. +//***************************************************************************** +typedef etl::visitor LogVisitorType; + +//***************************************************************************** +// Square accepts draw & log visitors. +//***************************************************************************** +class Square : public etl::visitable +{ +public: + + void accept(DrawVisitorType& visitor) + { + visitor.visit(*this); + } + + void accept(LogVisitorType& visitor) + { + visitor.visit(*this); + } +}; + +//***************************************************************************** +// Circle only accepts draw visitors. +//***************************************************************************** +class Circle : public etl::visitable +{ +public: + + void accept(DrawVisitorType& visitor) + { + visitor.visit(*this); + } +}; + +//***************************************************************************** +// Triangle accepts draw & log visitors. +//***************************************************************************** +class Triangle : public etl::visitable +{ +public: + + void accept(DrawVisitorType& visitor) + { + visitor.visit(*this); + } + + void accept(LogVisitorType& visitor) + { + visitor.visit(*this); + } +}; + + +//***************************************************************************** +// Generic other shapes. +//***************************************************************************** +template +class Shape : public etl::visitable +{ +public: + + void accept(TVisitor& visitor) + { + visitor.visit(*this); + } +}; + +//***************************************************************************** +class DrawVisitor : public DrawVisitorType +{ +public: + + DrawVisitor() + : square_called(false), + circle_called(false), + triangle_called(false) + { + } + + void visit(Square&) + { + square_called = true; + } + + void visit(Circle&) + { + circle_called = true; + } + + void visit(const Triangle&) + { + triangle_called = true; + } + + bool square_called; + bool circle_called; + bool triangle_called; +}; + +//***************************************************************************** +class LogVisitor : public LogVisitorType +{ +public: + + LogVisitor() + : square_called(false), + circle_called(false), + triangle_called(false) + { + } + + void visit(Square&) + { + square_called = true; + } + + // SHOULD NEVER BE CALLED. + void visit(Circle&) + { + circle_called = true; + } + + void visit(const Triangle&) + { + triangle_called = true; + } + + bool square_called; + bool circle_called; + bool triangle_called; +}; + +namespace +{ + SUITE(TestVisitor) + { + //************************************************************************* + TEST(TestTwoVisitorsThreeVisitables) + { + DrawVisitor draw_visitor; + LogVisitor log_visitor; + + Square square; + Circle circle; + Triangle triangle; + + CHECK_EQUAL(false, draw_visitor.square_called); + CHECK_EQUAL(false, draw_visitor.circle_called); + CHECK_EQUAL(false, draw_visitor.triangle_called); + CHECK_EQUAL(false, log_visitor.square_called); + CHECK_EQUAL(false, log_visitor.circle_called); + CHECK_EQUAL(false, log_visitor.triangle_called); + + square.accept(draw_visitor); + square.accept(log_visitor); + + CHECK_EQUAL(true, draw_visitor.square_called); + CHECK_EQUAL(false, draw_visitor.circle_called); + CHECK_EQUAL(false, draw_visitor.triangle_called); + CHECK_EQUAL(true, log_visitor.square_called); + CHECK_EQUAL(false, log_visitor.circle_called); + CHECK_EQUAL(false, log_visitor.triangle_called); + + circle.accept(draw_visitor); + + CHECK_EQUAL(true, draw_visitor.square_called); + CHECK_EQUAL(true, draw_visitor.circle_called); + CHECK_EQUAL(false, draw_visitor.triangle_called); + CHECK_EQUAL(true, log_visitor.square_called); + CHECK_EQUAL(false, log_visitor.circle_called); + CHECK_EQUAL(false, log_visitor.triangle_called); + + triangle.accept(draw_visitor); + triangle.accept(log_visitor); + + CHECK_EQUAL(true, draw_visitor.square_called); + CHECK_EQUAL(true, draw_visitor.circle_called); + CHECK_EQUAL(true, draw_visitor.triangle_called); + CHECK_EQUAL(true, log_visitor.square_called); + CHECK_EQUAL(false, log_visitor.circle_called); + CHECK_EQUAL(true, log_visitor.triangle_called); + } + + //************************************************************************* + TEST(Test1Visitor) + { + class AShape; + class ShapeVisitor1 : public etl::visitor + { + void visit(AShape&) {} + }; + + class AShape : public etl::visitable + { + public: + + void accept(ShapeVisitor1&) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test2Visitor) + { + class AShape; + class ShapeVisitor1 : public etl::visitor + { + void visit(AShape&) {} + }; + + class ShapeVisitor2 : public etl::visitor + { + void visit(AShape&) {} + }; + + class AShape : public etl::visitable + { + public: + + void accept(ShapeVisitor1&) {} + void accept(ShapeVisitor2&) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test3Visitor) + { + class AShape; + class ShapeVisitor1 : public etl::visitor + { + void visit(AShape&) {} + }; + + class ShapeVisitor2 : public etl::visitor + { + void visit(AShape&) {} + }; + + class ShapeVisitor3 : public etl::visitor + { + void visit(AShape&) {} + }; + + class AShape : public etl::visitable + { + public: + + void accept(ShapeVisitor1&) {} + void accept(ShapeVisitor2&) {} + void accept(ShapeVisitor3&) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test4Visitor) + { + class AShape; + class ShapeVisitor1 : public etl::visitor + { + void visit(AShape&) {} + }; + + class ShapeVisitor2 : public etl::visitor + { + void visit(AShape&) {} + }; + + class ShapeVisitor3 : public etl::visitor + { + void visit(AShape&) {} + }; + + class ShapeVisitor4 : public etl::visitor + { + void visit(AShape&) {} + }; + + class AShape : public etl::visitable + { + public: + + void accept(ShapeVisitor1&) {} + void accept(ShapeVisitor2&) {} + void accept(ShapeVisitor3&) {} + void accept(ShapeVisitor4&) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test1Visitable) + { + class ShapeVisitor : public etl::visitor > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test2Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test3Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test4Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test5Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test6Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test7Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test8Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test9Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test10Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor>, Shape<10, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + void visit(Shape<10, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + + //************************************************************************* + TEST(Test11Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor>, Shape<10, ShapeVisitor>, Shape<11, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + void visit(Shape<10, ShapeVisitor>) {} + void visit(Shape<11, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test12Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor>, Shape<10, ShapeVisitor>, Shape<11, ShapeVisitor>, Shape<12, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + void visit(Shape<10, ShapeVisitor>) {} + void visit(Shape<11, ShapeVisitor>) {} + void visit(Shape<12, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test13Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor>, Shape<10, ShapeVisitor>, Shape<11, ShapeVisitor>, Shape<12, ShapeVisitor>, + Shape<13, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + void visit(Shape<10, ShapeVisitor>) {} + void visit(Shape<11, ShapeVisitor>) {} + void visit(Shape<12, ShapeVisitor>) {} + void visit(Shape<13, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test14Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor>, Shape<10, ShapeVisitor>, Shape<11, ShapeVisitor>, Shape<12, ShapeVisitor>, + Shape<13, ShapeVisitor>, Shape<14, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + void visit(Shape<10, ShapeVisitor>) {} + void visit(Shape<11, ShapeVisitor>) {} + void visit(Shape<12, ShapeVisitor>) {} + void visit(Shape<13, ShapeVisitor>) {} + void visit(Shape<14, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test15Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor>, Shape<10, ShapeVisitor>, Shape<11, ShapeVisitor>, Shape<12, ShapeVisitor>, + Shape<13, ShapeVisitor>, Shape<14, ShapeVisitor>, Shape<15, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + void visit(Shape<10, ShapeVisitor>) {} + void visit(Shape<11, ShapeVisitor>) {} + void visit(Shape<12, ShapeVisitor>) {} + void visit(Shape<13, ShapeVisitor>) {} + void visit(Shape<14, ShapeVisitor>) {} + void visit(Shape<15, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + + //************************************************************************* + TEST(Test16Visitables) + { + class ShapeVisitor : public etl::visitor , Shape<2, ShapeVisitor>, Shape<3, ShapeVisitor>, Shape<4, ShapeVisitor>, + Shape<5, ShapeVisitor>, Shape<6, ShapeVisitor>, Shape<7, ShapeVisitor>, Shape<8, ShapeVisitor>, + Shape<9, ShapeVisitor>, Shape<10, ShapeVisitor>, Shape<11, ShapeVisitor>, Shape<12, ShapeVisitor>, + Shape<13, ShapeVisitor>, Shape<14, ShapeVisitor>, Shape<15, ShapeVisitor>, Shape<16, ShapeVisitor> > + { + public: + + void visit(Shape<1, ShapeVisitor>) {} + void visit(Shape<2, ShapeVisitor>) {} + void visit(Shape<3, ShapeVisitor>) {} + void visit(Shape<4, ShapeVisitor>) {} + void visit(Shape<5, ShapeVisitor>) {} + void visit(Shape<6, ShapeVisitor>) {} + void visit(Shape<7, ShapeVisitor>) {} + void visit(Shape<8, ShapeVisitor>) {} + void visit(Shape<9, ShapeVisitor>) {} + void visit(Shape<10, ShapeVisitor>) {} + void visit(Shape<11, ShapeVisitor>) {} + void visit(Shape<12, ShapeVisitor>) {} + void visit(Shape<13, ShapeVisitor>) {} + void visit(Shape<14, ShapeVisitor>) {} + void visit(Shape<15, ShapeVisitor>) {} + void visit(Shape<16, ShapeVisitor>) {} + }; + + // This test just needs to compile without errors. + CHECK(true); + } + } +} +