[libcxx-commits] [libcxx] [libc++] Improve tests for std::find_if and std::find_if_not (PR #71192)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Nov 3 08:48:52 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Louis Dionne (ldionne)
<details>
<summary>Changes</summary>
These tests are salvaged from https://reviews.llvm.org/D112152 which I decided not to pursue anymore.
---
Full diff: https://github.com/llvm/llvm-project/pull/71192.diff
3 Files Affected:
- (modified) libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp (+41-30)
- (modified) libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp (+41-30)
- (modified) libcxx/utils/data/ignore_format.txt (-2)
``````````diff
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp
index a294b5c6a281043..fe548c10b96273b 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp
@@ -20,38 +20,49 @@
#include "test_macros.h"
#include "test_iterators.h"
-struct eq {
- TEST_CONSTEXPR eq (int val) : v(val) {}
- TEST_CONSTEXPR bool operator () (int v2) const { return v == v2; }
- int v;
- };
-
-#if TEST_STD_VER > 17
-TEST_CONSTEXPR bool test_constexpr() {
- int ia[] = {1, 3, 5, 2, 4, 6};
- int ib[] = {1, 2, 3, 7, 5, 6};
- eq c(4);
- return (std::find_if(std::begin(ia), std::end(ia), c) == ia+4)
- && (std::find_if(std::begin(ib), std::end(ib), c) == ib+6)
- ;
+struct EqualTo {
+ int v;
+ TEST_CONSTEXPR EqualTo(int val) : v(val) {}
+ TEST_CONSTEXPR bool operator()(int other) const { return v == other; }
+};
+
+template <class Iter>
+TEST_CONSTEXPR_CXX17 void test_iter() {
+ int range[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+
+ // We don't find what we're looking for in the range
+ {
+ {
+ Iter result = std::find_if(Iter(range), Iter(range), EqualTo(0));
+ assert(result == Iter(range));
}
-#endif
+ {
+ Iter result = std::find_if(Iter(range), Iter(std::end(range)), EqualTo(999));
+ assert(result == Iter(std::end(range)));
+ }
+ }
+
+ // Tests with sub-ranges of various sizes
+ for (int size = 1; size != 10; ++size) {
+ for (int i = 0; i != size - 1; ++i) {
+ Iter result = std::find_if(Iter(range), Iter(range + size), EqualTo(i));
+ assert(result == Iter(range + i));
+ }
+ }
+}
+
+TEST_CONSTEXPR_CXX17 bool test() {
+ test_iter<cpp17_input_iterator<int*>>();
+ test_iter<forward_iterator<int*>>();
+ test_iter<bidirectional_iterator<int*>>();
+ test_iter<random_access_iterator<int*>>();
+ return true;
+}
-int main(int, char**)
-{
- int ia[] = {0, 1, 2, 3, 4, 5};
- const unsigned s = sizeof(ia)/sizeof(ia[0]);
- cpp17_input_iterator<const int*> r = std::find_if(cpp17_input_iterator<const int*>(ia),
- cpp17_input_iterator<const int*>(ia+s),
- eq(3));
- assert(*r == 3);
- r = std::find_if(cpp17_input_iterator<const int*>(ia),
- cpp17_input_iterator<const int*>(ia+s),
- eq(10));
- assert(r == cpp17_input_iterator<const int*>(ia+s));
-
-#if TEST_STD_VER > 17
- static_assert(test_constexpr());
+int main(int, char**) {
+ test();
+#if TEST_STD_VER >= 20
+ static_assert(test());
#endif
return 0;
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp
index 6f48da3b9b42d64..8848337b18f014f 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp
@@ -20,38 +20,49 @@
#include "test_macros.h"
#include "test_iterators.h"
-struct ne {
- TEST_CONSTEXPR ne (int val) : v(val) {}
- TEST_CONSTEXPR bool operator () (int v2) const { return v != v2; }
- int v;
- };
-
-#if TEST_STD_VER > 17
-TEST_CONSTEXPR bool test_constexpr() {
- int ia[] = {1, 3, 5, 2, 4, 6};
- int ib[] = {1, 2, 3, 7, 5, 6};
- ne c(4);
- return (std::find_if_not(std::begin(ia), std::end(ia), c) == ia+4)
- && (std::find_if_not(std::begin(ib), std::end(ib), c) == ib+6)
- ;
+struct DifferentFrom {
+ int v;
+ TEST_CONSTEXPR DifferentFrom(int val) : v(val) {}
+ TEST_CONSTEXPR bool operator()(int other) const { return v != other; }
+};
+
+template <class Iter>
+TEST_CONSTEXPR_CXX17 void test_iter() {
+ int range[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+
+ // We don't find what we're looking for in the range
+ {
+ {
+ Iter result = std::find_if_not(Iter(range), Iter(range), DifferentFrom(0));
+ assert(result == Iter(range));
}
-#endif
+ {
+ Iter result = std::find_if_not(Iter(range), Iter(std::end(range)), DifferentFrom(999));
+ assert(result == Iter(std::end(range)));
+ }
+ }
+
+ // Tests with sub-ranges of various sizes
+ for (int size = 1; size != 10; ++size) {
+ for (int i = 0; i != size - 1; ++i) {
+ Iter result = std::find_if_not(Iter(range), Iter(range + size), DifferentFrom(i));
+ assert(result == Iter(range + i));
+ }
+ }
+}
+
+TEST_CONSTEXPR_CXX17 bool test() {
+ test_iter<cpp17_input_iterator<int*>>();
+ test_iter<forward_iterator<int*>>();
+ test_iter<bidirectional_iterator<int*>>();
+ test_iter<random_access_iterator<int*>>();
+ return true;
+}
-int main(int, char**)
-{
- int ia[] = {0, 1, 2, 3, 4, 5};
- const unsigned s = sizeof(ia)/sizeof(ia[0]);
- cpp17_input_iterator<const int*> r = std::find_if_not(cpp17_input_iterator<const int*>(ia),
- cpp17_input_iterator<const int*>(ia+s),
- ne(3));
- assert(*r == 3);
- r = std::find_if_not(cpp17_input_iterator<const int*>(ia),
- cpp17_input_iterator<const int*>(ia+s),
- ne(10));
- assert(r == cpp17_input_iterator<const int*>(ia+s));
-
-#if TEST_STD_VER > 17
- static_assert(test_constexpr());
+int main(int, char**) {
+ test();
+#if TEST_STD_VER >= 20
+ static_assert(test());
#endif
return 0;
diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 1eb3bf5d5e86751..2892d10a5efb040 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -1138,8 +1138,6 @@ libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/ranges.equal.pass.cpp
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/find_end.pass.cpp
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/find_end_pred.pass.cpp
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/ranges.find_end.pass.cpp
-libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if_not.pass.cpp
-libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find_if.pass.cpp
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.first.of/find_first_of.pass.cpp
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.first.of/find_first_of_pred.pass.cpp
libcxx/test/std/algorithms/alg.nonmodifying/alg.find.first.of/ranges.find_first_of.pass.cpp
``````````
</details>
https://github.com/llvm/llvm-project/pull/71192
More information about the libcxx-commits
mailing list