Skip to content

Commit 0109834

Browse files
committed
Move binary_search tests to their own file.
1 parent ed797fb commit 0109834

File tree

3 files changed

+113
-102
lines changed

3 files changed

+113
-102
lines changed

source/containers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ target_sources(containers_test PRIVATE
224224
test/sort/sort_exactly_n.cpp
225225
test/sort/test_sort.cpp
226226
test/algorithms/adjacent.cpp
227+
test/algorithms/binary_search.cpp
227228
test/algorithms/chunk_by.cpp
228229
test/algorithms/compare.cpp
229230
test/algorithms/count.cpp

source/containers/algorithms/binary_search.cpp

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ module;
1010
export module containers.algorithms.binary_search;
1111

1212
import containers.algorithms.partition;
13-
import containers.array;
1413
import containers.begin_end;
1514
import containers.range;
1615
import containers.range_view;
1716

18-
import bounded;
1917
import std_module;
2018

21-
using namespace bounded::literal;
22-
2319
namespace containers {
2420

2521
struct lower_bound_t {
@@ -71,101 +67,3 @@ struct binary_search_t {
7167
export constexpr auto binary_search = binary_search_t();
7268

7369
} // namespace containers
74-
75-
constexpr auto zero = containers::array<int, 0_bi>{};
76-
constexpr auto one = containers::array{1};
77-
constexpr auto two = containers::array{1, 2};
78-
constexpr auto three = containers::array{1, 2, 3};
79-
constexpr auto two_duplicates = containers::array{1, 1};
80-
constexpr auto three_duplicates = containers::array{1, 1, 1};
81-
constexpr auto three_two_duplicates_first = containers::array{1, 1, 2};
82-
constexpr auto three_two_duplicates_last = containers::array{1, 2, 2};
83-
84-
static_assert(containers::lower_bound(zero, 0) == end(zero));
85-
static_assert(containers::upper_bound(zero, 0) == end(zero));
86-
static_assert(!containers::binary_search(zero, 0));
87-
88-
static_assert(containers::lower_bound(one, 0) == begin(one));
89-
static_assert(containers::lower_bound(one, 1) == begin(one));
90-
static_assert(containers::lower_bound(one, 2) == end(one));
91-
static_assert(containers::upper_bound(one, 0) == begin(one));
92-
static_assert(containers::upper_bound(one, 1) == end(one));
93-
static_assert(containers::upper_bound(one, 2) == end(one));
94-
static_assert(!containers::binary_search(one, 0));
95-
static_assert(containers::binary_search(one, 1));
96-
static_assert(!containers::binary_search(one, 2));
97-
98-
static_assert(containers::lower_bound(two, 0) == begin(two) + 0_bi);
99-
static_assert(containers::lower_bound(two, 1) == begin(two) + 0_bi);
100-
static_assert(containers::lower_bound(two, 2) == begin(two) + 1_bi);
101-
static_assert(containers::lower_bound(two, 3) == end(two));
102-
static_assert(containers::upper_bound(two, 0) == begin(two) + 0_bi);
103-
static_assert(containers::upper_bound(two, 1) == begin(two) + 1_bi);
104-
static_assert(containers::upper_bound(two, 2) == begin(two) + 2_bi);
105-
static_assert(containers::upper_bound(two, 3) == end(two));
106-
static_assert(!containers::binary_search(two, 0));
107-
static_assert(containers::binary_search(two, 1));
108-
static_assert(containers::binary_search(two, 2));
109-
static_assert(!containers::binary_search(two, 3));
110-
111-
static_assert(containers::lower_bound(three, 0) == begin(three) + 0_bi);
112-
static_assert(containers::lower_bound(three, 1) == begin(three) + 0_bi);
113-
static_assert(containers::lower_bound(three, 2) == begin(three) + 1_bi);
114-
static_assert(containers::lower_bound(three, 3) == begin(three) + 2_bi);
115-
static_assert(containers::lower_bound(three, 4) == end(three));
116-
static_assert(containers::upper_bound(three, 0) == begin(three) + 0_bi);
117-
static_assert(containers::upper_bound(three, 1) == begin(three) + 1_bi);
118-
static_assert(containers::upper_bound(three, 2) == begin(three) + 2_bi);
119-
static_assert(containers::upper_bound(three, 3) == begin(three) + 3_bi);
120-
static_assert(containers::upper_bound(three, 4) == end(three));
121-
static_assert(!containers::binary_search(three, 0));
122-
static_assert(containers::binary_search(three, 1));
123-
static_assert(containers::binary_search(three, 2));
124-
static_assert(containers::binary_search(three, 3));
125-
static_assert(!containers::binary_search(three, 4));
126-
127-
static_assert(containers::lower_bound(two_duplicates, 0) == begin(two_duplicates) + 0_bi);
128-
static_assert(containers::lower_bound(two_duplicates, 1) == begin(two_duplicates) + 0_bi);
129-
static_assert(containers::lower_bound(two_duplicates, 2) == end(two_duplicates));
130-
static_assert(containers::upper_bound(two_duplicates, 0) == begin(two_duplicates) + 0_bi);
131-
static_assert(containers::upper_bound(two_duplicates, 1) == end(two_duplicates));
132-
static_assert(containers::upper_bound(two_duplicates, 2) == end(two_duplicates));
133-
static_assert(!containers::binary_search(two_duplicates, 0));
134-
static_assert(containers::binary_search(two_duplicates, 1));
135-
static_assert(!containers::binary_search(two_duplicates, 2));
136-
137-
static_assert(containers::lower_bound(three_duplicates, 0) == begin(three_duplicates) + 0_bi);
138-
static_assert(containers::lower_bound(three_duplicates, 1) == begin(three_duplicates) + 0_bi);
139-
static_assert(containers::lower_bound(three_duplicates, 2) == end(three_duplicates));
140-
static_assert(containers::upper_bound(three_duplicates, 0) == begin(three_duplicates) + 0_bi);
141-
static_assert(containers::upper_bound(three_duplicates, 1) == end(three_duplicates));
142-
static_assert(containers::upper_bound(three_duplicates, 2) == end(three_duplicates));
143-
static_assert(!containers::binary_search(three_duplicates, 0));
144-
static_assert(containers::binary_search(three_duplicates, 1));
145-
static_assert(!containers::binary_search(three_duplicates, 2));
146-
147-
static_assert(containers::lower_bound(three_two_duplicates_first, 0) == begin(three_two_duplicates_first) + 0_bi);
148-
static_assert(containers::lower_bound(three_two_duplicates_first, 1) == begin(three_two_duplicates_first) + 0_bi);
149-
static_assert(containers::lower_bound(three_two_duplicates_first, 2) == begin(three_two_duplicates_first) + 2_bi);
150-
static_assert(containers::lower_bound(three_two_duplicates_first, 3) == end(three_two_duplicates_first));
151-
static_assert(containers::upper_bound(three_two_duplicates_first, 0) == begin(three_two_duplicates_first) + 0_bi);
152-
static_assert(containers::upper_bound(three_two_duplicates_first, 1) == begin(three_two_duplicates_first) + 2_bi);
153-
static_assert(containers::upper_bound(three_two_duplicates_first, 2) == begin(three_two_duplicates_first) + 3_bi);
154-
static_assert(containers::upper_bound(three_two_duplicates_first, 3) == end(three_two_duplicates_first));
155-
static_assert(!containers::binary_search(three_two_duplicates_first, 0));
156-
static_assert(containers::binary_search(three_two_duplicates_first, 1));
157-
static_assert(containers::binary_search(three_two_duplicates_first, 2));
158-
static_assert(!containers::binary_search(three_two_duplicates_first, 3));
159-
160-
static_assert(containers::lower_bound(three_two_duplicates_last, 0) == begin(three_two_duplicates_last) + 0_bi);
161-
static_assert(containers::lower_bound(three_two_duplicates_last, 1) == begin(three_two_duplicates_last) + 0_bi);
162-
static_assert(containers::lower_bound(three_two_duplicates_last, 2) == begin(three_two_duplicates_last) + 1_bi);
163-
static_assert(containers::lower_bound(three_two_duplicates_last, 3) == end(three_two_duplicates_last));
164-
static_assert(containers::upper_bound(three_two_duplicates_last, 0) == begin(three_two_duplicates_last) + 0_bi);
165-
static_assert(containers::upper_bound(three_two_duplicates_last, 1) == begin(three_two_duplicates_last) + 1_bi);
166-
static_assert(containers::upper_bound(three_two_duplicates_last, 2) == begin(three_two_duplicates_last) + 3_bi);
167-
static_assert(containers::upper_bound(three_two_duplicates_last, 3) == end(three_two_duplicates_last));
168-
static_assert(!containers::binary_search(three_two_duplicates_last, 0));
169-
static_assert(containers::binary_search(three_two_duplicates_last, 1));
170-
static_assert(containers::binary_search(three_two_duplicates_last, 2));
171-
static_assert(!containers::binary_search(three_two_duplicates_last, 3));
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright David Stone 2024.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
export module containers.test.algorithms.binary_search;
7+
8+
import containers.algorithms.binary_search;
9+
import containers.array;
10+
import containers.begin_end;
11+
12+
import bounded;
13+
14+
using namespace bounded::literal;
15+
16+
constexpr auto zero = containers::array<int, 0_bi>{};
17+
constexpr auto one = containers::array{1};
18+
constexpr auto two = containers::array{1, 2};
19+
constexpr auto three = containers::array{1, 2, 3};
20+
constexpr auto two_duplicates = containers::array{1, 1};
21+
constexpr auto three_duplicates = containers::array{1, 1, 1};
22+
constexpr auto three_two_duplicates_first = containers::array{1, 1, 2};
23+
constexpr auto three_two_duplicates_last = containers::array{1, 2, 2};
24+
25+
static_assert(containers::lower_bound(zero, 0) == end(zero));
26+
static_assert(containers::upper_bound(zero, 0) == end(zero));
27+
static_assert(!containers::binary_search(zero, 0));
28+
29+
static_assert(containers::lower_bound(one, 0) == begin(one));
30+
static_assert(containers::lower_bound(one, 1) == begin(one));
31+
static_assert(containers::lower_bound(one, 2) == end(one));
32+
static_assert(containers::upper_bound(one, 0) == begin(one));
33+
static_assert(containers::upper_bound(one, 1) == end(one));
34+
static_assert(containers::upper_bound(one, 2) == end(one));
35+
static_assert(!containers::binary_search(one, 0));
36+
static_assert(containers::binary_search(one, 1));
37+
static_assert(!containers::binary_search(one, 2));
38+
39+
static_assert(containers::lower_bound(two, 0) == begin(two) + 0_bi);
40+
static_assert(containers::lower_bound(two, 1) == begin(two) + 0_bi);
41+
static_assert(containers::lower_bound(two, 2) == begin(two) + 1_bi);
42+
static_assert(containers::lower_bound(two, 3) == end(two));
43+
static_assert(containers::upper_bound(two, 0) == begin(two) + 0_bi);
44+
static_assert(containers::upper_bound(two, 1) == begin(two) + 1_bi);
45+
static_assert(containers::upper_bound(two, 2) == begin(two) + 2_bi);
46+
static_assert(containers::upper_bound(two, 3) == end(two));
47+
static_assert(!containers::binary_search(two, 0));
48+
static_assert(containers::binary_search(two, 1));
49+
static_assert(containers::binary_search(two, 2));
50+
static_assert(!containers::binary_search(two, 3));
51+
52+
static_assert(containers::lower_bound(three, 0) == begin(three) + 0_bi);
53+
static_assert(containers::lower_bound(three, 1) == begin(three) + 0_bi);
54+
static_assert(containers::lower_bound(three, 2) == begin(three) + 1_bi);
55+
static_assert(containers::lower_bound(three, 3) == begin(three) + 2_bi);
56+
static_assert(containers::lower_bound(three, 4) == end(three));
57+
static_assert(containers::upper_bound(three, 0) == begin(three) + 0_bi);
58+
static_assert(containers::upper_bound(three, 1) == begin(three) + 1_bi);
59+
static_assert(containers::upper_bound(three, 2) == begin(three) + 2_bi);
60+
static_assert(containers::upper_bound(three, 3) == begin(three) + 3_bi);
61+
static_assert(containers::upper_bound(three, 4) == end(three));
62+
static_assert(!containers::binary_search(three, 0));
63+
static_assert(containers::binary_search(three, 1));
64+
static_assert(containers::binary_search(three, 2));
65+
static_assert(containers::binary_search(three, 3));
66+
static_assert(!containers::binary_search(three, 4));
67+
68+
static_assert(containers::lower_bound(two_duplicates, 0) == begin(two_duplicates) + 0_bi);
69+
static_assert(containers::lower_bound(two_duplicates, 1) == begin(two_duplicates) + 0_bi);
70+
static_assert(containers::lower_bound(two_duplicates, 2) == end(two_duplicates));
71+
static_assert(containers::upper_bound(two_duplicates, 0) == begin(two_duplicates) + 0_bi);
72+
static_assert(containers::upper_bound(two_duplicates, 1) == end(two_duplicates));
73+
static_assert(containers::upper_bound(two_duplicates, 2) == end(two_duplicates));
74+
static_assert(!containers::binary_search(two_duplicates, 0));
75+
static_assert(containers::binary_search(two_duplicates, 1));
76+
static_assert(!containers::binary_search(two_duplicates, 2));
77+
78+
static_assert(containers::lower_bound(three_duplicates, 0) == begin(three_duplicates) + 0_bi);
79+
static_assert(containers::lower_bound(three_duplicates, 1) == begin(three_duplicates) + 0_bi);
80+
static_assert(containers::lower_bound(three_duplicates, 2) == end(three_duplicates));
81+
static_assert(containers::upper_bound(three_duplicates, 0) == begin(three_duplicates) + 0_bi);
82+
static_assert(containers::upper_bound(three_duplicates, 1) == end(three_duplicates));
83+
static_assert(containers::upper_bound(three_duplicates, 2) == end(three_duplicates));
84+
static_assert(!containers::binary_search(three_duplicates, 0));
85+
static_assert(containers::binary_search(three_duplicates, 1));
86+
static_assert(!containers::binary_search(three_duplicates, 2));
87+
88+
static_assert(containers::lower_bound(three_two_duplicates_first, 0) == begin(three_two_duplicates_first) + 0_bi);
89+
static_assert(containers::lower_bound(three_two_duplicates_first, 1) == begin(three_two_duplicates_first) + 0_bi);
90+
static_assert(containers::lower_bound(three_two_duplicates_first, 2) == begin(three_two_duplicates_first) + 2_bi);
91+
static_assert(containers::lower_bound(three_two_duplicates_first, 3) == end(three_two_duplicates_first));
92+
static_assert(containers::upper_bound(three_two_duplicates_first, 0) == begin(three_two_duplicates_first) + 0_bi);
93+
static_assert(containers::upper_bound(three_two_duplicates_first, 1) == begin(three_two_duplicates_first) + 2_bi);
94+
static_assert(containers::upper_bound(three_two_duplicates_first, 2) == begin(three_two_duplicates_first) + 3_bi);
95+
static_assert(containers::upper_bound(three_two_duplicates_first, 3) == end(three_two_duplicates_first));
96+
static_assert(!containers::binary_search(three_two_duplicates_first, 0));
97+
static_assert(containers::binary_search(three_two_duplicates_first, 1));
98+
static_assert(containers::binary_search(three_two_duplicates_first, 2));
99+
static_assert(!containers::binary_search(three_two_duplicates_first, 3));
100+
101+
static_assert(containers::lower_bound(three_two_duplicates_last, 0) == begin(three_two_duplicates_last) + 0_bi);
102+
static_assert(containers::lower_bound(three_two_duplicates_last, 1) == begin(three_two_duplicates_last) + 0_bi);
103+
static_assert(containers::lower_bound(three_two_duplicates_last, 2) == begin(three_two_duplicates_last) + 1_bi);
104+
static_assert(containers::lower_bound(three_two_duplicates_last, 3) == end(three_two_duplicates_last));
105+
static_assert(containers::upper_bound(three_two_duplicates_last, 0) == begin(three_two_duplicates_last) + 0_bi);
106+
static_assert(containers::upper_bound(three_two_duplicates_last, 1) == begin(three_two_duplicates_last) + 1_bi);
107+
static_assert(containers::upper_bound(three_two_duplicates_last, 2) == begin(three_two_duplicates_last) + 3_bi);
108+
static_assert(containers::upper_bound(three_two_duplicates_last, 3) == end(three_two_duplicates_last));
109+
static_assert(!containers::binary_search(three_two_duplicates_last, 0));
110+
static_assert(containers::binary_search(three_two_duplicates_last, 1));
111+
static_assert(containers::binary_search(three_two_duplicates_last, 2));
112+
static_assert(!containers::binary_search(three_two_duplicates_last, 3));

0 commit comments

Comments
 (0)