-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy patharray.cpp
More file actions
247 lines (209 loc) · 8.58 KB
/
Copy patharray.cpp
File metadata and controls
247 lines (209 loc) · 8.58 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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/array.hpp>
#include <catch.hpp>
#include <foonathan/array/block_storage_embedded.hpp>
#include <foonathan/array/block_storage_sbo.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)) {}
};
template <class BlockStorage>
using test_array = array<test_type, BlockStorage>;
template <class Array>
void verify_array_impl(const Array& array, std::initializer_list<int> ids)
{
REQUIRE(array.empty() == (array.size() == 0u));
REQUIRE(array.size() == size_type(ids.end() - ids.begin()));
REQUIRE(array.capacity() >= array.size());
REQUIRE(array.capacity() <= array.max_size());
auto view = array_view<const test_type>(array);
REQUIRE(view.size() == array.size());
REQUIRE(view.data() == iterator_to_pointer(array.begin()));
REQUIRE(view.data_end() == iterator_to_pointer(array.end()));
REQUIRE(view.data() == iterator_to_pointer(array.cbegin()));
REQUIRE(view.data_end() == iterator_to_pointer(array.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); });
for (size_type i = 0u; i != array.size(); ++i)
REQUIRE(array[i].id == ids.begin()[i]);
if (!array.empty())
{
REQUIRE(array.front().id == *ids.begin());
REQUIRE(array.back().id == *std::prev(ids.end()));
}
}
template <class Array>
void verify_array(const Array& array, std::initializer_list<int> ids)
{
verify_array_impl(array, ids);
// copy constructor
Array copy(array);
verify_array_impl(copy, ids);
REQUIRE(copy.capacity() <= array.capacity());
// shrink to fit
auto old_cap = copy.capacity();
copy.shrink_to_fit();
verify_array_impl(copy, ids);
REQUIRE(copy.capacity() <= old_cap);
// reserve
copy.reserve(copy.size() + 4u);
REQUIRE(copy.capacity() >= copy.size() + 4u);
verify_array_impl(copy, ids);
// copy assignment
copy.push_back(0xFFFF);
copy = array;
verify_array_impl(copy, ids);
// range assignment
copy.push_back(0xFFFF);
copy.assign_range(array.begin(), array.end());
verify_array_impl(copy, ids);
// block assignment
copy.push_back(0xFFFF);
copy.assign(array);
verify_array_impl(copy, ids);
}
template <class Array>
void array_test_impl()
{
SECTION("default ctor")
{
Array array;
verify_array(array, {});
// emplace_back/push_back
array.emplace_back(0xF0F0);
verify_array(array, {0xF0F0});
array.push_back(test_type(0xF1F1));
verify_array(array, {0xF0F0, 0xF1F1});
test_type test(0xF2F2);
array.push_back(test);
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2});
SECTION("emplace/insert")
{
array.emplace(array.begin(), 0xF3F3);
verify_array(array, {0xF3F3, 0xF0F0, 0xF1F1, 0xF2F2});
array.insert(std::next(array.begin()), 0xF4F4);
verify_array(array, {0xF3F3, 0xF4F4, 0xF0F0, 0xF1F1, 0xF2F2});
test.id = 0xF5F5;
array.insert(std::prev(array.end()), test);
verify_array(array, {0xF3F3, 0xF4F4, 0xF0F0, 0xF1F1, 0xF5F5, 0xF2F2});
}
SECTION("append")
{
test_type tests[] = {{0xF3F3}, {0xF4F4}, {0xF5F5}};
array.append(tests);
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5});
array.append_range(std::begin(tests), std::end(tests));
verify_array(array,
{0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4, 0xF5F5});
SECTION("pop_back/erase")
{
array.pop_back();
verify_array(array,
{0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4});
array.erase(array.begin());
verify_array(array, {0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4});
array.erase(array.begin() + 3);
verify_array(array, {0xF1F1, 0xF2F2, 0xF3F3, 0xF5F5, 0xF3F3, 0xF4F4});
}
SECTION("erase range")
{
array.erase_range(std::prev(array.end(), 3), array.end());
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5});
array.erase_range(std::next(array.begin()), std::prev(array.end()));
verify_array(array, {0xF0F0, 0xF5F5});
array.erase_range(array.begin(), array.begin());
verify_array(array, {0xF0F0, 0xF5F5});
}
SECTION("move constructor")
{
using is_embedded = embedded_storage<typename Array::block_storage>;
auto data = iterator_to_pointer(array.begin());
Array other(std::move(array));
verify_array(other, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3, 0xF4F4,
0xF5F5});
verify_array(array, {});
if (!is_embedded{})
REQUIRE(iterator_to_pointer(other.begin()) == data);
SECTION("copy assignment")
{
array = other;
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3,
0xF4F4, 0xF5F5});
verify_array(other, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3,
0xF4F4, 0xF5F5});
}
SECTION("move assignment")
{
array = std::move(other);
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3,
0xF4F4, 0xF5F5});
verify_array(other, {});
if (!is_embedded{})
REQUIRE(iterator_to_pointer(array.begin()) == data);
}
SECTION("input_view assignment")
{
array = input_view<test_type, typename Array::block_storage>(std::move(other));
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3,
0xF4F4, 0xF5F5});
verify_array(other, {});
if (!is_embedded{})
REQUIRE(iterator_to_pointer(array.begin()) == data);
}
SECTION("swap")
{
swap(array, other);
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3, 0xF4F4, 0xF5F5, 0xF3F3,
0xF4F4, 0xF5F5});
verify_array(other, {});
if (!is_embedded{})
REQUIRE(iterator_to_pointer(array.begin()) == data);
}
}
}
SECTION("multi insert")
{
test_type tests[] = {{0xF3F3}, {0xF4F4}, {0xF5F5}};
array.insert(std::next(array.begin()), tests);
verify_array(array, {0xF0F0, 0xF3F3, 0xF4F4, 0xF5F5, 0xF1F1, 0xF2F2});
array.insert_range(std::prev(array.end()), std::begin(tests), std::end(tests));
verify_array(array,
{0xF0F0, 0xF3F3, 0xF4F4, 0xF5F5, 0xF1F1, 0xF3F3, 0xF4F4, 0xF5F5, 0xF2F2});
}
SECTION("clear")
{
auto old_cap = array.capacity();
array.clear();
REQUIRE(old_cap == array.capacity());
verify_array(array, {});
}
}
SECTION("input_view ctor")
{
Array array{{test_type(0xF0F0), test_type(0xF1F1), test_type(0xF2F2), test_type(0xF3F3)}};
verify_array(array, {0xF0F0, 0xF1F1, 0xF2F2, 0xF3F3});
}
}
} // namespace
TEST_CASE("array block_storage_new", "[container]")
{
array_test_impl<test_array<block_storage_new<default_growth>>>();
}
TEST_CASE("array block_storage_embedded", "[container]")
{
array_test_impl<test_array<block_storage_embedded<15 * sizeof(test_type)>>>();
}
TEST_CASE("array block_storage_sbo", "[container]")
{
array_test_impl<test_array<block_storage_sbo<5 * sizeof(test_type), block_storage_default>>>();
}