-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_set.cpp
192 lines (162 loc) · 4.25 KB
/
test_set.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <set.hpp>
#include <ut.hpp>
#include <set>
namespace ut = boost::ut;
// template<typename K>
// using set = edgs::set<K>;
/**
* @brief tests for simple set
*
*/
template < template <typename... T> typename set, ut::fixed_string Name>
ut::suite<Name> test_set = []
{
using namespace boost::ut;
using namespace boost::ut::spec;
describe("constructors") = [] {
it("should construct empty") = [] {
set<int> s;
expect(s.size() == 0 && s.empty());
};
it("should construct from initializer list ") = [] {
set<int> s{4, 9, 2};
auto it = s.begin();
expect(
*it == 2 &&
*(++it) == 4 &&
*(++it) == 9 && s.size() == 3);
};
};
describe("modifiers") = [] {
it("should insert") = [] {
set<int> s;
s.insert(1);
s.insert(2);
expect(*s.begin() == 1 && *(++s.begin()) == 2);
};
it("should not insert duplicate") = [] {
set<int> s;
s.insert(1);
auto result = s.insert(1);
expect(result.second == false && s.size() == 1);
};
it("should insert and reorder") = [] {
set<int> s;
s.insert(8);
s.insert(2);
s.insert(5);
s.insert(4);
s.insert(9); // 2 4 5 8 9
auto it = s.begin();
expect(
*it == 2 &&
*(++it) == 4 &&
*(++it) == 5 &&
*(++it) == 8 &&
*(++it) == 9 );
};
it("should construt, insert and reorder") = [] {
set<int> s { 2, 3, 8};
s.insert(5);
s.insert(4);
s.insert(9); // 2 3 4 5 8 9
auto it = s.begin();
expect(
*(it++) == 2 &&
*(it++) == 3 &&
*(it++) == 4 &&
*(it++) == 5 &&
*(it++) == 8 &&
*(it++) == 9 );
};
it("should clear") = [] {
set<int> s{4, 9, 2};
expect(s.size() == 3 && !s.empty());
s.clear();
expect(s.size() == 0 && s.empty());
};
};
describe("iterators" ) = [] {
it("should point to past-the-end empty") = [] {
set<int> s;
auto it = s.begin();
expect(it == s.end());
};
it("should point to past-the-end") = [] {
set<int> s{1};
auto it = s.begin();
++it;
expect(it == s.end());
};
it("begin == end if empty") = [] {
set<int> s;
expect(s.begin() == s.end() && s.size() == 0);
};
it("begin == end if not empty") = [] {
set<int> s{1, 2, 3};
expect(s.begin() != s.end() && s.size() == 3);
};
};
describe("lookup" ) = [] {
it("should contain and find") = [] {
set<int> s;
s.insert(1);
expect(s.contains(1) && *s.find(1) == 1 );
};
it("should count no duplicates") = [] {
set<int> s;
s.insert(8);
s.insert(8);
expect(s.count(8) == 1);
};
it("upper_bound should return next possible element in set") = [] {
set<int> s{ 8, 2, 5, 4};
auto it = s.upper_bound(0);
expect(*it == 2);
it = s.upper_bound(2);
expect(*it == 4);
it = s.upper_bound(3);
expect(*it == 4);
};
it("invalid upper_bound index should return end") = [] {
set<int> s;
auto it = s.upper_bound(500);
expect(it == s.end());
it = s.upper_bound(-500);
expect(it == s.end());
set<int> s2{ 1 };
it = s2.upper_bound(-500);
expect(*it == 1);
it = s2.upper_bound(500);
expect(it == s.end());
it = s2.upper_bound(1);
expect(it == s.end());
};
it("lower_bound should return next possible element in set") = [] {
set<int> s{ 8, 2, 5, 4};
auto it = s.lower_bound(0);
expect(*it == 2);
it = s.lower_bound(2);
expect(*it == 2);
it = s.lower_bound(3);
expect(*it == 4);
};
it("invalid lower_bound index should return end") = [] {
set<int> s;
auto it = s.lower_bound(500);
expect(it == s.end());
it = s.lower_bound(-500);
expect(it == s.end());
set<int> s2{ 1 };
it = s2.lower_bound(-500);
expect(*it == 1);
it = s2.lower_bound(500);
expect(it == s2.end());
it = s2.lower_bound(1);
expect(it != s2.end() && *it == 1);
};
};
};
inline auto s1 = test_set<edgs::set, "edgs::set">;
// TODO
// inline auto s2 = test_set< std::set, "std::set">;