From f105ca0c4854c4925b330509050210198a424fd0 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Mon, 8 Dec 2014 20:20:25 +0000 Subject: [PATCH] Added new unit tests --- test/test_algorithm.cpp | 314 ++++++++++++++++++++++++++++++++++++++++ test/test_alignment.cpp | 165 +++++++++++++++++++++ 2 files changed, 479 insertions(+) create mode 100644 test/test_algorithm.cpp create mode 100644 test/test_alignment.cpp diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp new file mode 100644 index 00000000..0afb05fb --- /dev/null +++ b/test/test_algorithm.cpp @@ -0,0 +1,314 @@ +/****************************************************************************** +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 "../algorithm.h" +#include "../container.h" + +#include +#include +#include +#include + +namespace +{ + typedef std::vector Data; + Data data = { 2, 1, 4, 3, 6, 5, 8, 7, 10, 9 }; + + SUITE(test_algorithm) + { + //========================================================================= + TEST(minmax_element) + { + std::pair expected = std::minmax_element(data.begin(), data.end()); + std::pair result = etl::minmax_element(data.begin(), data.end()); + CHECK_EQUAL(std::distance(data.begin(), expected.first), std::distance(data.begin(), result.first)); + CHECK_EQUAL(std::distance(data.begin(), expected.second), std::distance(data.begin(), result.second)); + } + + //========================================================================= + TEST(minmax_element_compare) + { + std::pair expected = std::minmax_element(data.begin(), data.end(), std::greater()); + std::pair result = etl::minmax_element(data.begin(), data.end(), std::greater()); + CHECK_EQUAL(std::distance(data.begin(), expected.first), std::distance(data.begin(), result.first)); + CHECK_EQUAL(std::distance(data.begin(), expected.second), std::distance(data.begin(), result.second)); + } + + //========================================================================= + TEST(minmax) + { + int a = 1; + int b = 2; + + std::pair expected = std::minmax(a, b); + std::pair result = etl::minmax(a, b); + CHECK_EQUAL(expected.first, result.first); + CHECK_EQUAL(expected.second, result.second); + + result = etl::minmax(b, a); + expected = std::minmax(b, a); + CHECK_EQUAL(expected.first, result.first); + CHECK_EQUAL(expected.second, result.second); + } + + //========================================================================= + TEST(minmax_compare) + { + int a = 1; + int b = 2; + + std::pair expected = std::minmax(a, b, std::greater()); + std::pair result = etl::minmax(a, b, std::greater()); + CHECK_EQUAL(expected.first, result.first); + CHECK_EQUAL(expected.second, result.second); + + result = etl::minmax(b, a, std::greater()); + expected = std::minmax(b, a, std::greater()); + CHECK_EQUAL(expected.first, result.first); + CHECK_EQUAL(expected.second, result.second); + } + + //========================================================================= + TEST(is_sorted_until) + { + int data[] = { 1, 2, 3, 4, 6, 5, 7, 8, 9, 10 }; + + int* p1 = std::is_sorted_until(std::begin(data), std::end(data)); + int* p2 = etl::is_sorted_until(std::begin(data), std::end(data)); + CHECK_EQUAL(std::distance(std::begin(data), p1), std::distance(std::begin(data), p2)); + } + + //========================================================================= + TEST(is_sorted_until_compare) + { + int data[] = { 10, 9, 8, 7, 5, 6, 4, 3, 4, 2, 1 }; + + int* p1 = std::is_sorted_until(std::begin(data), std::end(data), std::greater()); + int* p2 = etl::is_sorted_until(std::begin(data), std::end(data), std::greater()); + CHECK_EQUAL(std::distance(etl::begin(data), p1), std::distance(std::begin(data), p2)); + } + + //========================================================================= + TEST(is_sorted) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + bool is_sorted = etl::is_sorted(std::begin(data1), std::end(data1)); + CHECK(is_sorted); + + int data2[] = { 1, 2, 3, 4, 6, 5, 7, 8 , 9, 10}; + + is_sorted = etl::is_sorted(std::begin(data2), std::end(data2)); + CHECK(!is_sorted); + } + + //========================================================================= + TEST(is_sorted_compare) + { + int data1[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; + + bool is_sorted = etl::is_sorted(std::begin(data1), std::end(data1), std::greater()); + CHECK(is_sorted); + + int data2[] = { 10, 9, 8, 7, 5, 6, 4, 3, 2, 1 }; + + is_sorted = etl::is_sorted(std::begin(data2), std::end(data2), std::greater()); + CHECK(!is_sorted); + } + + //========================================================================= + TEST(copy_n) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int data2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + int data3[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + std::copy_n(std::begin(data1), 4, std::begin(data2)); + etl::copy_n(std::begin(data1), 4, std::begin(data3)); + + bool is_same = std::equal(std::begin(data2), std::end(data2), std::begin(data3)); + CHECK(is_same); + } + + //========================================================================= + TEST(copy_if) + { + int data1[] = { 1, 8, 2, 7, 3, 6, 4, 5, 10, 9 }; + int data2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + int data3[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + // Copy everything less than 5. + std::copy_if(std::begin(data1), std::end(data1), std::begin(data2), std::bind2nd(std::less(), 5)); + etl::copy_if(std::begin(data1), std::end(data1), std::begin(data3), std::bind2nd(std::less(), 5)); + + bool is_same = std::equal(std::begin(data2), std::end(data2), std::begin(data3)); + CHECK(is_same); + } + + //========================================================================= + TEST(any_of) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + bool expected = std::any_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + bool result = etl::any_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + CHECK_EQUAL(expected, result); + + expected = std::any_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 0)); + result = etl::any_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 0)); + CHECK_EQUAL(expected, result); + } + + //========================================================================= + TEST(all_of) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + bool expected = std::all_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 0)); + bool result = etl::all_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 0)); + CHECK_EQUAL(expected, result); + + expected = std::all_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + result = etl::all_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + CHECK_EQUAL(expected, result); + } + + //========================================================================= + TEST(none_of) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + bool expected = std::none_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 8)); + bool result = etl::none_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 8)); + CHECK_EQUAL(expected, result); + + expected = std::none_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + result = etl::none_of(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + CHECK_EQUAL(expected, result); + } + + struct Compare : public std::binary_function < int, int, bool > + { + bool operator()(int a, int b) const + { + return a == b; + } + }; + + //========================================================================= + TEST(is_permutation) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int permutation[] = { 1, 3, 2, 4, 7, 6, 5, 8 }; + int not_permutation[] = { 1, 2, 3, 4, 5, 6, 7, 7 }; + + bool is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(permutation)); + CHECK(is_permutation); + + is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(not_permutation)); + CHECK(!is_permutation); + + is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(permutation), std::equal_to()); + CHECK(is_permutation); + + is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(not_permutation), std::equal_to()); + CHECK(!is_permutation); + + is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(permutation), std::end(permutation)); + CHECK(is_permutation); + + is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(not_permutation), std::end(not_permutation)); + CHECK(!is_permutation); + + is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(permutation), std::end(permutation), std::equal_to()); + CHECK(is_permutation); + + is_permutation = etl::is_permutation(std::begin(data1), std::end(data1), std::begin(not_permutation), std::end(not_permutation), std::equal_to()); + CHECK(!is_permutation); + } + + //========================================================================= + TEST(is_partitioned) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + + bool expected = std::is_partitioned(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + bool result = etl::is_partitioned(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + CHECK_EQUAL(expected, result); + + std::partition(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + + expected = std::is_partitioned(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + result = etl::is_partitioned(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + CHECK_EQUAL(expected, result); + } + + //========================================================================= + TEST(partition_point) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + + std::partition(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + + int* partition1 = std::partition_point(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + int* partition2 = etl::partition_point(std::begin(data1), std::end(data1), std::bind2nd(std::greater(), 4)); + CHECK_EQUAL(std::distance(std::begin(data1), partition1), std::distance(std::begin(data1), partition2)); + } + + //========================================================================= + TEST(partition_copy) + { + int data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int data2[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + int data3[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + int data4[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + int data5[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + + std::partition_copy(std::begin(data1), std::end(data1), std::begin(data2), std::begin(data3), std::bind2nd(std::greater(), 4)); + etl::partition_copy(std::begin(data1), std::end(data1), std::begin(data4), std::begin(data5), std::bind2nd(std::greater(), 4)); + + bool are_equal; + + are_equal = std::equal(std::begin(data2), std::end(data2), std::begin(data4)); + CHECK(are_equal); + + are_equal = std::equal(std::begin(data3), std::end(data3), std::begin(data5)); + CHECK(are_equal); + } + + //========================================================================= + TEST(find_if_not) + { + int data1[] = { 1, 2, 3, 5, 6, 7, 8 }; + + // Find the element not less than 4. + int* p = std::find_if_not(std::begin(data1), std::end(data1), std::bind2nd(std::less(), 4)); + CHECK_EQUAL(5, *p); + } + }; +} diff --git a/test/test_alignment.cpp b/test/test_alignment.cpp new file mode 100644 index 00000000..791c82c4 --- /dev/null +++ b/test/test_alignment.cpp @@ -0,0 +1,165 @@ +/****************************************************************************** +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 "../alignment.h" +#include "../type_traits.h" + +#include + +namespace +{ + struct Data + { + int i; + char c; + short s[3]; + }; + + SUITE(test_alignment) + { + //************************************************************************* + TEST(test_align_at) + { + size_t alignment; + + // 1 + typedef etl::align_at align_char_1; + alignment = etl::alignment_of::value; + CHECK_EQUAL(1, alignment); + + // 2 + typedef etl::align_at align_char_2; + alignment = etl::alignment_of::value; + CHECK_EQUAL(2, alignment); + + align_char_2 data2[2]; + CHECK((size_t(&data2[0]) % 2) == 0); + CHECK((size_t(&data2[1]) % 2) == 0); + + // 4 + typedef etl::align_at align_char_4; + alignment = etl::alignment_of::value; + CHECK_EQUAL(4, alignment); + + align_char_4 data4[2]; + CHECK((size_t(&data4[0]) % 4) == 0); + CHECK((size_t(&data4[1]) % 4) == 0); + + // 8 + typedef etl::align_at align_char_8; + alignment = etl::alignment_of::value; + CHECK_EQUAL(8, alignment); + + align_char_8 data8[2]; + CHECK((size_t(&data8[0]) % 8) == 0); + CHECK((size_t(&data8[1]) % 8) == 0); + + // 16 + typedef etl::align_at align_char_16; + alignment = etl::alignment_of::value; + CHECK_EQUAL(16, alignment); + + align_char_16 data16[2]; + CHECK((size_t(&data16[0]) % 16) == 0); + CHECK((size_t(&data16[1]) % 16) == 0); + } + + //************************************************************************* + TEST(test_align_as) + { + size_t alignment; + size_t expected; + + // char + typedef etl::align_as align_char_char; + alignment = etl::alignment_of::value; + expected = std::alignment_of::value; + CHECK_EQUAL(expected, alignment); + + // short + typedef etl::align_as align_char_short; + alignment = etl::alignment_of::value; + expected = std::alignment_of::value; + CHECK_EQUAL(expected, alignment); + + align_char_short data_short[2]; + CHECK((size_t(&data_short[0].value) % expected) == 0); + CHECK((size_t(&data_short[1].value) % expected) == 0); + + // int + typedef etl::align_as align_char_int; + alignment = etl::alignment_of::value; + expected = std::alignment_of::value; + CHECK_EQUAL(expected, alignment); + + align_char_int data_int[2]; + CHECK((size_t(&data_int[0].value) % expected) == 0); + CHECK((size_t(&data_int[1].value) % expected) == 0); + + // long + typedef etl::align_as align_char_long; + alignment = etl::alignment_of::value; + expected = std::alignment_of::value; + CHECK_EQUAL(expected, alignment); + + align_char_long data_long[2]; + CHECK((size_t(&data_long[0].value) % expected) == 0); + CHECK((size_t(&data_long[1].value) % expected) == 0); + + // long long + typedef etl::align_as align_char_long_long; + alignment = etl::alignment_of::value; + expected = std::alignment_of::value; + CHECK_EQUAL(expected, alignment); + + align_char_long_long data_long_long[2]; + CHECK((size_t(&data_long_long[0].value) % expected) == 0); + CHECK((size_t(&data_long_long[1].value) % expected) == 0); + + // double + typedef etl::align_as align_char_double; + alignment = etl::alignment_of::value; + expected = std::alignment_of::value; + CHECK_EQUAL(expected, alignment); + + align_char_double data_double[2]; + CHECK((size_t(&data_double[0].value) % expected) == 0); + CHECK((size_t(&data_double[1].value) % expected) == 0); + + // struct Data + typedef etl::align_as align_char_data; + alignment = etl::alignment_of::value; + expected = std::alignment_of::value; + CHECK_EQUAL(expected, alignment); + + align_char_data data_data[2]; + CHECK((size_t(&data_data[0].value) % expected) == 0); + CHECK((size_t(&data_data[1].value) % expected) == 0); + } + }; +}