Skip to content

Commit 465d55f

Browse files
authored
Merge pull request #212 from nwad123/main
Added find_if and find_if_not tests
2 parents 46cb9a5 + a0459ac commit 465d55f

File tree

3 files changed

+290
-0
lines changed

3 files changed

+290
-0
lines changed

test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ add_executable(test-flux
5151
test_filter.cpp
5252
test_filter_map.cpp
5353
test_find.cpp
54+
test_find_if.cpp
55+
test_find_if_not.cpp
5456
test_find_min_max.cpp
5557
test_flatten.cpp
5658
test_flatten_with.cpp

test/test_find_if.cpp

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

test/test_find_if_not.cpp

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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_not_fn = decltype(flux::find_if_not);
29+
30+
using int_comp = auto(int x) -> bool;
31+
static_assert(std::invocable<find_if_not_fn, int[10], int_comp>);
32+
33+
using const_int_comp = auto(const int x) -> bool;
34+
static_assert(std::invocable<find_if_not_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_not_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_not_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_not_fn, int[10], not_a_predicate>);
46+
47+
constexpr bool test_find_if_not()
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_zero = [](int x) { return x == 0; };
54+
auto is_greater_than_zero = [](int x) { return x >= 0; };
55+
auto is_0_1_2_or_3 = [](int x) { return 0 <= x && x <= 3; };
56+
57+
auto cur = flux::find_if_not(ints, is_three);
58+
if (cur != 0) {
59+
return false;
60+
}
61+
62+
cur = flux::find_if_not(ints, is_zero);
63+
if (cur != 1) {
64+
return false;
65+
}
66+
67+
cur = flux::find_if_not(ints, is_greater_than_zero);
68+
if (!flux::is_last(ints, cur)) {
69+
return false;
70+
}
71+
72+
cur = flux::find_if_not(ints, is_0_1_2_or_3);
73+
if (cur != 4) {
74+
return false;
75+
}
76+
77+
auto lens = flux::ref(ints);
78+
79+
cur = lens.find_if_not(is_three);
80+
if (cur != 0) {
81+
return false;
82+
}
83+
84+
cur = lens.find_if_not(is_zero);
85+
if (cur != 1) {
86+
return false;
87+
}
88+
89+
cur = lens.find_if_not(is_greater_than_zero);
90+
if (!flux::is_last(ints, cur)) {
91+
return false;
92+
}
93+
94+
cur = lens.find_if_not(is_0_1_2_or_3);
95+
if (cur != 4) {
96+
return false;
97+
}
98+
}
99+
100+
return true;
101+
}
102+
static_assert(test_find_if_not());
103+
104+
} // namespace
105+
106+
TEST_CASE("find_if_not")
107+
{
108+
REQUIRE(test_find_if_not());
109+
110+
{
111+
std::vector<int> vec {1, 2, 3, 4, 5};
112+
auto is_odd = [](int x) { return x % 2 == 1; };
113+
auto idx = flux::find_if_not(vec, is_odd);
114+
REQUIRE(idx == 1);
115+
}
116+
117+
{
118+
std::vector<int> vec {1, 2, 3, 4, 5};
119+
auto is_positive = [](int x) { return x > 0; };
120+
auto idx = flux::ref(vec).find_if_not(is_positive);
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_lower = [](char x) { return std::islower(static_cast<unsigned char>(x)); };
134+
auto idx = flux::find_if_not(str, is_lower);
135+
REQUIRE(idx == flux::last(str));
136+
}
137+
138+
{
139+
std::string str = "123abc";
140+
auto is_numeric = [](char x) { return std::isdigit(static_cast<unsigned char>(x)); };
141+
auto idx = flux::find_if_not(str, is_numeric);
142+
REQUIRE(idx == 3);
143+
}
144+
}

0 commit comments

Comments
 (0)