| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <__bits/test/tests.hpp>
|
|---|
| 8 | #include <algorithm>
|
|---|
| 9 | #include <array>
|
|---|
| 10 | #include <string>
|
|---|
| 11 | #include <utility>
|
|---|
| 12 |
|
|---|
| 13 | namespace std::test
|
|---|
| 14 | {
|
|---|
| 15 | bool algorithm_test::run(bool report)
|
|---|
| 16 | {
|
|---|
| 17 | report_ = report;
|
|---|
| 18 | start();
|
|---|
| 19 |
|
|---|
| 20 | test_non_modifying();
|
|---|
| 21 | test_mutating();
|
|---|
| 22 |
|
|---|
| 23 | return end();
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | const char* algorithm_test::name()
|
|---|
| 27 | {
|
|---|
| 28 | return "algorithm";
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | void algorithm_test::test_non_modifying()
|
|---|
| 32 | {
|
|---|
| 33 | auto data1 = {1, 2, 3, 4, 5};
|
|---|
| 34 | auto res1 = std::all_of(
|
|---|
| 35 | data1.begin(), data1.end(),
|
|---|
| 36 | [](auto x){ return x > 0; }
|
|---|
| 37 | );
|
|---|
| 38 | auto res2 = std::all_of(
|
|---|
| 39 | data1.begin(), data1.end(),
|
|---|
| 40 | [](auto x){ return x < 4; }
|
|---|
| 41 | );
|
|---|
| 42 |
|
|---|
| 43 | test("all_of pt1", res1);
|
|---|
| 44 | test("all_of pt2", !res2);
|
|---|
| 45 |
|
|---|
| 46 | auto res3 = std::any_of(
|
|---|
| 47 | data1.begin(), data1.end(),
|
|---|
| 48 | [](auto x){ return x > 4; }
|
|---|
| 49 | );
|
|---|
| 50 | auto res4 = std::any_of(
|
|---|
| 51 | data1.begin(), data1.end(),
|
|---|
| 52 | [](auto x){ return x == 10; }
|
|---|
| 53 | );
|
|---|
| 54 |
|
|---|
| 55 | test("any_of pt1", res3);
|
|---|
| 56 | test("any_of pt2", !res4);
|
|---|
| 57 |
|
|---|
| 58 | auto res5 = std::none_of(
|
|---|
| 59 | data1.begin(), data1.end(),
|
|---|
| 60 | [](auto x){ return x < 0; }
|
|---|
| 61 | );
|
|---|
| 62 | auto res6 = std::none_of(
|
|---|
| 63 | data1.begin(), data1.end(),
|
|---|
| 64 | [](auto x){ return x == 4; }
|
|---|
| 65 | );
|
|---|
| 66 |
|
|---|
| 67 | test("none_of pt1", res5);
|
|---|
| 68 | test("none_of pt2", !res6);
|
|---|
| 69 |
|
|---|
| 70 | std::array<int, 5> data2{1, 2, 3, 4, 5};
|
|---|
| 71 | auto check1 = {1, 20, 3, 40, 5};
|
|---|
| 72 | std::for_each(
|
|---|
| 73 | data2.begin(), data2.end(),
|
|---|
| 74 | [](auto& x){
|
|---|
| 75 | if (x % 2 == 0)
|
|---|
| 76 | x *= 10;
|
|---|
| 77 | }
|
|---|
| 78 | );
|
|---|
| 79 |
|
|---|
| 80 | test_eq(
|
|---|
| 81 | "for_each", check1.begin(), check1.end(),
|
|---|
| 82 | data2.begin(), data2.end()
|
|---|
| 83 | );
|
|---|
| 84 |
|
|---|
| 85 | auto res7 = std::find(data2.begin(), data2.end(), 40);
|
|---|
| 86 | test_eq("find", res7, &data2[3]);
|
|---|
| 87 |
|
|---|
| 88 | auto res8 = std::find_if(
|
|---|
| 89 | data2.begin(), data2.end(),
|
|---|
| 90 | [](auto x){ return x > 30; }
|
|---|
| 91 | );
|
|---|
| 92 | test_eq("find_if", res8, &data2[3]);
|
|---|
| 93 |
|
|---|
| 94 | auto res9 = std::find_if_not(
|
|---|
| 95 | data2.begin(), data2.end(),
|
|---|
| 96 | [](auto x){ return x < 30; }
|
|---|
| 97 | );
|
|---|
| 98 | test_eq("find_if_not", res9, &data2[3]);
|
|---|
| 99 |
|
|---|
| 100 | // TODO: find_end, find_first_of
|
|---|
| 101 |
|
|---|
| 102 | std::array<int, 7> data3{1, 2, 3, 3, 4, 6, 5};
|
|---|
| 103 | auto res10 = std::adjacent_find(data3.begin(), data3.end());
|
|---|
| 104 | test_eq("adjacent_find pt1", res10, &data3[2]);
|
|---|
| 105 |
|
|---|
| 106 | auto res11 = std::adjacent_find(
|
|---|
| 107 | data3.begin(), data3.end(),
|
|---|
| 108 | [](auto lhs, auto rhs){
|
|---|
| 109 | return rhs < lhs;
|
|---|
| 110 | }
|
|---|
| 111 | );
|
|---|
| 112 | test_eq("adjacent_find pt2", res11, &data3[5]);
|
|---|
| 113 |
|
|---|
| 114 | auto res12 = std::count(data3.begin(), data3.end(), 3);
|
|---|
| 115 | test_eq("count", res12, 2);
|
|---|
| 116 |
|
|---|
| 117 | auto res13 = std::count_if(
|
|---|
| 118 | data3.begin(), data3.end(),
|
|---|
| 119 | [](auto x){
|
|---|
| 120 | return x % 2 == 1;
|
|---|
| 121 | }
|
|---|
| 122 | );
|
|---|
| 123 | test_eq("count_if", res13, 4);
|
|---|
| 124 |
|
|---|
| 125 | std::array<int, 6> data4{1, 2, 3, 4, 5, 6};
|
|---|
| 126 | std::array<int, 6> data5{1, 2, 3, 4, 6, 5};
|
|---|
| 127 | auto res14 = std::mismatch(
|
|---|
| 128 | data4.begin(), data4.end(),
|
|---|
| 129 | data5.begin(), data5.end()
|
|---|
| 130 | );
|
|---|
| 131 |
|
|---|
| 132 | test_eq("mismatch pt1", res14.first, &data4[4]);
|
|---|
| 133 | test_eq("mismatch pt2", res14.second, &data5[4]);
|
|---|
| 134 |
|
|---|
| 135 | auto res15 = std::equal(
|
|---|
| 136 | data4.begin(), data4.end(),
|
|---|
| 137 | data4.begin(), data4.end()
|
|---|
| 138 | );
|
|---|
| 139 | test("equal pt1", res15);
|
|---|
| 140 |
|
|---|
| 141 | auto res16 = std::equal(
|
|---|
| 142 | data4.begin(), data4.end(),
|
|---|
| 143 | data5.begin(), data5.end()
|
|---|
| 144 | );
|
|---|
| 145 | test("equal pt2", !res16);
|
|---|
| 146 |
|
|---|
| 147 | auto res17 = std::equal(
|
|---|
| 148 | data4.begin(), data4.end(),
|
|---|
| 149 | data4.begin(), data4.end(),
|
|---|
| 150 | [](auto lhs, auto rhs){
|
|---|
| 151 | return lhs == rhs;
|
|---|
| 152 | }
|
|---|
| 153 | );
|
|---|
| 154 | test("equal pt3", res17);
|
|---|
| 155 |
|
|---|
| 156 | auto res18 = std::equal(
|
|---|
| 157 | data4.begin(), data4.end(),
|
|---|
| 158 | data5.begin(), data5.end(),
|
|---|
| 159 | [](auto lhs, auto rhs){
|
|---|
| 160 | return lhs == rhs;
|
|---|
| 161 | }
|
|---|
| 162 | );
|
|---|
| 163 | test("equal pt4", !res18);
|
|---|
| 164 |
|
|---|
| 165 | // TODO: is_permutation, search
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | void algorithm_test::test_mutating()
|
|---|
| 169 | {
|
|---|
| 170 | auto check1 = {1, 2, 3, 10, 20, 30, 40};
|
|---|
| 171 | std::array<int, 4> data1{10, 20, 30, 40};
|
|---|
| 172 | std::array<int, 7> data2{1, 2, 3, 4, 5, 6, 7};
|
|---|
| 173 |
|
|---|
| 174 | auto res1 = std::copy(
|
|---|
| 175 | data1.begin(), data1.end(), data2.begin() + 3
|
|---|
| 176 | );
|
|---|
| 177 |
|
|---|
| 178 | test_eq(
|
|---|
| 179 | "copy pt1", check1.begin(), check1.end(),
|
|---|
| 180 | data2.begin(), data2.end()
|
|---|
| 181 | );
|
|---|
| 182 | test_eq("copy pt2", res1, data2.end());
|
|---|
| 183 |
|
|---|
| 184 | auto check2 = {1, 2, 3, 10, 20, 30, 7, 8};
|
|---|
| 185 | std::array<int, 8> data3{1, 2, 3, 4, 5, 6, 7, 8};
|
|---|
| 186 |
|
|---|
| 187 | auto res2 = std::copy_n(
|
|---|
| 188 | data1.begin(), 3, data3.begin() + 3
|
|---|
| 189 | );
|
|---|
| 190 | test_eq(
|
|---|
| 191 | "copy_n pt1", check2.begin(), check2.end(),
|
|---|
| 192 | data3.begin(), data3.end()
|
|---|
| 193 | );
|
|---|
| 194 | test_eq("copy_n pt2", res2, &data3[6]);
|
|---|
| 195 |
|
|---|
| 196 | auto check3 = {2, 4, 6, 8};
|
|---|
| 197 | std::array<int, 4> data4{0, 0, 0, 0};
|
|---|
| 198 | std::array<int, 9> data5{1, 2, 3, 4, 5, 6, 7, 8, 9};
|
|---|
| 199 |
|
|---|
| 200 | auto res3 = std::copy_if(
|
|---|
| 201 | data5.begin(), data5.end(), data4.begin(),
|
|---|
| 202 | [](auto x){ return x % 2 == 0; }
|
|---|
| 203 | );
|
|---|
| 204 |
|
|---|
| 205 | test_eq(
|
|---|
| 206 | "copy_if pt1", check3.begin(), check3.end(),
|
|---|
| 207 | data4.begin(), data4.end()
|
|---|
| 208 | );
|
|---|
| 209 | test_eq("copy_if pt2", res3, data4.end());
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * Note: Not testing copy_backwards as it isn't
|
|---|
| 213 | * really easy to test, but since it is
|
|---|
| 214 | * widely used in the rest of the library
|
|---|
| 215 | * (mainly right shifting in sequence
|
|---|
| 216 | * containers) it's tested by other tests.
|
|---|
| 217 | */
|
|---|
| 218 |
|
|---|
| 219 | auto check4 = {std::string{"A"}, std::string{"B"}, std::string{"C"}};
|
|---|
| 220 | std::array<std::string, 3> data6{"A", "B", "C"};
|
|---|
| 221 | std::array<std::string, 3> data7{"", "", ""};
|
|---|
| 222 |
|
|---|
| 223 | auto res4 = std::move(
|
|---|
| 224 | data6.begin(), data6.end(), data7.begin()
|
|---|
| 225 | );
|
|---|
| 226 | test_eq(
|
|---|
| 227 | "move pt1", check4.begin(), check4.end(),
|
|---|
| 228 | data7.begin(), data7.end()
|
|---|
| 229 | );
|
|---|
| 230 | test(
|
|---|
| 231 | "move pt2", (data6[0].empty() && data6[1].empty(),
|
|---|
| 232 | data6[2].empty())
|
|---|
| 233 | );
|
|---|
| 234 | test_eq("move pt3", res4, data7.end());
|
|---|
| 235 |
|
|---|
| 236 | auto check5 = {1, 2, 3, 4};
|
|---|
| 237 | auto check6 = {10, 20, 30, 40};
|
|---|
| 238 | std::array<int, 4> data8{1, 2, 3, 4};
|
|---|
| 239 | std::array<int, 4> data9{10, 20, 30, 40};
|
|---|
| 240 |
|
|---|
| 241 | auto res5 = std::swap_ranges(
|
|---|
| 242 | data8.begin(), data8.end(),
|
|---|
| 243 | data9.begin()
|
|---|
| 244 | );
|
|---|
| 245 |
|
|---|
| 246 | test_eq(
|
|---|
| 247 | "swap_ranges pt1", check6.begin(), check6.end(),
|
|---|
| 248 | data8.begin(), data8.end()
|
|---|
| 249 | );
|
|---|
| 250 | test_eq(
|
|---|
| 251 | "swap_ranges pt2", check5.begin(), check5.end(),
|
|---|
| 252 | data9.begin(), data9.end()
|
|---|
| 253 | );
|
|---|
| 254 | test_eq("swap_ranges pt3", res5, data9.end());
|
|---|
| 255 |
|
|---|
| 256 | std::iter_swap(data8.begin(), data9.begin());
|
|---|
| 257 | test_eq("swap_iter pt1", data8[0], 1);
|
|---|
| 258 | test_eq("swap_iter pt2", data9[0], 10);
|
|---|
| 259 |
|
|---|
| 260 | auto check7 = {2, 3, 4, 5};
|
|---|
| 261 | std::array<int, 4> data10{1, 2, 3, 4};
|
|---|
| 262 |
|
|---|
| 263 | auto res6 = std::transform(
|
|---|
| 264 | data10.begin(), data10.end(), data10.begin(),
|
|---|
| 265 | [](auto x) { return x + 1; }
|
|---|
| 266 | );
|
|---|
| 267 | test_eq(
|
|---|
| 268 | "transform pt1",
|
|---|
| 269 | check7.begin(), check7.end(),
|
|---|
| 270 | data10.begin(), data10.end()
|
|---|
| 271 | );
|
|---|
| 272 | test_eq("transform pt2", res6, data10.end());
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|