|
| 1 | + |
| 2 | +// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com) |
| 3 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#include <doctest/doctest.h> |
| 7 | + |
| 8 | +#include <array> |
| 9 | +#include <concepts> |
| 10 | +#include <cctype> |
| 11 | +#include <string> |
| 12 | +#include <string_view> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#ifdef USE_MODULES |
| 16 | +import flux; |
| 17 | +#else |
| 18 | +# include <flux/core/default_impls.hpp> |
| 19 | +# include <flux/algorithm/find.hpp> |
| 20 | +#endif |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +struct S { |
| 25 | + int i_; |
| 26 | +}; |
| 27 | + |
| 28 | +using find_if_fn = decltype(flux::find_if); |
| 29 | + |
| 30 | +using int_comp = auto(int x) -> bool; |
| 31 | +static_assert(std::invocable<find_if_fn, int[10], int_comp>); |
| 32 | + |
| 33 | +using const_int_comp = auto(const int x) -> bool; |
| 34 | +static_assert(std::invocable<find_if_fn, int[10], const_int_comp>); |
| 35 | + |
| 36 | +using const_int_ref_comp = auto(const int& x) -> bool; |
| 37 | +static_assert(std::invocable<find_if_fn, int[10], const_int_ref_comp>); |
| 38 | + |
| 39 | +// Incompatible predicate type |
| 40 | +using S_comp = auto(S x) -> bool; |
| 41 | +static_assert(not std::invocable<find_if_fn, int[10], S_comp>); |
| 42 | + |
| 43 | +// Not a predicate |
| 44 | +using not_a_predicate = auto(int x) -> S; |
| 45 | +static_assert(not std::invocable<find_if_fn, int[10], not_a_predicate>); |
| 46 | + |
| 47 | +constexpr bool test_find_if() |
| 48 | +{ |
| 49 | + { |
| 50 | + int const ints[] = {0, 1, 2, 3, 4, 5}; |
| 51 | + |
| 52 | + auto is_three = [](int x) { return x == 3; }; |
| 53 | + auto is_ten = [](int x) { return x == 10; }; |
| 54 | + auto is_negative = [](int x) { return x < 0; }; |
| 55 | + auto is_greater_than_4 = [](int x) { return x > 4; }; |
| 56 | + |
| 57 | + auto cur = flux::find_if(ints, is_three); |
| 58 | + if (cur != 3) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + cur = flux::find_if(ints, is_ten); |
| 63 | + if (!flux::is_last(ints, cur)) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + cur = flux::find_if(ints, is_negative); |
| 68 | + if (!flux::is_last(ints, cur)) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + cur = flux::find_if(ints, is_greater_than_4); |
| 73 | + if (cur != 5) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + auto lens = flux::ref(ints); |
| 78 | + |
| 79 | + cur = lens.find_if(is_three); |
| 80 | + if (cur != 3) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + cur = lens.find_if(is_ten); |
| 85 | + if (!lens.is_last(cur)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + cur = lens.find_if(is_negative); |
| 90 | + if (!flux::is_last(ints, cur)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + cur = lens.find_if(is_greater_than_4); |
| 95 | + if (cur != 5) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | +} |
| 102 | +static_assert(test_find_if()); |
| 103 | + |
| 104 | +} // namespace |
| 105 | + |
| 106 | +TEST_CASE("find_if") |
| 107 | +{ |
| 108 | + REQUIRE(test_find_if()); |
| 109 | + |
| 110 | + { |
| 111 | + std::vector<int> vec {1, 2, 3, 4, 5}; |
| 112 | + auto is_greater_than_3 = [](int x) { return x > 3; }; |
| 113 | + auto idx = flux::find_if(vec, is_greater_than_3); |
| 114 | + REQUIRE(idx == 3); |
| 115 | + } |
| 116 | + |
| 117 | + { |
| 118 | + std::vector<int> vec {1, 2, 3, 4, 5}; |
| 119 | + auto is_negative = [](int x) { return x < 0; }; |
| 120 | + auto idx = flux::ref(vec).find_if(is_negative); |
| 121 | + REQUIRE(idx == std::ssize(vec)); |
| 122 | + } |
| 123 | + |
| 124 | + { |
| 125 | + std::string_view str = ""; |
| 126 | + auto is_lower = [](char x) { return std::islower(static_cast<unsigned char>(x)); }; |
| 127 | + auto idx = flux::find_if(str, is_lower); |
| 128 | + REQUIRE(idx == flux::last(str)); |
| 129 | + } |
| 130 | + |
| 131 | + { |
| 132 | + std::string_view str = "abcdefg"; |
| 133 | + auto is_upper = [](char x) { return std::isupper(static_cast<unsigned char>(x)); }; |
| 134 | + auto idx = flux::find_if(str, is_upper); |
| 135 | + REQUIRE(idx == flux::last(str)); |
| 136 | + } |
| 137 | + |
| 138 | + { |
| 139 | + std::string str = "abcdefg123xyz"; |
| 140 | + auto is_numeric = [](char x) { return std::isdigit(static_cast<unsigned char>(x)); }; |
| 141 | + auto idx = flux::find_if(str, is_numeric); |
| 142 | + REQUIRE(idx == 7); |
| 143 | + } |
| 144 | +} |
0 commit comments