| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// expensive: * [1-9] * * |
| 19 |
#include "bits/main.h" |
| 20 |
|
| 21 |
template <typename V> |
| 22 |
void |
| 23 |
test() |
| 24 |
{ |
| 25 |
using M = typename V::mask_type; |
| 26 |
const M alternating_mask = make_alternating_mask<M>(); |
| 27 |
COMPARE(alternating_mask[0], false); // assumption below |
| 28 |
auto&& gen = make_mask<M>; |
| 29 |
|
| 30 |
// all_of |
| 31 |
VERIFY(all_of(M{true})); |
| 32 |
VERIFY(!all_of(alternating_mask)); |
| 33 |
VERIFY(!all_of(M{false})); |
| 34 |
using std::experimental::all_of; |
| 35 |
VERIFY(all_of(true)); |
| 36 |
VERIFY(!all_of(false)); |
| 37 |
VERIFY(sfinae_is_callable<bool>( |
| 38 |
[](auto x) -> decltype(std::experimental::all_of(x)) { return {}; })); |
| 39 |
VERIFY(!sfinae_is_callable<int>( |
| 40 |
[](auto x) -> decltype(std::experimental::all_of(x)) { return {}; })); |
| 41 |
VERIFY(!sfinae_is_callable<float>( |
| 42 |
[](auto x) -> decltype(std::experimental::all_of(x)) { return {}; })); |
| 43 |
VERIFY(!sfinae_is_callable<char>( |
| 44 |
[](auto x) -> decltype(std::experimental::all_of(x)) { return {}; })); |
| 45 |
|
| 46 |
// any_of |
| 47 |
VERIFY(any_of(M{true})); |
| 48 |
COMPARE(any_of(alternating_mask), M::size() > 1); |
| 49 |
VERIFY(!any_of(M{false})); |
| 50 |
using std::experimental::any_of; |
| 51 |
VERIFY(any_of(true)); |
| 52 |
VERIFY(!any_of(false)); |
| 53 |
VERIFY(sfinae_is_callable<bool>( |
| 54 |
[](auto x) -> decltype(std::experimental::any_of(x)) { return {}; })); |
| 55 |
VERIFY(!sfinae_is_callable<int>( |
| 56 |
[](auto x) -> decltype(std::experimental::any_of(x)) { return {}; })); |
| 57 |
VERIFY(!sfinae_is_callable<float>( |
| 58 |
[](auto x) -> decltype(std::experimental::any_of(x)) { return {}; })); |
| 59 |
VERIFY(!sfinae_is_callable<char>( |
| 60 |
[](auto x) -> decltype(std::experimental::any_of(x)) { return {}; })); |
| 61 |
|
| 62 |
// none_of |
| 63 |
VERIFY(!none_of(M{true})); |
| 64 |
COMPARE(none_of(alternating_mask), M::size() == 1); |
| 65 |
VERIFY(none_of(M{false})); |
| 66 |
using std::experimental::none_of; |
| 67 |
VERIFY(!none_of(true)); |
| 68 |
VERIFY(none_of(false)); |
| 69 |
VERIFY(sfinae_is_callable<bool>( |
| 70 |
[](auto x) -> decltype(std::experimental::none_of(x)) { return {}; })); |
| 71 |
VERIFY(!sfinae_is_callable<int>( |
| 72 |
[](auto x) -> decltype(std::experimental::none_of(x)) { return {}; })); |
| 73 |
VERIFY(!sfinae_is_callable<float>( |
| 74 |
[](auto x) -> decltype(std::experimental::none_of(x)) { return {}; })); |
| 75 |
VERIFY(!sfinae_is_callable<char>( |
| 76 |
[](auto x) -> decltype(std::experimental::none_of(x)) { return {}; })); |
| 77 |
|
| 78 |
// some_of |
| 79 |
VERIFY(!some_of(M{true})); |
| 80 |
VERIFY(!some_of(M{false})); |
| 81 |
if (M::size() > 1) |
| 82 |
{ |
| 83 |
VERIFY(some_of(gen({true, false}))); |
| 84 |
VERIFY(some_of(gen({false, true}))); |
| 85 |
if (M::size() > 3) |
| 86 |
{ |
| 87 |
VERIFY(some_of(gen({0, 0, 0, 1}))); |
| 88 |
} |
| 89 |
} |
| 90 |
using std::experimental::some_of; |
| 91 |
VERIFY(!some_of(true)); |
| 92 |
VERIFY(!some_of(false)); |
| 93 |
VERIFY(sfinae_is_callable<bool>( |
| 94 |
[](auto x) -> decltype(std::experimental::some_of(x)) { return {}; })); |
| 95 |
VERIFY(!sfinae_is_callable<int>( |
| 96 |
[](auto x) -> decltype(std::experimental::some_of(x)) { return {}; })); |
| 97 |
VERIFY(!sfinae_is_callable<float>( |
| 98 |
[](auto x) -> decltype(std::experimental::some_of(x)) { return {}; })); |
| 99 |
VERIFY(!sfinae_is_callable<char>( |
| 100 |
[](auto x) -> decltype(std::experimental::some_of(x)) { return {}; })); |
| 101 |
|
| 102 |
// popcount |
| 103 |
COMPARE(popcount(M{true}), int(M::size())); |
| 104 |
COMPARE(popcount(alternating_mask), int(M::size()) / 2); |
| 105 |
COMPARE(popcount(M{false}), 0); |
| 106 |
COMPARE(popcount(gen({0, 0, 1})), int(M::size()) / 3); |
| 107 |
COMPARE(popcount(gen({0, 0, 0, 1})), int(M::size()) / 4); |
| 108 |
COMPARE(popcount(gen({0, 0, 0, 0, 1})), int(M::size()) / 5); |
| 109 |
COMPARE(std::experimental::popcount(true), 1); |
| 110 |
COMPARE(std::experimental::popcount(false), 0); |
| 111 |
VERIFY(sfinae_is_callable<bool>( |
| 112 |
[](auto x) -> decltype(std::experimental::popcount(x)) { return {}; })); |
| 113 |
VERIFY(!sfinae_is_callable<int>( |
| 114 |
[](auto x) -> decltype(std::experimental::popcount(x)) { return {}; })); |
| 115 |
VERIFY(!sfinae_is_callable<float>( |
| 116 |
[](auto x) -> decltype(std::experimental::popcount(x)) { return {}; })); |
| 117 |
VERIFY(!sfinae_is_callable<char>( |
| 118 |
[](auto x) -> decltype(std::experimental::popcount(x)) { return {}; })); |
| 119 |
|
| 120 |
// find_first_set |
| 121 |
{ |
| 122 |
M x(false); |
| 123 |
for (int i = int(M::size() / 2 - 1); i >= 0; --i) |
| 124 |
{ |
| 125 |
x[i] = true; |
| 126 |
COMPARE(find_first_set(x), i) << x; |
| 127 |
} |
| 128 |
x = M(false); |
| 129 |
for (int i = int(M::size() - 1); i >= 0; --i) |
| 130 |
{ |
| 131 |
x[i] = true; |
| 132 |
COMPARE(find_first_set(x), i) << x; |
| 133 |
} |
| 134 |
} |
| 135 |
COMPARE(find_first_set(M{true}), 0); |
| 136 |
if (M::size() > 1) |
| 137 |
{ |
| 138 |
COMPARE(find_first_set(gen({0, 1})), 1); |
| 139 |
} |
| 140 |
if (M::size() > 2) |
| 141 |
{ |
| 142 |
COMPARE(find_first_set(gen({0, 0, 1})), 2); |
| 143 |
} |
| 144 |
COMPARE(std::experimental::find_first_set(true), 0); |
| 145 |
VERIFY(sfinae_is_callable<bool>( |
| 146 |
[](auto x) -> decltype(std::experimental::find_first_set(x)) { |
| 147 |
return {}; |
| 148 |
})); |
| 149 |
VERIFY(!sfinae_is_callable<int>( |
| 150 |
[](auto x) -> decltype(std::experimental::find_first_set(x)) { |
| 151 |
return {}; |
| 152 |
})); |
| 153 |
VERIFY(!sfinae_is_callable<float>( |
| 154 |
[](auto x) -> decltype(std::experimental::find_first_set(x)) { |
| 155 |
return {}; |
| 156 |
})); |
| 157 |
VERIFY(!sfinae_is_callable<char>( |
| 158 |
[](auto x) -> decltype(std::experimental::find_first_set(x)) { |
| 159 |
return {}; |
| 160 |
})); |
| 161 |
|
| 162 |
// find_last_set |
| 163 |
{ |
| 164 |
M x(false); |
| 165 |
for (int i = 0; i < int(M::size()); ++i) |
| 166 |
{ |
| 167 |
x[i] = true; |
| 168 |
COMPARE(find_last_set(x), i) << x; |
| 169 |
} |
| 170 |
} |
| 171 |
COMPARE(find_last_set(M{true}), int(M::size()) - 1); |
| 172 |
if (M::size() > 1) |
| 173 |
{ |
| 174 |
COMPARE(find_last_set(gen({1, 0})), |
| 175 |
int(M::size()) - 2 + int(M::size() & 1)); |
| 176 |
} |
| 177 |
if (M::size() > 3 && (M::size() & 3) == 0) |
| 178 |
{ |
| 179 |
COMPARE(find_last_set(gen({1, 0, 0, 0})), |
| 180 |
int(M::size()) - 4 - int(M::size() & 3)); |
| 181 |
} |
| 182 |
COMPARE(std::experimental::find_last_set(true), 0); |
| 183 |
VERIFY(sfinae_is_callable<bool>( |
| 184 |
[](auto x) -> decltype(std::experimental::find_last_set(x)) { |
| 185 |
return {}; |
| 186 |
})); |
| 187 |
VERIFY(!sfinae_is_callable<int>( |
| 188 |
[](auto x) -> decltype(std::experimental::find_last_set(x)) { |
| 189 |
return {}; |
| 190 |
})); |
| 191 |
VERIFY(!sfinae_is_callable<float>( |
| 192 |
[](auto x) -> decltype(std::experimental::find_last_set(x)) { |
| 193 |
return {}; |
| 194 |
})); |
| 195 |
VERIFY(!sfinae_is_callable<char>( |
| 196 |
[](auto x) -> decltype(std::experimental::find_last_set(x)) { |
| 197 |
return {}; |
| 198 |
})); |
| 199 |
} |