File: small_vector.cpp

package info (click to toggle)
vulkan-validationlayers 1.4.321.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,412 kB
  • sloc: cpp: 594,175; python: 11,321; sh: 24; makefile: 20; xml: 14
file content (449 lines) | stat: -rw-r--r-- 14,374 bytes parent folder | download | duplicates (6)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (c) 2025 The Khronos Group Inc.
 * Copyright (c) 2025 Valve Corporation
 * Copyright (c) 2025 LunarG, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 */

#include "../framework/test_common.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <array>
#include <cstdarg>
#include <vector>

#include <vulkan/utility/vk_struct_helper.hpp>
#include "containers/small_vector.h"
#include "containers/span.h"

template <typename T, typename U>
bool HaveSameElementsUpTo(const T& l1, const U& l2, size_t n) {
    for (size_t i = 0; i < n; ++i) {
        if (l1[i] != l2[i]) {
            return false;
        }
    }
    return true;
}

template <typename T, typename U>
bool HaveSameElements(const T& l1, const U& l2) {
    return l1.size() == l2.size() && HaveSameElementsUpTo(l1, l2, l1.size());
}

TEST(CustomContainer, SmallVectorIntResize) {
    // Resize int small vector, moving to small store
    // ---
    {
        // resize to current size
        small_vector<int, 2, size_t> v1 = {1, 2, 3, 4};
        v1.resize(v1.size());
        std::array ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElements(v1, ref));
    }

    {
        // growing resize
        small_vector<int, 2, size_t> v2 = {1, 2, 3, 4};
        v2.resize(5);
        std::array ref = {1, 2, 3, 4, 0};
        ASSERT_TRUE(HaveSameElements(v2, ref));
    }

    {
        // shrinking resize
        small_vector<int, 2, size_t> v3 = {1, 2, 3, 4};
        const auto v3_cap = v3.capacity();
        v3.resize(3);
        ASSERT_TRUE(v3.capacity() == v3_cap);  // Resize doesn't shrink capacity
        v3.shrink_to_fit();
        ASSERT_TRUE(v3.capacity() == v3.size());
        std::array ref = {1, 2, 3};
        ASSERT_TRUE(HaveSameElements(v3, ref));
    }

    {
        // shrink to 0
        small_vector<int, 2, size_t> v4 = {1, 2, 3, 4};
        v4.resize(0);
        ASSERT_TRUE(v4.capacity() == 4);  // Resize doesn't shrink capacity
        v4.shrink_to_fit();
        ASSERT_TRUE(v4.capacity() == 2);  // Small capacity is in the minimal
        std::array<int, 0> ref = {};
        ASSERT_TRUE(HaveSameElements(v4, ref));
    }

    {
        // resize to size limit
        small_vector<int, 2, uint8_t> v5 = {1, 2, 3, 4};
        v5.resize(std::numeric_limits<uint8_t>::max());
        std::vector<int> vec = {1, 2, 3, 4};
        vec.resize(std::numeric_limits<uint8_t>::max());
        ASSERT_TRUE(HaveSameElements(v5, vec));
    }

    // Resize int small vector, not moving to small store
    // ---
    {
        // resize to current size
        small_vector<int, 2, size_t> v6 = {1, 2, 3, 4};
        v6.resize(v6.size());
        std::array ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElements(v6, ref));
    }

    {
        // growing resize
        small_vector<int, 2, size_t> v7 = {1, 2, 3, 4};
        v7.resize(5);
        std::array ref = {1, 2, 3, 4, 0};
        ASSERT_TRUE(HaveSameElements(v7, ref));
    }

    {
        // shrinking resize
        small_vector<int, 2, size_t> v8 = {1, 2, 3, 4};
        v8.resize(3);
        std::array ref = {1, 2, 3};
        ASSERT_TRUE(HaveSameElements(v8, ref));
    }

    {
        // shrink to 0
        small_vector<int, 2, size_t> v9 = {1, 2, 3, 4};
        v9.resize(0);
        std::array<int, 0> ref = {};
        ASSERT_TRUE(HaveSameElements(v9, ref));
    }

    {
        // resize to size limit
        small_vector<int, 2, uint8_t> v10 = {1, 2, 3, 4};
        v10.resize(std::numeric_limits<uint8_t>::max());
        std::vector<int> vec = {1, 2, 3, 4};
        vec.resize(std::numeric_limits<uint8_t>::max());
        ASSERT_TRUE(HaveSameElements(v10, vec));
    }
}

struct NoDefaultCons {
    NoDefaultCons(int x) : x(x) {}
    int x;
};

bool operator!=(const NoDefaultCons& lhs, const NoDefaultCons& rhs) { return lhs.x != rhs.x; }

TEST(CustomContainer, SmallVectorNotDefaultInsertable) {
    // Resize NoDefault small vector, moving to small store
    // ---
    {
        // resize to current size
        small_vector<NoDefaultCons, 2, size_t> v1 = {1, 2, 3, 4};
        v1.resize(v1.size());
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElements(v1, ref));
    }

    {
        // growing resize
        small_vector<NoDefaultCons, 2, size_t> v2 = {1, 2, 3, 4};
        v2.resize(5);
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElementsUpTo(v2, ref, ref.size()));
    }

    {
        // shrinking resize
        small_vector<NoDefaultCons, 2, size_t> v3 = {1, 2, 3, 4};
        const auto v3_cap = v3.capacity();
        v3.resize(3);
        ASSERT_TRUE(v3.capacity() == v3_cap);  // Resize doesn't shrink capacity
        v3.shrink_to_fit();
        ASSERT_TRUE(v3.capacity() == v3.size());

        std::vector<NoDefaultCons> ref = {1, 2, 3};
        ASSERT_TRUE(HaveSameElements(v3, ref));
    }

    {
        // shrink to 0
        small_vector<NoDefaultCons, 2, size_t> v4 = {1, 2, 3, 4};
        v4.resize(0);
        ASSERT_TRUE(v4.capacity() == 4);  // Resize doesn't shrink capacity
        v4.shrink_to_fit();
        ASSERT_TRUE(v4.capacity() == 2);  // Small capacity is in the minimal
        std::vector<NoDefaultCons> ref = {};
        ASSERT_TRUE(HaveSameElements(v4, ref));
    }

    // Resize NoDefault small vector, not moving to small store
    // ---
    {
        // resize to current size
        small_vector<NoDefaultCons, 2, size_t> v6 = {1, 2, 3, 4};
        v6.resize(v6.size());
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElements(v6, ref));
    }

    {
        // growing resize
        small_vector<NoDefaultCons, 2, size_t> v7 = {1, 2, 3, 4};
        v7.resize(5);
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElementsUpTo(v7, ref, ref.size()));
    }

    {
        // shrinking resize
        small_vector<NoDefaultCons, 2, size_t> v8 = {1, 2, 3, 4};
        v8.resize(3);
        std::vector<NoDefaultCons> ref = {1, 2, 3};
        ASSERT_TRUE(HaveSameElements(v8, ref));
    }

    {
        // shrink to 0
        small_vector<NoDefaultCons, 2, size_t> v9 = {1, 2, 3, 4};
        v9.resize(0);
        std::vector<NoDefaultCons> ref = {};
        ASSERT_TRUE(HaveSameElements(v9, ref));
    }
}

TEST(CustomContainer, SmallVectorNotDefaultInsertableDefaultValue) {
    // Resize NoDefault small vector, moving to small store
    // ---
    {
        // resize to current size
        small_vector<NoDefaultCons, 2, size_t> v1 = {1, 2, 3, 4};
        v1.resize(v1.size(), NoDefaultCons(0));
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElements(v1, ref));
    }

    {
        // growing resize
        small_vector<NoDefaultCons, 2, size_t> v2 = {1, 2, 3, 4};
        v2.resize(5, NoDefaultCons(0));
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4, 0};
        ASSERT_TRUE(HaveSameElements(v2, ref));
    }

    {
        // shrinking resize
        small_vector<NoDefaultCons, 2, size_t> v3 = {1, 2, 3, 4};
        v3.resize(3, NoDefaultCons(0));
        v3.shrink_to_fit();
        ASSERT_TRUE(v3.capacity() == v3.size());
        std::vector<NoDefaultCons> ref = {1, 2, 3};
        ASSERT_TRUE(HaveSameElements(v3, ref));
    }

    {
        // shrink to 0
        small_vector<NoDefaultCons, 2, size_t> v4 = {1, 2, 3, 4};
        v4.resize(0, NoDefaultCons(0));
        ASSERT_TRUE(v4.capacity() == 4);  // Resize doesn't shrink capacity
        v4.shrink_to_fit();
        ASSERT_TRUE(v4.capacity() == 2);  // Small capacity is in the minimal
        std::vector<NoDefaultCons> ref = {};
        ASSERT_TRUE(HaveSameElements(v4, ref));
    }

    // Resize NoDefault small vector, not moving to small store
    // ---
    {
        // resize to current size
        small_vector<NoDefaultCons, 2, size_t> v6 = {1, 2, 3, 4};
        v6.resize(v6.size());
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4};
        ASSERT_TRUE(HaveSameElements(v6, ref));
    }

    {
        // growing resize
        small_vector<NoDefaultCons, 2, size_t> v7 = {1, 2, 3, 4};
        v7.resize(5, NoDefaultCons(0));
        std::vector<NoDefaultCons> ref = {1, 2, 3, 4, 0};
        ASSERT_TRUE(HaveSameElements(v7, ref));
    }

    {
        // shrinking resize
        small_vector<NoDefaultCons, 2, size_t> v8 = {1, 2, 3, 4};
        v8.resize(3, NoDefaultCons(0));
        ASSERT_TRUE(v8.capacity() == 4);  // Resize doesn't shrink capacity
        std::vector<NoDefaultCons> ref = {1, 2, 3};
        ASSERT_TRUE(HaveSameElements(v8, ref));
    }

    {
        // shrink to 0
        small_vector<NoDefaultCons, 2, size_t> v9 = {1, 2, 3, 4};
        v9.resize(0, NoDefaultCons(0));
        ASSERT_TRUE(v9.capacity() == 4);  // Resize doesn't shrink capacity
        std::vector<NoDefaultCons> ref = {};
        ASSERT_TRUE(HaveSameElements(v9, ref));
    }
}
TEST(CustomContainer, SmallVectorConstruct) {
    using SmallVector = small_vector<std::string, 5, size_t>;
    const SmallVector ref_small = {"one", "two", "three", "four"};
    SmallVector ref_large = {"one", "two", "three", "four", "five", "six"};

    // Small construct and emplace vs. list (tests list contruction, really)
    SmallVector v_small_emplace;
    v_small_emplace.emplace_back("one");
    v_small_emplace.emplace_back("two");
    v_small_emplace.emplace_back("three");
    v_small_emplace.emplace_back("four");
    ASSERT_TRUE(HaveSameElements(ref_small, v_small_emplace));

    // Copy construct from small_store
    SmallVector v_small_copy(ref_small);
    ASSERT_TRUE(HaveSameElements(ref_small, v_small_copy));

    // Move construct from small_store
    SmallVector v_small_move_src(ref_small);
    SmallVector v_small_move_dst(std::move(v_small_move_src));
    ASSERT_TRUE(HaveSameElements(ref_small, v_small_move_dst));

    // Small construct and emplace vs. list (tests list contruction, really)
    SmallVector v_large_emplace;
    v_large_emplace.emplace_back("one");
    v_large_emplace.emplace_back("two");
    v_large_emplace.emplace_back("three");
    v_large_emplace.emplace_back("four");
    v_large_emplace.emplace_back("five");
    v_large_emplace.emplace_back("six");
    ASSERT_TRUE(HaveSameElements(ref_large, v_large_emplace));

    // Copy construct from large_store
    SmallVector v_large_copy(ref_large);
    ASSERT_TRUE(HaveSameElements(ref_large, v_large_copy));

    // Move construct from large_store
    SmallVector v_large_move_src(ref_large);
    SmallVector v_large_move_dst(std::move(v_large_move_src));
    ASSERT_TRUE(HaveSameElements(ref_large, v_large_move_dst));
}

TEST(CustomContainer, SmallVectorAssign) {
    using SmallVector = small_vector<std::string, 5, size_t>;
    const SmallVector ref_xxs = {"one", "two"};
    const SmallVector ref_xs = {"one", "two", "three"};
    const SmallVector ref_small = {"one", "two", "three", "four"};

    const SmallVector ref_large = {"one", "two", "three", "four", "five", "six"};
    const SmallVector ref_xl = {"one", "two", "three", "four", "five", "six", "seven"};
    const SmallVector ref_xxl = {"one", "two", "three", "four", "five", "six", "seven", "eight"};

    SmallVector v_src(ref_large);
    SmallVector v_dst(ref_small);

    // Copy from large store to small store
    v_dst = v_src;
    ASSERT_TRUE(HaveSameElements(ref_large, v_src));
    ASSERT_TRUE(HaveSameElements(ref_large, v_dst));

    // Quick small to large check to reset...
    v_dst = ref_small;
    ASSERT_TRUE(HaveSameElements(ref_small, v_dst));

    // Copy from large store to small store
    v_dst = std::move(v_src);
    // Spec doesn't require src to be empty after move *assignment*
    ASSERT_TRUE(HaveSameElements(ref_large, v_dst));

    // Same store type copy/move

    // Small
    //
    // Copy small to small reducing
    v_src = ref_xs;
    v_dst = ref_small;
    v_dst = v_src;
    ASSERT_TRUE(HaveSameElements(ref_xs, v_src));
    ASSERT_TRUE(HaveSameElements(ref_xs, v_dst));

    // Move small to small reducing
    v_src = ref_xs;
    v_dst = ref_small;
    v_dst = std::move(v_src);
    // Small move operators don't empty source
    ASSERT_TRUE(HaveSameElements(ref_xs, v_dst));

    // Copy small to small increasing
    v_src = ref_small;
    v_dst = ref_xs;
    v_dst = v_src;
    ASSERT_TRUE(HaveSameElements(ref_small, v_src));
    ASSERT_TRUE(HaveSameElements(ref_small, v_dst));

    // Move small to small increasing
    v_src = ref_small;
    v_dst = ref_xs;
    v_dst = std::move(v_src);
    // Small move operators don't empty source
    ASSERT_TRUE(HaveSameElements(ref_small, v_dst));

    // Large
    //
    // Copy large to large reducing
    v_src = ref_large;
    v_dst = ref_xl;
    v_dst = v_src;
    ASSERT_TRUE(HaveSameElements(ref_large, v_src));
    ASSERT_TRUE(HaveSameElements(ref_large, v_dst));

    // Move large to large reducing
    v_src = ref_large;
    v_dst = ref_xl;
    v_dst = std::move(v_src);
    ASSERT_TRUE(v_src.empty());  // Since large moves move the large store, the source is empty, but not required by spec of vector
    ASSERT_TRUE(HaveSameElements(ref_large, v_dst));

    // Copy large to large increasing
    v_src = ref_xxl;
    v_dst = ref_xl;
    v_dst = v_src;
    ASSERT_TRUE(HaveSameElements(ref_xxl, v_src));
    ASSERT_TRUE(HaveSameElements(ref_xxl, v_dst));

    // Move large to large increasing
    v_src = ref_xxl;
    v_dst = ref_xl;
    v_dst = std::move(v_src);
    ASSERT_TRUE(v_src.empty());
    ASSERT_TRUE(HaveSameElements(ref_xxl, v_dst));
}

TEST(CustomContainer, Enumerate) {
    small_vector<int, 2, size_t> sv = {1, 2, 3, 4};
    std::array ref_elements = {1, 2, 3, 4};
    size_t indices_i = 0;
    for (auto [i, x] : vvl::enumerate(sv)) {
        ASSERT_TRUE(i == indices_i);
        ASSERT_TRUE(x == ref_elements[indices_i]);
        ++indices_i;
    }
}

TEST(CustomContainer, EnumerateConst) {
    const std::vector<int> sv{1, 2, 3, 4};
    std::array ref_elements = {1, 2, 3, 4};
    size_t indices_i = 0;
    for (auto [i, x] : vvl::enumerate(sv)) {
        ASSERT_TRUE(i == indices_i);
        ASSERT_TRUE(x == ref_elements[indices_i]);
        ++indices_i;
    }
}