-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-fixed_vector.cpp
More file actions
918 lines (814 loc) · 28.1 KB
/
Copy pathtest-fixed_vector.cpp
File metadata and controls
918 lines (814 loc) · 28.1 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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
// SPDX-FileCopyrightText: Steven Ward
// SPDX-License-Identifier: MPL-2.0
#include "fixed_vector.hpp"
#include "test_utils.hpp"
#include <algorithm>
#include <array>
#include <compare>
#include <cstddef>
#include <iterator>
#include <new>
#include <numeric>
#include <ranges>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
constexpr auto is_odd = [](const int x) { return x % 2 != 0; };
// Compile-time check: nearly the whole interface is usable in constant expressions.
// Unlike the heap-backed siblings -- whose over-aligned allocation is not usable in constant
// evaluation, so their static_assert can only reach the empty/zero-capacity members --
// fixed_vector's in-place std::array storage imposes no such limit. A semantic regression in
// any member exercised here therefore fails the compile, not just the run.
// NOLINTBEGIN(readability-simplify-boolean-expr)
constexpr bool
constexpr_api_ok()
{
fixed_vector<int, 8> v;
if (!(v.is_empty() && v.size() == 0 && v.reserved_unused() == 8))
return false;
v.append_range({1, 2, 3});
v.push_back(4);
v.emplace_back(5);
if (!v.try_push_back(6))
return false;
if (!v.try_emplace_back(7))
return false;
v.unchecked_push_back(8);
if (!(v.is_full() && v.size() == 8))
return false;
if (v.try_push_back(9)) // full -> false, must not throw
return false;
if (!(v.front() == 1 && v.back() == 8 && v.at(2) == 3 && v[7] == 8))
return false;
v.assign_range({9, 9});
v.resize(4, 7);
if (!(v.size() == 4 && v[0] == 9 && v[1] == 9 && v[2] == 7 && v[3] == 7))
return false;
v.pop_back();
v.fill_size(1);
if (!(v.size() == 3 && v[0] == 1 && v[2] == 1))
return false;
v.clear();
// Never destroyed: clear() only reset size(), so the elements still read back.
if (!(v.is_empty() && v[0] == 1))
return false;
fixed_vector<int, 8> w{4, 5, 6};
swap(v, w); // hidden friend
if (!(v.size() == 3 && w.is_empty()))
return false;
v.swap(w); // member
if (!(w.size() == 3 && v.is_empty()))
return false;
return true;
}
// NOLINTEND(readability-simplify-boolean-expr)
static_assert(constexpr_api_ok());
// Compile-time check: zeroize_reserved_unused() is usable in constant expressions (the
// runtime explicit-zeroing path is replaced by value-assignment during constant evaluation).
constexpr bool
constexpr_zeroize_ok()
{
fixed_vector<int, 5> v;
v.fill_capacity(9);
v.resize(2); // the tail slots [2, 5) still hold 9
v.zeroize_reserved_unused();
// operator[] is capacity-based: the tail is now zero
return v.size() == 2 && v[0] == 9 && v[1] == 9 && v[2] == 0 && v[3] == 0 && v[4] == 0;
}
static_assert(constexpr_zeroize_ok());
// Compile-time check: reserve() and the capacity observers work in constant expressions too.
// NOLINTBEGIN(readability-simplify-boolean-expr)
constexpr bool
constexpr_capacity_ok()
{
fixed_vector<int, 5> v{1, 2, 3, 4};
if (!(v.capacity() == 5 && v.unreserved() == 0 && v.reserved_unused() == 1))
return false;
v.reserve(2); // below size(): truncates size(), destroys nothing
if (!(v.capacity() == 2 && v.size() == 2 && v.is_full() && v.unreserved() == 3))
return false;
v.reserve(5); // growing leaves the regained slots as they were
if (!(v.capacity() == 5 && v.size() == 2 && v[2] == 3 && v[3] == 4 && v[4] == 0))
return false;
v.reserve(0); // both empty and full
if (!(v.is_empty() && v.is_full() && v.reserved_unused() == 0 && v.unreserved() == 5))
return false;
v.reserve(5);
v.zeroize_unreserved(); // no-op: nothing is unreserved
return v[0] == 1 && v[4] == 0;
}
// NOLINTEND(readability-simplify-boolean-expr)
static_assert(constexpr_capacity_ok());
// max_size() is static here -- callable with no object. capacity() is not: it reports the
// current, run-time capacity, as in the heap-backed siblings.
static_assert(fixed_vector<int, 5>::max_size() == 5);
// Align defaults to max(alignof(std::size_t), alignof(T)) and is honored by the storage.
static_assert(alignof(fixed_vector<int, 5>) == alignof(std::size_t));
static_assert(alignof(fixed_vector<std::byte, 64, 32>) == 32);
// Contiguous-range conformance (what lets the std algorithms below work on it).
static_assert(std::ranges::contiguous_range<fixed_vector<int, 5>>);
static_assert(std::ranges::sized_range<fixed_vector<int, 5>>);
// ---- Constructors ----
static void
test_ctor_default()
{
const fixed_vector<int, 5> v;
CHECK(v.size() == 0);
CHECK(v.capacity() == 5);
CHECK(v.is_empty());
CHECK(!v.is_full());
CHECK(v.data() != nullptr); // in-place storage: never null, unlike the heap-backed siblings
}
static void
test_ctor_count()
{
// Creates count value-initialized elements -- unlike the heap-backed siblings, where X(n)
// reserves capacity n and starts empty.
const fixed_vector<int, 5> v(3);
CHECK(v.size() == 3);
CHECK(v.capacity() == 5);
CHECK(to_ivec(v) == std::vector({0, 0, 0}));
}
static void
test_ctor_count_value()
{
const fixed_vector<int, 5> v(3, 42);
CHECK(v.size() == 3);
CHECK(!v.is_full()); // capacity is N, not count
CHECK(to_ivec(v) == std::vector({42, 42, 42}));
}
static void
test_ctor_span()
{
constexpr std::array arr{1, 2, 3};
const fixed_vector<int, 5> v(std::span<const int>{arr});
CHECK(to_ivec(v) == std::vector({1, 2, 3}));
}
static void
test_ctor_iter_sentinel()
{
const std::vector src{1, 2, 3, 4};
const fixed_vector<int, 5> v(src.begin(), src.end());
CHECK(to_ivec(v) == std::vector({1, 2, 3, 4}));
}
static void
test_ctor_iter_count()
{
const std::vector src{1, 2, 3, 4};
const fixed_vector<int, 5> v(src.begin() + 1, 2);
CHECK(to_ivec(v) == std::vector({2, 3}));
}
static void
test_ctor_init_list()
{
const fixed_vector<int, 5> v{1, 2, 3};
CHECK(to_ivec(v) == std::vector({1, 2, 3}));
}
static void
test_ctor_from_range_sized()
{
const fixed_vector<int, 5> v(std::from_range, std::views::iota(1, 5));
CHECK(to_ivec(v) == std::vector({1, 2, 3, 4}));
}
static void
test_ctor_from_range_unsized()
{
// A filter_view is not a sized_range, so there is no up-front size check: the elements are
// appended one at a time. (It is still a *forward* range, so the heap-backed siblings would
// accept this source too -- what they reject is an input-only one, which fixed_vector takes
// because it never has to size an allocation. See test-dynamic_fixed_vector.cpp's
// istream_view static_assert for that line.)
const fixed_vector<int, 5> v(std::from_range, std::views::iota(1, 10) | std::views::filter(is_odd));
CHECK(to_ivec(v) == std::vector({1, 3, 5, 7, 9}));
}
static void
test_assign_init_list()
{
fixed_vector<int, 5> v(5, 1);
v = {7, 8, 9};
CHECK(to_ivec(v) == std::vector({7, 8, 9}));
}
// ---- Copy / move / swap ----
static void
test_copy_ctor()
{
fixed_vector<int, 5> a{1, 2, 3};
const fixed_vector<int, 5> b = a;
CHECK(to_ivec(a) == to_ivec(b));
a[0] = 99;
CHECK(b[0] == 1); // mutation of a does not affect b
}
static void
test_move_ctor()
{
fixed_vector<int, 5> a{1, 2, 3};
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
const fixed_vector<int, 5> b = std::move(a);
CHECK(to_ivec(b) == std::vector({1, 2, 3}));
// Copy and move are member-wise (defaulted): for a trivially copyable T a moved-from
// fixed_vector is left unchanged -- unlike the heap-backed siblings, where move
// construction transfers the buffer and leaves the source empty.
// NOLINTNEXTLINE(bugprone-use-after-move,hicpp-invalid-access-moved,clang-analyzer-cplusplus.Move)
CHECK(a.size() == 3);
CHECK(to_ivec(a) == std::vector({1, 2, 3}));
}
static void
test_copy_assign()
{
const fixed_vector<int, 5> a{1, 2, 3, 4, 5};
fixed_vector<int, 5> b;
b = a;
CHECK(to_ivec(b) == to_ivec(a));
}
static void
test_move_assign()
{
fixed_vector<int, 5> a{4, 5, 6};
fixed_vector<int, 5> b;
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
b = std::move(a);
CHECK(to_ivec(b) == std::vector({4, 5, 6}));
}
static void
test_swap()
{
fixed_vector<int, 5> a{1, 2};
fixed_vector<int, 5> b{7, 8, 9};
swap(a, b); // hidden friend
CHECK(to_ivec(a) == std::vector({7, 8, 9}));
CHECK(to_ivec(b) == std::vector({1, 2}));
a.swap(b); // member
CHECK(to_ivec(a) == std::vector({1, 2}));
CHECK(to_ivec(b) == std::vector({7, 8, 9}));
}
static void
test_swap_exchanges_all_slots()
{
// swap exchanges all max_size() slots, not just the live elements.
fixed_vector<int, 5> a;
a.fill_capacity(4); // every slot, including the tail, holds 4
a.resize(2);
fixed_vector<int, 5> b{9};
swap(a, b);
CHECK(b[4] == 4); // b received a's tail slot
CHECK(a[4] == 0); // a received b's value-initialized tail
// The capacities are exchanged too, not just the sizes and the slots.
a.reserve(2);
swap(a, b);
CHECK(a.capacity() == 5);
CHECK(b.capacity() == 2);
}
// ---- Observers ----
static void
test_capacity_max_size()
{
const fixed_vector<int, 5> v{1, 2, 3};
CHECK(v.capacity() == 5); // capacity starts at N ...
CHECK(v.max_size() == 5); // ... and max_size() stays N forever
// max_size() is static: callable with no object. capacity() is not -- it is a run-time
// value that reserve() moves.
CHECK(fixed_vector<int, 5>::max_size() == 5);
}
static void
test_size_reserved_unused_is_empty_is_full()
{
fixed_vector<int, 3> v;
CHECK(v.is_empty());
CHECK(!v.is_full());
CHECK(v.size() == 0);
CHECK(v.reserved_unused() == 3);
CHECK(v.unreserved() == 0);
v.push_back(1);
CHECK(!v.is_empty());
CHECK(!v.is_full());
CHECK(v.size() == 1);
CHECK(v.reserved_unused() == 2);
v.push_back(2);
v.push_back(3);
CHECK(v.is_full());
CHECK(v.size() == 3);
CHECK(v.reserved_unused() == 0);
CHECK(v.unreserved() == 0);
// reserved_unused() and unreserved() split [size(), max_size()) at capacity().
v.reserve(2);
CHECK(v.size() == 2);
CHECK(v.reserved_unused() == 0);
CHECK(v.unreserved() == 1);
v.pop_back();
CHECK(v.reserved_unused() == 1);
CHECK(v.unreserved() == 1);
}
static void
test_reserve()
{
fixed_vector<int, 5> v{1, 2, 3, 4};
// Shrinking above size(): capacity is the limit every space check consults.
v.reserve(4);
CHECK(v.capacity() == 4);
CHECK(v.max_size() == 5); // max_size() is unaffected
CHECK(v.is_full());
CHECK(v.reserved_unused() == 0);
CHECK(v.unreserved() == 1);
CHECK_THROWS(std::bad_alloc, v.push_back(5));
CHECK_THROWS(std::bad_alloc, v.emplace_back(5));
CHECK_THROWS(std::bad_alloc, v.append_range({5}));
CHECK(!v.try_push_back(5));
CHECK(to_ivec(v) == std::vector({1, 2, 3, 4}));
// Shrinking below size() truncates size(); nothing is destroyed.
v.reserve(2);
CHECK(v.capacity() == 2);
CHECK(v.size() == 2);
CHECK(to_ivec(v) == std::vector({1, 2}));
// Growing leaves the regained slots untouched -- 3 and 4 are still there.
v.reserve(5);
CHECK(v.capacity() == 5);
CHECK(v.size() == 2);
CHECK(v[2] == 3);
CHECK(v[3] == 4);
CHECK(v[4] == 0); // never written
v.push_back(30);
CHECK(to_ivec(v) == std::vector({1, 2, 30}));
// Zero capacity is both empty and full, as in the heap-backed siblings.
v.reserve(0);
CHECK(v.is_empty());
CHECK(v.is_full());
CHECK(v.reserved_unused() == 0);
CHECK(v.unreserved() == 5);
CHECK(!v.try_push_back(1));
// Past max_size() is capacity overflow, like every other space failure.
CHECK_THROWS(std::bad_alloc, v.reserve(6));
CHECK(v.capacity() == 0); // unchanged by the failed call
}
// ---- Modifiers ----
static void
test_clear()
{
fixed_vector<int, 5> v{1, 2, 3};
v.clear();
CHECK(v.is_empty());
CHECK(v.capacity() == 5);
// clear() only resets size(); operator[] is capacity-based, so the former elements still
// read back.
CHECK(v[0] == 1);
CHECK(v[1] == 2);
CHECK(v[2] == 3);
}
static void
test_resize()
{
fixed_vector<int, 5> v;
v.resize(3, 7); // grow with a value
CHECK(to_ivec(v) == std::vector({7, 7, 7}));
v.resize(1); // shrink, no destruction
CHECK(to_ivec(v) == std::vector({7}));
v.resize(4); // grow with T{} == 0
CHECK(to_ivec(v) == std::vector({7, 0, 0, 0}));
// Bounded by capacity(), not max_size(): resize does not implicitly reserve.
v.reserve(4);
CHECK_THROWS(std::bad_alloc, v.resize(5));
CHECK(v.size() == 4);
v.reserve(5);
v.resize(5); // now it fits
CHECK(v.size() == 5);
}
static void
test_pop_back()
{
fixed_vector<int, 5> v{1, 2, 3};
v.pop_back();
CHECK(to_ivec(v) == std::vector({1, 2}));
CHECK(v[2] == 3); // not destroyed, just outside size()
v.pop_back();
v.pop_back();
v.pop_back(); // pop on empty is a no-op
CHECK(v.is_empty());
}
static void
test_push_back()
{
fixed_vector<int, 5> v;
const int x = 10;
v.push_back(x); // const&
v.push_back(20); // &&
CHECK(to_ivec(v) == std::vector({10, 20}));
}
static void
test_emplace_back()
{
fixed_vector<int, 2> v;
v.emplace_back(5);
v.emplace_back(6);
CHECK(to_ivec(v) == std::vector({5, 6}));
}
static void
test_unchecked_push_back_unchecked_emplace_back()
{
fixed_vector<int, 4> v;
v.unchecked_emplace_back(1);
const int x = 2;
v.unchecked_push_back(x); // const&
v.unchecked_push_back(3); // &&
int y = 4;
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
v.unchecked_push_back(std::move(y)); // &&
CHECK(to_ivec(v) == std::vector({1, 2, 3, 4}));
CHECK(v.is_full());
}
static void
test_try_push_back_try_emplace_back()
{
fixed_vector<int, 3> v;
const int x = 1;
CHECK(v.try_push_back(x)); // const&
CHECK(v.try_push_back(2)); // &&
CHECK(v.try_emplace_back(3));
CHECK(v.is_full());
// Full -> false, no throw.
CHECK(!v.try_push_back(x)); // const&
CHECK(!v.try_push_back(4)); // &&
CHECK(!v.try_emplace_back(5));
CHECK(to_ivec(v) == std::vector({1, 2, 3}));
}
static void
test_fill_capacity_fill_size()
{
fixed_vector<int, 5> v;
v.append_range({1, 2, 3});
v.fill_size(9); // only the live [0,size)
CHECK(to_ivec(v) == std::vector({9, 9, 9}));
CHECK(!v.is_full());
v.fill_capacity(4); // whole capacity, size := capacity()
CHECK(to_ivec(v) == std::vector({4, 4, 4, 4, 4}));
CHECK(v.is_full());
// fill_capacity() stops at capacity(), not max_size(): the unreserved slots keep their 4s.
v.reserve(3);
v.fill_capacity(7);
CHECK(v.size() == 3);
CHECK(to_ivec(v) == std::vector({7, 7, 7}));
v.reserve(5);
CHECK(v[3] == 4);
CHECK(v[4] == 4);
// resize(capacity(), value) is the tail-only counterpart of fill_capacity(): it fills
// [size(), capacity()) and grows into it, leaving the live elements as they are.
v.resize(v.capacity(), 6);
CHECK(to_ivec(v) == std::vector({7, 7, 7, 6, 6}));
CHECK(v.is_full());
}
static void
test_zeroize_reserved_unused()
{
fixed_vector<int, 5> v;
v.fill_capacity(9);
v.resize(2); // the tail slots [2, 5) still hold 9
v.zeroize_reserved_unused();
CHECK(v.size() == 2);
CHECK(to_ivec(v) == std::vector({9, 9}));
// operator[] is capacity-based: the tail is now zero
// NOLINTNEXTLINE(readability-static-accessed-through-instance)
for (std::size_t i = v.size(); i < v.max_size(); ++i)
CHECK(v[i] == 0);
// Scrub the whole array: clear() + zeroize_reserved_unused() (non-elidable stores). This
// reaches every slot only because capacity() is still max_size(); see the test below.
v.clear();
v.zeroize_reserved_unused();
CHECK(v.is_empty());
// NOLINTNEXTLINE(readability-static-accessed-through-instance)
for (std::size_t i = 0; i < v.max_size(); ++i)
CHECK(v[i] == 0);
}
static void
test_zeroize_unreserved()
{
fixed_vector<int, 5> v;
v.fill_capacity(9); // every slot holds 9
v.reserve(3); // slots [3, 5) are now unreserved, still holding 9
CHECK(v.size() == 3);
v.zeroize_unreserved();
CHECK(to_ivec(v) == std::vector({9, 9, 9})); // the reserved half is untouched
v.reserve(5); // regain the slots to read them
CHECK(v[3] == 0);
CHECK(v[4] == 0);
// A no-op while nothing is unreserved.
v.fill_capacity(8);
v.zeroize_unreserved();
CHECK(to_ivec(v) == std::vector({8, 8, 8, 8, 8}));
// The whole array, capacity reduced: clear() + both halves.
v.reserve(2);
v.clear();
v.zeroize_reserved_unused();
v.zeroize_unreserved();
v.reserve(5);
// NOLINTNEXTLINE(readability-static-accessed-through-instance)
for (std::size_t i = 0; i < v.max_size(); ++i)
CHECK(v[i] == 0);
}
// ---- append_range / try_append_range / assign_range ----
static void
test_append_range()
{
constexpr std::array tail{4, 5};
const std::vector more{6, 7};
fixed_vector<int, 12> v;
v.append_range({1, 2, 3}); // initializer_list
v.append_range(std::span<const int>{tail}); // span
v.append_range(more.begin(), more.end()); // iterator + sentinel
v.append_range(more.begin(), std::size_t{1}); // iterator + count -> 6
v.append_range(std::views::iota(8, 10)); // range -> 8,9
CHECK(to_ivec(v) == std::vector({1, 2, 3, 4, 5, 6, 7, 6, 8, 9}));
}
static void
test_append_range_unsized_partial()
{
// No up-front size check is possible for an unsized source, so the elements that fit are
// appended before std::bad_alloc is thrown (the sized overloads are all-or-nothing).
fixed_vector<int, 4> v;
v.append_range({1, 2});
CHECK_THROWS(std::bad_alloc, v.append_range(std::views::iota(1, 10) | std::views::filter(is_odd)));
CHECK(to_ivec(v) == std::vector({1, 2, 1, 3})); // partially appended before the throw
}
static void
test_try_append_range()
{
constexpr std::array a{1, 2};
const std::vector more{5, 6};
fixed_vector<int, 4> v;
CHECK(v.try_append_range(std::span<const int>{a})); // span
CHECK(v.try_append_range({3, 4})); // initializer_list
CHECK(!v.try_append_range({5, 6})); // would overflow -> false
CHECK(!v.try_append_range(std::views::iota(0, 3))); // sized range: checked up front
CHECK(!v.try_append_range(more.begin(), more.end())); // sized sentinel: checked up front
CHECK(!v.try_append_range(more.begin(), std::size_t{2})); // iterator + count
CHECK(to_ivec(v) == std::vector({1, 2, 3, 4})); // nothing appended by the failures
}
static void
test_try_append_range_unsized_partial()
{
fixed_vector<int, 4> v;
v.append_range({1, 2});
// filter_view is not sized: the elements that fit land before false is returned.
CHECK(!v.try_append_range(std::views::iota(1, 10) | std::views::filter(is_odd)));
CHECK(to_ivec(v) == std::vector({1, 2, 1, 3}));
}
static void
test_assign_range()
{
constexpr std::array arr{5, 6};
const std::vector src{7, 8, 9};
fixed_vector<int, 6> v;
v.append_range({1, 2, 3});
v.assign_range(std::span<const int>{arr}); // span
CHECK(to_ivec(v) == std::vector({5, 6}));
v.assign_range(src.begin(), src.end()); // iterator + sentinel
CHECK(to_ivec(v) == std::vector({7, 8, 9}));
v.assign_range(src.begin(), std::size_t{2}); // iterator + count
CHECK(to_ivec(v) == std::vector({7, 8}));
v.assign_range({1, 1}); // initializer_list
CHECK(to_ivec(v) == std::vector({1, 1}));
v.assign_range(std::views::iota(10, 13)); // range
CHECK(to_ivec(v) == std::vector({10, 11, 12}));
}
static void
test_assign_range_unsized_partial()
{
// assign_range is clear() + append_range, so it inherits the unsized source's partial
// append: the clear() has already run when the throw arrives, and the elements that fit
// are already in place.
fixed_vector<int, 4> v{9, 9, 9, 9};
CHECK_THROWS(std::bad_alloc,
v.assign_range(std::views::iota(1, 10) | std::views::filter(is_odd)));
CHECK(to_ivec(v) == std::vector({1, 3, 5, 7})); // not empty -- what fit survived the throw
// The sized counterpart, for contrast: checked up front, so it throws before writing.
fixed_vector<int, 4> w{9, 9, 9, 9};
CHECK_THROWS(std::bad_alloc, w.assign_range({1, 2, 3, 4, 5}));
CHECK(w.is_empty());
CHECK(w[0] == 9); // nothing was written; the old elements are alive, just outside size()
}
// ---- Element access ----
static void
test_span_and_data()
{
fixed_vector<int, 5> v{1, 2, 3, 4};
const std::span<const int> s1 = v.span();
const auto s2 = static_cast<std::span<int>>(v); // operator std::span<T>
CHECK(s1.size() == 4);
CHECK(s2.size() == 4);
CHECK(v.data() == s1.data());
CHECK(v.data() == s2.data());
}
static void
test_front_back()
{
fixed_vector<int, 5> v{10, 20, 30};
CHECK(v.front() == 10);
CHECK(v.back() == 30);
v.front() = 11;
v.back() = 31;
CHECK(to_ivec(v) == std::vector({11, 20, 31}));
}
static void
test_operator_index()
{
fixed_vector<int, 5> v; // all 5 slots value-initialized to 0
v.append_range({11, 22, 33});
CHECK(v[0] == 11);
CHECK(v[2] == 33);
v[1] = 99;
CHECK(v[1] == 99);
// Indexes 3 and 4 are >= size() but < capacity(): live, value-initialized elements.
// Deterministic here, unlike aligned_byte_buffer, whose reserved tail is unspecified.
CHECK(v[3] == 0);
CHECK(v[4] == 0);
}
static void
test_at()
{
fixed_vector<int, 5> v{1, 2, 3};
CHECK(v.at(0) == 1);
CHECK(v.at(2) == 3);
v.at(1) = 99;
CHECK(v.at(1) == 99);
// at() is size-checked, so an index the unchecked operator[] would happily read throws.
CHECK_THROWS(std::out_of_range, (void)v.at(3));
}
static void
test_const_accessors()
{
const fixed_vector<int, 5> v{1, 2, 3};
CHECK(v.front() == 1);
CHECK(v.back() == 3);
CHECK(v[2] == 3);
CHECK(v.at(2) == 3);
CHECK(v.data() != nullptr);
CHECK(v.span().size() == 3);
CHECK(std::vector<int>(v.begin(), v.end()) == std::vector({1, 2, 3}));
CHECK(std::vector<int>(v.rbegin(), v.rend()) == std::vector({3, 2, 1}));
const auto s = static_cast<std::span<const int>>(v); // operator std::span<const T>
CHECK(s.size() == 3);
CHECK_THROWS(std::out_of_range, (void)v.at(3));
}
// ---- Iterators ----
static void
test_forward_iteration()
{
fixed_vector<int, 5> v{1, 2, 3};
int sum = 0;
for (const int e : v)
sum += e;
CHECK(sum == 6);
CHECK(std::vector<int>(v.begin(), v.end()) == std::vector({1, 2, 3}));
CHECK(std::vector<int>(v.cbegin(), v.cend()) == std::vector({1, 2, 3}));
*v.begin() = 10;
CHECK(to_ivec(v) == std::vector({10, 2, 3}));
}
static void
test_reverse_iteration()
{
fixed_vector<int, 5> v{1, 2, 3};
CHECK(std::vector<int>(v.rbegin(), v.rend()) == std::vector({3, 2, 1}));
CHECK(std::vector<int>(v.crbegin(), v.crend()) == std::vector({3, 2, 1}));
*v.rbegin() = 30; // back
CHECK(to_ivec(v) == std::vector({1, 2, 30}));
}
static void
test_std_algorithms()
{
fixed_vector<int, 10> v{5, 2, 8, 1, 9, 3};
std::ranges::sort(v);
CHECK(to_ivec(v) == std::vector({1, 2, 3, 5, 8, 9}));
auto* const it = std::ranges::find(v, 8);
CHECK(it != v.end());
CHECK(std::distance(v.begin(), it) == 4);
CHECK(std::accumulate(v.begin(), v.end(), 0) == 28);
fixed_vector<int, 10> w(v.size());
std::ranges::transform(v, w.begin(), [](const int x) { return x * 2; });
CHECK(to_ivec(w) == std::vector({2, 4, 6, 10, 16, 18}));
}
// ---- Comparisons ----
static void
test_comparisons()
{
// N is part of the type, so comparison is between same-capacity vectors only.
const fixed_vector<int, 5> a{1, 2, 3};
fixed_vector<int, 5> b{1, 2, 3};
const fixed_vector<int, 5> c{1, 2, 4};
const fixed_vector<int, 5> d{1, 2};
CHECK(a == b);
CHECK(a != c);
CHECK(a < c);
CHECK(c > a);
CHECK(d < a); // a prefix compares less
CHECK((a <=> b) == std::strong_ordering::equal);
CHECK((d <=> a) == std::strong_ordering::less);
// Only the live [0,size) elements take part: the unused tail slots differ but are ignored.
b.fill_capacity(1);
b.assign_range({1, 2, 3});
CHECK(b[4] == 1);
CHECK(a[4] == 0);
CHECK(a == b);
}
// ---- Custom alignment ----
static void
test_alignment()
{
// Align honored for several values (alignas on the array storage).
const auto check_align = []<std::size_t A>()
{
fixed_vector<std::byte, 64, A> buf;
buf.resize(A); // make it non-empty
CHECK(is_aligned(buf.data(), A));
CHECK(alignof(decltype(buf)) == A);
};
check_align.template operator()<16>();
check_align.template operator()<32>();
check_align.template operator()<64>();
}
static void
test_byte_storage_for_simd()
{
fixed_vector<std::byte, 1024, 16> buf;
CHECK(buf.capacity() == 1024);
CHECK(buf.is_empty());
for (int i = 0; i < 16; ++i)
buf.push_back(to_byte(i));
const std::span<const std::byte> lane = buf.span();
CHECK(lane.size() == 16);
CHECK(is_aligned(lane.data(), 16));
// On a NEON target this span feeds a load directly, e.g.:
// const uint8x16_t v = vld1q_u8(reinterpret_cast<const uint8_t*>(lane.data()));
CHECK(std::to_integer<unsigned>(lane[0]) == 0);
CHECK(std::to_integer<unsigned>(lane[15]) == 15);
}
// ---- Overflow -> std::bad_alloc ----
static void
test_overflow_throws_bad_alloc()
{
static constexpr std::array too_many{1, 2, 3, 4, 5, 6};
// The count constructor creates count elements, so it can overflow N -- unlike the
// heap-backed siblings, where X(n) reserves capacity n and cannot.
CHECK_THROWS(std::bad_alloc, const fixed_vector<int, 5> v(6); (void)v);
CHECK_THROWS(std::bad_alloc, const fixed_vector<int, 5> v(6, 42); (void)v);
CHECK_THROWS(std::bad_alloc, const fixed_vector<int, 5> v(std::span<const int>{too_many}); (void)v);
CHECK_THROWS(std::bad_alloc, const fixed_vector<int, 5> v{1, 2, 3, 4, 5, 6}; (void)v);
CHECK_THROWS(std::bad_alloc, fixed_vector<int, 2> v{1, 2}; v.push_back(3));
CHECK_THROWS(std::bad_alloc, fixed_vector<int, 1> v{1}; v.emplace_back(2));
CHECK_THROWS(std::bad_alloc, fixed_vector<int, 2> v; v.append_range(std::span<const int>{too_many}));
CHECK_THROWS(std::bad_alloc, fixed_vector<int, 2> v; v.resize(3));
CHECK_THROWS(std::bad_alloc, fixed_vector<int, 2> v; v.reserve(3));
// Capacity, not max_size(), is what resize() may not exceed.
CHECK_THROWS(std::bad_alloc, fixed_vector<int, 2> v; v.reserve(1); v.resize(2));
CHECK_THROWS(std::bad_alloc, fixed_vector<int, 2> v; v.assign_range({1, 2, 3}));
}
int
main() // NOLINT(bugprone-exception-escape)
{
return run_tests([] {
test_ctor_default();
test_ctor_count();
test_ctor_count_value();
test_ctor_span();
test_ctor_iter_sentinel();
test_ctor_iter_count();
test_ctor_init_list();
test_ctor_from_range_sized();
test_ctor_from_range_unsized();
test_assign_init_list();
test_copy_ctor();
test_move_ctor();
test_copy_assign();
test_move_assign();
test_swap();
test_swap_exchanges_all_slots();
test_capacity_max_size();
test_size_reserved_unused_is_empty_is_full();
test_clear();
test_reserve();
test_resize();
test_pop_back();
test_push_back();
test_emplace_back();
test_unchecked_push_back_unchecked_emplace_back();
test_try_push_back_try_emplace_back();
test_fill_capacity_fill_size();
test_zeroize_reserved_unused();
test_zeroize_unreserved();
test_append_range();
test_append_range_unsized_partial();
test_try_append_range();
test_try_append_range_unsized_partial();
test_assign_range();
test_assign_range_unsized_partial();
test_span_and_data();
test_front_back();
test_operator_index();
test_at();
test_const_accessors();
test_forward_iteration();
test_reverse_iteration();
test_std_algorithms();
test_comparisons();
test_alignment();
test_byte_storage_for_simd();
test_overflow_throws_bad_alloc();
});
}