-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbag.cpp
More file actions
211 lines (179 loc) · 6.82 KB
/
Copy pathbag.cpp
File metadata and controls
211 lines (179 loc) · 6.82 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (C) 2018 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <foonathan/array/bag.hpp>
#include <catch.hpp>
#include "equal_checker.hpp"
#include "leak_checker.hpp"
using namespace foonathan::array;
namespace
{
struct test_type : leak_tracked
{
std::uint16_t id;
test_type(int i) : id(static_cast<std::uint16_t>(i)) {}
};
using test_bag = bag<test_type>;
void verify_bag_impl(const test_bag& bag, std::initializer_list<int> ids)
{
REQUIRE(bag.empty() == (bag.size() == 0u));
REQUIRE(bag.size() == size_type(ids.end() - ids.begin()));
REQUIRE(bag.capacity() >= bag.size());
REQUIRE(bag.capacity() <= bag.max_size());
auto view = block_view<const test_type>(bag);
REQUIRE(view.size() == bag.size());
REQUIRE(view.data() == iterator_to_pointer(bag.begin()));
REQUIRE(view.data_end() == iterator_to_pointer(bag.end()));
REQUIRE(view.data() == iterator_to_pointer(bag.cbegin()));
REQUIRE(view.data_end() == iterator_to_pointer(bag.cend()));
check_equal(
view.begin(), view.end(), ids.begin(), ids.end(),
[](const test_type& test, int i) { return test.id == i; },
[&](const test_type& test) { FAIL_CHECK(std::hex << test.id); });
}
void verify_bag(const test_bag& bag, std::initializer_list<int> ids)
{
verify_bag_impl(bag, ids);
// copy constructor
test_bag copy(bag);
verify_bag_impl(copy, ids);
REQUIRE(copy.capacity() <= bag.capacity());
// shrink to fit
auto old_cap = copy.capacity();
copy.shrink_to_fit();
verify_bag_impl(copy, ids);
REQUIRE(copy.capacity() <= old_cap);
// reserve
copy.reserve(copy.size() + 4u);
REQUIRE(copy.capacity() >= copy.size() + 4u);
verify_bag_impl(copy, ids);
// copy assignment
copy.emplace(0xFFFF);
copy = bag;
verify_bag_impl(copy, ids);
// range assignment
copy.emplace(0xFFFF);
copy.assign_range(bag.begin(), bag.end());
verify_bag_impl(copy, ids);
// block assignment
copy.emplace(0xFFFF);
copy.assign(bag);
verify_bag_impl(copy, ids);
}
} // namespace
TEST_CASE("bag", "[container]")
{
leak_checker checker;
SECTION("insertion")
{
test_bag bag;
verify_bag(bag, {});
bag.emplace(0xF0F0);
verify_bag(bag, {0xF0F0});
bag.insert(test_type(0xF1F1));
verify_bag(bag, {0xF0F0, 0xF1F1});
test_type test(0xF2F2);
bag.insert(test);
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2});
test_type tests[] = {{0xF3F3}, {0xF4F4}, {0xF5F5}};
bag.insert(tests);
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5});
bag.insert_range(std::begin(tests), std::end(tests));
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4, 0xF5F5});
SECTION("clear")
{
auto old_cap = bag.capacity();
bag.clear();
REQUIRE(old_cap == bag.capacity());
verify_bag(bag, {});
}
SECTION("erase")
{
auto iter = bag.begin();
iter = bag.erase(iter);
verify_bag(bag, {0xF5F5, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4});
REQUIRE(iter == bag.begin());
iter = bag.erase(std::next(iter, 3));
verify_bag(bag, {0xF5F5, 0xF1F1, 0xF2F2, 0xF4F4, 0xF4F4, 0xF5F5, 0xF3F3});
REQUIRE(iter == bag.begin() + 3);
while (iter != bag.end())
iter = bag.erase(iter);
verify_bag(bag, {0xF5F5, 0xF1F1, 0xF2F2});
}
SECTION("erase range")
{
auto begin = bag.end() - 3;
auto end = bag.end();
auto next = bag.erase_range(begin, end);
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5});
REQUIRE(next == bag.end());
begin = bag.begin() + 1;
end = bag.begin() + 3;
next = bag.erase_range(begin, end);
verify_bag(bag, {0xF0F0, 0xF4F4, 0xF5F5, 0xF3F3});
REQUIRE(next == bag.begin() + 1);
begin = bag.begin() + 1;
end = bag.begin() + 3;
next = bag.erase_range(begin, end);
verify_bag(bag, {0xF0F0, 0xF3F3});
REQUIRE(next == bag.begin() + 1);
}
SECTION("move constructor")
{
auto data = iterator_to_pointer(bag.begin());
test_bag other(std::move(bag));
verify_bag(other,
{0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4, 0xF5F5});
verify_bag(bag, {});
REQUIRE(iterator_to_pointer(other.begin()) == data);
SECTION("copy assignment")
{
bag = other;
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4,
0xF5F5});
verify_bag(other, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4,
0xF5F5});
}
SECTION("move assignment")
{
bag = std::move(other);
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4,
0xF5F5});
verify_bag(other, {});
REQUIRE(iterator_to_pointer(bag.begin()) == data);
}
SECTION("input_view assignment")
{
bag = input_view<test_type, block_storage_new<default_growth>>(std::move(other));
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4,
0xF5F5});
verify_bag(other, {});
REQUIRE(iterator_to_pointer(bag.begin()) == data);
}
SECTION("swap")
{
swap(bag, other);
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4,
0xF5F5});
verify_bag(other, {});
REQUIRE(iterator_to_pointer(bag.begin()) == data);
}
}
}
SECTION("input_view ctor")
{
test_bag bag{{test_type(0xF0F0), test_type(0xF1F1), test_type(0xF2F2), test_type(0xF3F3)}};
verify_bag(bag, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3});
}
}
TEST_CASE("bag_insert_iterator", "[container]")
{
int array[] = {1, 2, 3, 4, 5};
bag<int> b;
std::copy_if(std::begin(array), std::end(array), bag_inserter(b),
[](int i) { return i % 2 == 0; });
int expected[] = {2, 4};
check_equal(
b.begin(), b.end(), std::begin(expected), std::end(expected),
[](int a, int b) { return a == b; }, [](int i) { FAIL_CHECK(i); });
}