diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 813be2c..942294d 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -85,7 +85,9 @@ alias unit_test_framework [ run is_partitioned_until_test.cpp unit_test_framework : : : : is_partitioned_until_test ] # Apply_permutation tests - [ run apply_permutation_test.cpp unit_test_framework : : : : apply_permutation_test ] + [ run apply_permutation_test.cpp unit_test_framework : : : : apply_permutation_test ] +# Find tests + [ run find_not_test.cpp unit_test_framework : : : : find_not_test ] ; } diff --git a/test/find_not_test.cpp b/test/find_not_test.cpp new file mode 100644 index 0000000..40b0b71 --- /dev/null +++ b/test/find_not_test.cpp @@ -0,0 +1,109 @@ +/* + Copyright (c) T. Zachary Laine 2018. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ +#include + +#include + +#define BOOST_TEST_MAIN +#include + +#include +#include + + +namespace ba = boost::algorithm; + +template +struct dist_t +{ + dist_t(Container & cont) : cont_(cont) {} + template + std::ptrdiff_t operator()(Iter it) const + { + return std::distance(cont_.begin(), it); + } + + Container & cont_; +}; + +BOOST_CXX14_CONSTEXPR bool check_constexpr() +{ + int in_data[] = {2, 2, 3, 4, 5}; + bool res = true; + + const int* from = in_data; + const int* to = in_data + 5; + + const int* start = ba::find_not(from, to, 1); // stops on first + res = (res && start == from); + + int in_data_2[] = {6, 6, 6, 6, 6}; + const int* end = ba::find_not(in_data_2, in_data_2 + 5, 6); // stops on the end + res = (res && end == in_data_2 + 5); + + const int* three = ba::find_not(from, to, 2); // stops on third element + res = (res && three == in_data + 2); + + return res; +} + +void test_sequence() +{ + { + std::vector v1; + dist_t > const dist(v1); + + for (int i = 5; i < 15; ++i) + v1.push_back(i); + BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0); + BOOST_CHECK_EQUAL( + dist(ba::find_not(v1.begin(), v1.end(), v1.back())), 0); + BOOST_CHECK_EQUAL( + dist(ba::find_not(v1.begin(), v1.end(), v1.front())), 1); + + v1 = std::vector(10, 2); + BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0); + BOOST_CHECK_EQUAL( + dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size()); + BOOST_CHECK_EQUAL( + dist(ba::find_not(v1.begin(), v1.end(), v1.front())), v1.size()); + } + + // With bidirectional iterators. + { + std::list l1; + dist_t > const dist(l1); + + for (int i = 5; i < 15; ++i) + l1.push_back(i); + BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0); + BOOST_CHECK_EQUAL( + dist(ba::find_not(l1.begin(), l1.end(), l1.back())), 0); + BOOST_CHECK_EQUAL( + dist(ba::find_not(l1.begin(), l1.end(), l1.front())), 1); + + l1.clear(); + for (int i = 0; i < 10; ++i) + l1.push_back(2); + BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0); + BOOST_CHECK_EQUAL( + dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size()); + BOOST_CHECK_EQUAL( + dist(ba::find_not(l1.begin(), l1.end(), l1.front())), l1.size()); + } + + BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr(); + BOOST_CHECK(ce_result); +} + + +BOOST_AUTO_TEST_CASE(test_main) +{ + test_sequence(); +}