Skip to content

Commit 2b4b26e

Browse files
authored
[libc++] Improve tests for std::find_if and std::find_if_not (#71192)
These tests are salvaged from https://reviews.llvm.org/D112152 which I decided not to pursue anymore.
1 parent c670cdb commit 2b4b26e

File tree

3 files changed

+82
-62
lines changed

3 files changed

+82
-62
lines changed

libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,49 @@
2020
#include "test_macros.h"
2121
#include "test_iterators.h"
2222

23-
struct eq {
24-
TEST_CONSTEXPR eq (int val) : v(val) {}
25-
TEST_CONSTEXPR bool operator () (int v2) const { return v == v2; }
26-
int v;
27-
};
28-
29-
#if TEST_STD_VER > 17
30-
TEST_CONSTEXPR bool test_constexpr() {
31-
int ia[] = {1, 3, 5, 2, 4, 6};
32-
int ib[] = {1, 2, 3, 7, 5, 6};
33-
eq c(4);
34-
return (std::find_if(std::begin(ia), std::end(ia), c) == ia+4)
35-
&& (std::find_if(std::begin(ib), std::end(ib), c) == ib+6)
36-
;
23+
struct EqualTo {
24+
int v;
25+
TEST_CONSTEXPR EqualTo(int val) : v(val) {}
26+
TEST_CONSTEXPR bool operator()(int other) const { return v == other; }
27+
};
28+
29+
template <class Iter>
30+
TEST_CONSTEXPR_CXX17 void test_iter() {
31+
int range[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
32+
33+
// We don't find what we're looking for in the range
34+
{
35+
{
36+
Iter result = std::find_if(Iter(range), Iter(range), EqualTo(0));
37+
assert(result == Iter(range));
3738
}
38-
#endif
39+
{
40+
Iter result = std::find_if(Iter(range), Iter(std::end(range)), EqualTo(999));
41+
assert(result == Iter(std::end(range)));
42+
}
43+
}
44+
45+
// Tests with sub-ranges of various sizes
46+
for (int size = 1; size != 10; ++size) {
47+
for (int i = 0; i != size - 1; ++i) {
48+
Iter result = std::find_if(Iter(range), Iter(range + size), EqualTo(i));
49+
assert(result == Iter(range + i));
50+
}
51+
}
52+
}
53+
54+
TEST_CONSTEXPR_CXX17 bool test() {
55+
test_iter<cpp17_input_iterator<int*> >();
56+
test_iter<forward_iterator<int*> >();
57+
test_iter<bidirectional_iterator<int*> >();
58+
test_iter<random_access_iterator<int*> >();
59+
return true;
60+
}
3961

40-
int main(int, char**)
41-
{
42-
int ia[] = {0, 1, 2, 3, 4, 5};
43-
const unsigned s = sizeof(ia)/sizeof(ia[0]);
44-
cpp17_input_iterator<const int*> r = std::find_if(cpp17_input_iterator<const int*>(ia),
45-
cpp17_input_iterator<const int*>(ia+s),
46-
eq(3));
47-
assert(*r == 3);
48-
r = std::find_if(cpp17_input_iterator<const int*>(ia),
49-
cpp17_input_iterator<const int*>(ia+s),
50-
eq(10));
51-
assert(r == cpp17_input_iterator<const int*>(ia+s));
52-
53-
#if TEST_STD_VER > 17
54-
static_assert(test_constexpr());
62+
int main(int, char**) {
63+
test();
64+
#if TEST_STD_VER >= 20
65+
static_assert(test());
5566
#endif
5667

5768
return 0;

libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,49 @@
2020
#include "test_macros.h"
2121
#include "test_iterators.h"
2222

23-
struct ne {
24-
TEST_CONSTEXPR ne (int val) : v(val) {}
25-
TEST_CONSTEXPR bool operator () (int v2) const { return v != v2; }
26-
int v;
27-
};
28-
29-
#if TEST_STD_VER > 17
30-
TEST_CONSTEXPR bool test_constexpr() {
31-
int ia[] = {1, 3, 5, 2, 4, 6};
32-
int ib[] = {1, 2, 3, 7, 5, 6};
33-
ne c(4);
34-
return (std::find_if_not(std::begin(ia), std::end(ia), c) == ia+4)
35-
&& (std::find_if_not(std::begin(ib), std::end(ib), c) == ib+6)
36-
;
23+
struct DifferentFrom {
24+
int v;
25+
TEST_CONSTEXPR DifferentFrom(int val) : v(val) {}
26+
TEST_CONSTEXPR bool operator()(int other) const { return v != other; }
27+
};
28+
29+
template <class Iter>
30+
TEST_CONSTEXPR_CXX17 void test_iter() {
31+
int range[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
32+
33+
// We don't find what we're looking for in the range
34+
{
35+
{
36+
Iter result = std::find_if_not(Iter(range), Iter(range), DifferentFrom(0));
37+
assert(result == Iter(range));
3738
}
38-
#endif
39+
{
40+
Iter result = std::find_if_not(Iter(range), Iter(std::end(range)), DifferentFrom(999));
41+
assert(result == Iter(std::end(range)));
42+
}
43+
}
44+
45+
// Tests with sub-ranges of various sizes
46+
for (int size = 1; size != 10; ++size) {
47+
for (int i = 0; i != size - 1; ++i) {
48+
Iter result = std::find_if_not(Iter(range), Iter(range + size), DifferentFrom(i));
49+
assert(result == Iter(range + i));
50+
}
51+
}
52+
}
53+
54+
TEST_CONSTEXPR_CXX17 bool test() {
55+
test_iter<cpp17_input_iterator<int*> >();
56+
test_iter<forward_iterator<int*> >();
57+
test_iter<bidirectional_iterator<int*> >();
58+
test_iter<random_access_iterator<int*> >();
59+
return true;
60+
}
3961

40-
int main(int, char**)
41-
{
42-
int ia[] = {0, 1, 2, 3, 4, 5};
43-
const unsigned s = sizeof(ia)/sizeof(ia[0]);
44-
cpp17_input_iterator<const int*> r = std::find_if_not(cpp17_input_iterator<const int*>(ia),
45-
cpp17_input_iterator<const int*>(ia+s),
46-
ne(3));
47-
assert(*r == 3);
48-
r = std::find_if_not(cpp17_input_iterator<const int*>(ia),
49-
cpp17_input_iterator<const int*>(ia+s),
50-
ne(10));
51-
assert(r == cpp17_input_iterator<const int*>(ia+s));
52-
53-
#if TEST_STD_VER > 17
54-
static_assert(test_constexpr());
62+
int main(int, char**) {
63+
test();
64+
#if TEST_STD_VER >= 20
65+
static_assert(test());
5566
#endif
5667

5768
return 0;

libcxx/utils/data/ignore_format.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,6 @@ libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/ranges.equal.pass.cpp
11381138
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/find_end.pass.cpp
11391139
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/find_end_pred.pass.cpp
11401140
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/ranges.find_end.pass.cpp
1141-
libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp
1142-
libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp
11431141
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.first.of/find_first_of.pass.cpp
11441142
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.first.of/find_first_of_pred.pass.cpp
11451143
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.first.of/ranges.find_first_of.pass.cpp

0 commit comments

Comments
 (0)