|
| 1 | +/* |
| 2 | + Copyright (c) Jonathan Gopel 2022. |
| 3 | +
|
| 4 | + Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <boost/algorithm/iterate.hpp> |
| 9 | + |
| 10 | +#include <boost/config.hpp> |
| 11 | + |
| 12 | +#define BOOST_TEST_MAIN |
| 13 | +#include <boost/test/unit_test.hpp> |
| 14 | + |
| 15 | +#include <boost/algorithm/cxx14/equal.hpp> |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <array> |
| 19 | +#include <functional> |
| 20 | + |
| 21 | +using data_t = int; |
| 22 | + |
| 23 | +static const int NUM_ELEMENTS = 4; |
| 24 | + |
| 25 | +void test_iterate() { |
| 26 | + { // iota |
| 27 | + std::array<data_t, NUM_ELEMENTS> actual{}; |
| 28 | + |
| 29 | + boost::algorithm::iterate( |
| 30 | + std::begin(actual), std::end(actual), 15, |
| 31 | + [](const data_t value) -> data_t { return value + 1; }); |
| 32 | + |
| 33 | + std::array<data_t, NUM_ELEMENTS> expected{15, 16, 17, 18}; |
| 34 | + BOOST_CHECK(std::size(actual) == std::size(expected)); |
| 35 | + BOOST_CHECK( |
| 36 | + std::equal(std::begin(actual), std::end(actual), std::begin(expected))); |
| 37 | + } |
| 38 | + { // striding |
| 39 | + std::array<data_t, NUM_ELEMENTS> actual{}; |
| 40 | + |
| 41 | + boost::algorithm::iterate( |
| 42 | + std::begin(actual), std::end(actual), 15, |
| 43 | + [](const data_t value) -> data_t { return value + 2; }); |
| 44 | + |
| 45 | + std::array<data_t, NUM_ELEMENTS> expected{15, 17, 19, 21}; |
| 46 | + BOOST_CHECK(std::size(actual) == std::size(expected)); |
| 47 | + BOOST_CHECK( |
| 48 | + std::equal(std::begin(actual), std::end(actual), std::begin(expected))); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void test_iterate_n() { |
| 53 | + { // iota |
| 54 | + std::array<data_t, NUM_ELEMENTS> actual{}; |
| 55 | + |
| 56 | + boost::algorithm::iterate( |
| 57 | + std::begin(actual), std::size(actual), 15, |
| 58 | + [](const data_t value) -> data_t { return value + 1; }); |
| 59 | + |
| 60 | + std::array<data_t, NUM_ELEMENTS> expected{15, 16, 17, 18}; |
| 61 | + BOOST_CHECK(std::size(actual) == std::size(expected)); |
| 62 | + BOOST_CHECK( |
| 63 | + std::equal(std::begin(actual), std::end(actual), std::begin(expected))); |
| 64 | + } |
| 65 | + { // striding |
| 66 | + std::array<data_t, NUM_ELEMENTS> actual{}; |
| 67 | + |
| 68 | + boost::algorithm::iterate( |
| 69 | + std::begin(actual), std::size(actual), 15, |
| 70 | + [](const data_t value) -> data_t { return value + 2; }); |
| 71 | + |
| 72 | + std::array<data_t, NUM_ELEMENTS> expected{15, 17, 19, 21}; |
| 73 | + BOOST_CHECK(std::size(actual) == std::size(expected)); |
| 74 | + BOOST_CHECK( |
| 75 | + std::equal(std::begin(actual), std::end(actual), std::begin(expected))); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +BOOST_CXX14_CONSTEXPR inline bool constexpr_test_iterate() { |
| 80 | + std::array<data_t, NUM_ELEMENTS> actual{}; |
| 81 | + |
| 82 | + boost::algorithm::iterate( |
| 83 | + std::begin(actual), std::end(actual), 15, |
| 84 | + [](const data_t value) -> data_t { return value + 1; }); |
| 85 | + |
| 86 | + std::array<data_t, NUM_ELEMENTS> expected{15, 16, 17, 18}; |
| 87 | + return (std::size(actual) == std::size(expected)) and |
| 88 | + boost::algorithm::equal(std::begin(actual), std::end(actual), |
| 89 | + std::begin(expected), std::end(expected)); |
| 90 | +} |
| 91 | + |
| 92 | +BOOST_CXX14_CONSTEXPR inline bool constexpr_test_iterate_n() { |
| 93 | + std::array<data_t, NUM_ELEMENTS> actual{}; |
| 94 | + |
| 95 | + boost::algorithm::iterate( |
| 96 | + std::begin(actual), std::size(actual), 15, |
| 97 | + [](const data_t value) -> data_t { return value + 1; }); |
| 98 | + |
| 99 | + std::array<data_t, NUM_ELEMENTS> expected{15, 16, 17, 18}; |
| 100 | + return (std::size(actual) == std::size(expected)) and |
| 101 | + boost::algorithm::equal(std::begin(actual), std::end(actual), |
| 102 | + std::begin(expected), std::end(expected)); |
| 103 | +} |
| 104 | + |
| 105 | +BOOST_AUTO_TEST_CASE(test_main) { |
| 106 | + test_iterate(); |
| 107 | + test_iterate_n(); |
| 108 | + |
| 109 | + { |
| 110 | + BOOST_CXX14_CONSTEXPR bool result = constexpr_test_iterate(); |
| 111 | + BOOST_CHECK(result); |
| 112 | + } |
| 113 | + |
| 114 | + { |
| 115 | + BOOST_CXX14_CONSTEXPR bool result = constexpr_test_iterate_n(); |
| 116 | + BOOST_CHECK(result); |
| 117 | + } |
| 118 | +} |
0 commit comments