|
| 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 "iterator_test.hpp" |
| 11 | + |
| 12 | +#include <boost/config.hpp> |
| 13 | + |
| 14 | +#define BOOST_TEST_MAIN |
| 15 | +#include <boost/test/unit_test.hpp> |
| 16 | + |
| 17 | +#include <boost/algorithm/cxx14/equal.hpp> |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | +#include <array> |
| 21 | +#include <functional> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +typedef int data_t; |
| 25 | + |
| 26 | +static const int NUM_ELEMENTS = 4; |
| 27 | + |
| 28 | +BOOST_CONSTEXPR data_t add_one(const data_t value) { return value + 1; } |
| 29 | + |
| 30 | +BOOST_CONSTEXPR data_t add_two(const data_t value) { return value + 2; } |
| 31 | + |
| 32 | +void test_iterate() { |
| 33 | + { // iota |
| 34 | + std::vector<data_t> actual(NUM_ELEMENTS); |
| 35 | + |
| 36 | + boost::algorithm::iterate(actual.begin(), actual.end(), 15, add_one); |
| 37 | + |
| 38 | + std::vector<data_t> expected(NUM_ELEMENTS); |
| 39 | + expected.at(0) = 15; |
| 40 | + expected.at(1) = 16; |
| 41 | + expected.at(2) = 17; |
| 42 | + expected.at(3) = 18; |
| 43 | + BOOST_CHECK(actual.size() == expected.size()); |
| 44 | + BOOST_CHECK(std::equal(actual.begin(), actual.end(), expected.begin())); |
| 45 | + } |
| 46 | + { // striding |
| 47 | + std::vector<data_t> actual(NUM_ELEMENTS); |
| 48 | + |
| 49 | + boost::algorithm::iterate(actual.begin(), actual.end(), 15, add_two); |
| 50 | + |
| 51 | + std::vector<data_t> expected(NUM_ELEMENTS); |
| 52 | + expected.at(0) = 15; |
| 53 | + expected.at(1) = 17; |
| 54 | + expected.at(2) = 19; |
| 55 | + expected.at(3) = 21; |
| 56 | + BOOST_CHECK(std::equal(actual.begin(), actual.end(), expected.begin())); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void test_iterate_range() { |
| 61 | + { // iota |
| 62 | + std::vector<data_t> actual(NUM_ELEMENTS); |
| 63 | + |
| 64 | + boost::algorithm::iterate(actual, 15, add_one); |
| 65 | + |
| 66 | + std::vector<data_t> expected(NUM_ELEMENTS); |
| 67 | + expected.at(0) = 15; |
| 68 | + expected.at(1) = 16; |
| 69 | + expected.at(2) = 17; |
| 70 | + expected.at(3) = 18; |
| 71 | + BOOST_CHECK(actual.size() == expected.size()); |
| 72 | + BOOST_CHECK(std::equal(actual.begin(), actual.end(), expected.begin())); |
| 73 | + } |
| 74 | + { // striding |
| 75 | + std::vector<data_t> actual(NUM_ELEMENTS); |
| 76 | + |
| 77 | + boost::algorithm::iterate(actual, 15, add_two); |
| 78 | + |
| 79 | + std::vector<data_t> expected(NUM_ELEMENTS); |
| 80 | + expected.at(0) = 15; |
| 81 | + expected.at(1) = 17; |
| 82 | + expected.at(2) = 19; |
| 83 | + expected.at(3) = 21; |
| 84 | + BOOST_CHECK(std::equal(actual.begin(), actual.end(), expected.begin())); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void test_iterate_n() { |
| 89 | + { // iota |
| 90 | + std::vector<data_t> actual(NUM_ELEMENTS); |
| 91 | + |
| 92 | + boost::algorithm::iterate(actual.begin(), actual.size(), 15, add_one); |
| 93 | + |
| 94 | + std::vector<data_t> expected(NUM_ELEMENTS); |
| 95 | + expected.at(0) = 15; |
| 96 | + expected.at(1) = 16; |
| 97 | + expected.at(2) = 17; |
| 98 | + expected.at(3) = 18; |
| 99 | + BOOST_CHECK(actual.size() == expected.size()); |
| 100 | + BOOST_CHECK(std::equal(actual.begin(), actual.end(), expected.begin())); |
| 101 | + } |
| 102 | + { // striding |
| 103 | + std::vector<data_t> actual(NUM_ELEMENTS); |
| 104 | + |
| 105 | + boost::algorithm::iterate(actual.begin(), actual.size(), 15, add_two); |
| 106 | + |
| 107 | + std::vector<data_t> expected(NUM_ELEMENTS); |
| 108 | + expected.at(0) = 15; |
| 109 | + expected.at(1) = 17; |
| 110 | + expected.at(2) = 19; |
| 111 | + expected.at(3) = 21; |
| 112 | + BOOST_CHECK(actual.size() == expected.size()); |
| 113 | + BOOST_CHECK(std::equal(actual.begin(), actual.end(), expected.begin())); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +BOOST_CXX14_CONSTEXPR inline bool constexpr_test_iterate() { |
| 118 | + int actual[] = {0, 0, 0, 0}; |
| 119 | + |
| 120 | + boost::algorithm::iterate(input_iterator<int *>(actual), |
| 121 | + input_iterator<int *>(actual + NUM_ELEMENTS), 15, |
| 122 | + add_one); |
| 123 | + |
| 124 | + int expected[] = {15, 16, 17, 18}; |
| 125 | + return boost::algorithm::equal( |
| 126 | + input_iterator<int *>(actual), |
| 127 | + input_iterator<int *>(actual + NUM_ELEMENTS), |
| 128 | + input_iterator<int *>(expected), |
| 129 | + input_iterator<int *>(expected + NUM_ELEMENTS)); |
| 130 | +} |
| 131 | + |
| 132 | +BOOST_CXX14_CONSTEXPR inline bool constexpr_test_iterate_n() { |
| 133 | + int actual[] = {0, 0, 0, 0}; |
| 134 | + |
| 135 | + boost::algorithm::iterate(input_iterator<int *>(actual), NUM_ELEMENTS, 15, |
| 136 | + add_one); |
| 137 | + |
| 138 | + int expected[] = {15, 16, 17, 18}; |
| 139 | + return boost::algorithm::equal( |
| 140 | + input_iterator<int *>(actual), |
| 141 | + input_iterator<int *>(actual + NUM_ELEMENTS), |
| 142 | + input_iterator<int *>(expected), |
| 143 | + input_iterator<int *>(expected + NUM_ELEMENTS)); |
| 144 | +} |
| 145 | + |
| 146 | +BOOST_AUTO_TEST_CASE(test_main) { |
| 147 | + test_iterate(); |
| 148 | + test_iterate_range(); |
| 149 | + test_iterate_n(); |
| 150 | + |
| 151 | + { |
| 152 | + BOOST_CXX14_CONSTEXPR bool result = constexpr_test_iterate(); |
| 153 | + BOOST_CHECK(result); |
| 154 | + } |
| 155 | + |
| 156 | + { |
| 157 | + BOOST_CXX14_CONSTEXPR bool result = constexpr_test_iterate_n(); |
| 158 | + BOOST_CHECK(result); |
| 159 | + } |
| 160 | +} |
0 commit comments