1 // Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
2 // https://github.com/Dobiasd/FunctionalPlus
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 //  http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <doctest/doctest.h>
8 #include <fplus/fplus.hpp>
9 #include <vector>
10 
11 namespace
12 {
is_even(int value)13     bool is_even(int value)
14     {
15         return value % 2 == 0;
16     }
17 }
18 
19 TEST_CASE("search_test - find_first_by")
20 {
21     std::vector<int> v = { 1, 3, 4, 6, 9 };
22     auto result = fplus::find_first_by(is_even, v);
23     REQUIRE_EQ(result, fplus::just(4));
24 }
25 
26 TEST_CASE("search_test - find_first_by_nothing_found")
27 {
28     std::vector<int> v = { 1, 3, 5, 7, 9 };
29     auto result = fplus::find_first_by(is_even, v);
30     REQUIRE_EQ(result, fplus::nothing<int>());
31 }
32 
33 TEST_CASE("search_test - find_last_by")
34 {
35     std::vector<int> v = { 1, 3, 4, 6, 9 };
36     auto result = fplus::find_last_by(is_even, v);
37     REQUIRE_EQ(result, fplus::just(6));
38 }
39 
40 TEST_CASE("search_test - find_last_by_nothing_found")
41 {
42     std::vector<int> v = { 1, 3, 5, 7, 9 };
43     auto result = fplus::find_first_by(is_even, v);
44     REQUIRE_EQ(result, fplus::nothing<int>());
45 }
46 
47 TEST_CASE("search_test - find_first_idx_by")
48 {
49     std::vector<int> v = { 1, 3, 4, 6, 9 };
50     auto result = fplus::find_first_idx_by(is_even, v);
51     REQUIRE_EQ(result, fplus::just<std::size_t>(2));
52 }
53 
54 TEST_CASE("search_test - find_first_idx_by_nothing_found")
55 {
56     std::vector<int> v = { 1, 3, 5, 7, 9 };
57     auto result = fplus::find_first_idx_by(is_even, v);
58     REQUIRE_EQ(result, fplus::nothing<size_t>());
59 }
60 
61 TEST_CASE("search_test - find_last_idx_by")
62 {
63     std::vector<int> v = { 1, 3, 4, 6, 9 };
64     auto result = fplus::find_last_idx_by(is_even, v);
65     REQUIRE_EQ(result, fplus::just<size_t>(3));
66 }
67 
68 TEST_CASE("search_test - find_last_idx_by_nothing_found")
69 {
70     std::vector<int> v = { 1, 3, 5, 7, 9 };
71     auto result = fplus::find_last_idx_by(is_even, v);
72     REQUIRE_EQ(result, fplus::nothing<size_t>());
73 }
74 
75 TEST_CASE("search_test - find_first_idx")
76 {
77     std::vector<int> v = { 1, 3, 4, 4, 9 };
78     auto result = fplus::find_first_idx(4, v);
79     REQUIRE_EQ(result, fplus::just<size_t>(2));
80 }
81 
82 TEST_CASE("search_test - find_first_idx_nothing_found")
83 {
84     std::vector<int> v = { 1, 3, 5, 7, 9 };
85     auto result = fplus::find_first_idx(4, v);
86     REQUIRE_EQ(result, fplus::nothing<size_t>());
87 }
88 
89 TEST_CASE("search_test - find_last_idx")
90 {
91     std::vector<int> v = { 1, 3, 4, 4, 9 };
92     auto result = fplus::find_last_idx(4, v);
93     REQUIRE_EQ(result, fplus::just<size_t>(3));
94 }
95 
96 TEST_CASE("search_test - find_last_idx_nothing_found")
97 {
98     std::vector<int> v = { 1, 3, 5, 7, 9 };
99     auto result = fplus::find_last_idx(4, v);
100     REQUIRE_EQ(result, fplus::nothing<size_t>());
101 }
102 
103 TEST_CASE("search_test - find_all_idxs_by")
104 {
105     std::vector<int> v = { 1, 3, 4, 6, 9 };
106     auto result = fplus::find_all_idxs_by(is_even, v);
107     REQUIRE_EQ(result, std::vector<std::size_t>({2, 3}));
108 }
109 
110 TEST_CASE("search_test - find_last_idxs_by_nothing_found")
111 {
112     std::vector<int> v = { 1, 3, 5, 7, 9 };
113     auto result = fplus::find_all_idxs_by(is_even, v);
114     REQUIRE(result.empty());
115 }
116 
117 TEST_CASE("search_test - find_all_idxs_of")
118 {
119     std::vector<int> v = { 1, 3, 4, 4, 9 };
120     auto result = fplus::find_all_idxs_of(4, v);
121     REQUIRE_EQ(result, std::vector<size_t>({2, 3}));
122 }
123 
124 TEST_CASE("search_test - find_all_instances_of_token")
125 {
126     const std::string token = "haha";
127     const std::string input = "oh, hahaha!";
128     auto result = fplus::find_all_instances_of_token(token, input);
129     REQUIRE_EQ(result, std::vector<std::size_t>({4, 6}));
130 }
131 
132 TEST_CASE("search_test - find_last_idxs_of_nothing_found")
133 {
134     std::vector<int> v = { 1, 3, 5, 7, 9 };
135     auto result = fplus::find_all_idxs_of(4, v);
136     REQUIRE(result.empty());
137 }
138 
139 TEST_CASE("search_test - find_all_instances_of_token_oversized_token")
140 {
141     const std::string token = "hahahahaha";
142     const std::string input = "oh, hahaha!";
143     auto result = fplus::find_all_instances_of_token(token, input);
144     REQUIRE(result.empty());
145 }
146 
147 TEST_CASE("search_test - find_all_instances_of_token_non_overlapping")
148 {
149     const std::string token = "haha";
150     const std::string input = "oh, hahaha!";
151     auto result = fplus::find_all_instances_of_token_non_overlapping(token, input);
152     REQUIRE_EQ(result, std::vector<std::size_t>({4}));
153 }
154 
155 TEST_CASE("search_test - find_first_instance_of_token")
156 {
157     const std::string token = "haha";
158     const std::string input = "oh, hahaha!";
159     auto result = fplus::find_first_instance_of_token(token, input);
160     REQUIRE_EQ(result, fplus::just<size_t>(4));
161 }
162 
163 TEST_CASE("search_test - find_first_instance_of_token_at_end")
164 {
165     const std::string token = "haha";
166     const std::string input = "oh, haha";
167     auto result = fplus::find_first_instance_of_token(token, input);
168     REQUIRE_EQ(result, fplus::just<size_t>(4));
169 }
170 
171 TEST_CASE("search_test - find_first_instance_of_token_nothing")
172 {
173     const std::string token = "hihi";
174     const std::string input = "oh, haha";
175     auto result = fplus::find_first_instance_of_token(token, input);
176     REQUIRE_EQ(result, fplus::nothing<size_t>());
177 }
178 
179 TEST_CASE("search_test - find_first_instance_of_token_oversized_token")
180 {
181     const std::string token = "hahahahaha";
182     const std::string input = "oh, hahaha!";
183     auto result = fplus::find_first_instance_of_token(token, input);
184     REQUIRE_EQ(result, fplus::nothing<size_t>());
185 }
186