forked from palotasb/static_vector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.cpp
846 lines (730 loc) · 26.2 KB
/
tests.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
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
#include <palotasb/static_vector.hpp>
#include <algorithm>
#include <exception>
#include <iostream>
#include <string>
#include <tuple>
#include <set>
using namespace stlpb;
template<typename T>
std::ostream& print_cont(std::ostream& os, T const& v){
os << "[ ";
for (int x = 0; x < v.size(); x++)
{
os << v[x];
if (x != v.size() - 1)
{
os << ", ";
}
}
os << " ]";
return os;
}
template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, static_vector<T, N> const& v){
return print_cont(os, v);
}
template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, std::array<T, N> const& v){
return print_cont(os, v);
}
std::ostream& operator<<(std::ostream& os, char c){
return os.operator<<(+c);
}
void debug_print_impl()
{
}
template<typename First, typename... T>
void debug_print_impl(First const& f, T const&... t)
{
std::cerr << f;
if (sizeof...(T) != 0)
{
std::cerr << ", ";
}
debug_print_impl(t...);
}
template<typename... T>
void debug_print(T const&... t)
{
if (sizeof...(T) != 0)
{
std::cerr << "(";
}
debug_print_impl(t...);
if (sizeof...(T) != 0)
{
std::cerr << ")";
}
}
template<typename T>
auto debug_transform(T const& t)
{
return t;
}
template<typename... T>
void assert_failure(const char* expression, const char* file, long line, T const&... t) {
std::cerr << "Assertion failure: " << expression;
debug_print(t...);
std::cerr << " failed at " << file
<< ':' << line << ".\n";
std::exit(1); // Exit program early
}
#define ASSERT(e) ((e) ? true : (assert_failure(#e, __FILE__, __LINE__), false))
#define ASSERT_MESSAGE(e, ...) ((e) ? true \
: (assert_failure(#e " note (" #__VA_ARGS__ ") == ", __FILE__, __LINE__, __VA_ARGS__), false))
#define ASSERT_EQUAL(a, b) ASSERT_MESSAGE(a == b, a, b)
#define ASSERT_UNEQUAL(a, b) ASSERT_MESSAGE(a != b, a, b)
template<typename... T>
std::array<char, sizeof...(T)> make_c_array(T&&... t)
{
return { static_cast<char>(t)... };
}
// Self-referential object that tests whether copies are semantically correct,
// using the copy constructors of stored objects.
//
// It also stores a payload in order to allow us to treat it "like" an int or
// whatever in order to allow us to verify orders
template<typename T = char>
struct Copyable_t {
Copyable_t() : self(this) {
ASSERT(constructed_.find(this) == std::end(constructed_));
constructed_.insert(this);
}
template<typename U,
typename=std::enable_if_t<
std::is_same<std::decay_t<U>, T>::value
>
>
Copyable_t(U&& t) : Copyable_t() {
inner = std::forward<U>(t);
}
Copyable_t(const Copyable_t& other)
: self(other.verify() ? this : nullptr), inner(other.inner) {
ASSERT(constructed_.find(this) == std::end(constructed_));
constructed_.insert(this);
}
Copyable_t& operator=(const Copyable_t& other) {
self = other.verify() ? this : nullptr;
this->inner = other.inner;
return *this;
}
Copyable_t(Copyable_t&& other)
: Copyable_t(static_cast<const Copyable_t&>(other)) {}
Copyable_t& operator=(Copyable_t&& other) {
return (*this) = static_cast<const Copyable_t&>(other);
}
~Copyable_t() {
auto x = constructed_.find(this);
ASSERT(x != std::end(constructed_)
&& "Freeing already freed element or one that does not exist");
constructed_.erase(x);
}
bool verify() const noexcept { return self == this; }
static int constructed() noexcept { return constructed_.size(); }
friend bool operator==(Copyable_t const& c, T const& t)
{
return c.inner == t;
}
friend bool operator==(T const& t, Copyable_t const& c)
{
return c.inner == t;
}
friend std::ostream& operator<<(std::ostream& os, Copyable_t c){
os << "Copyable(" << c.inner << (c.verify() ? "" : " !") << ")";
return os;
}
private:
const Copyable_t* self;
T inner = {};
static std::set<Copyable_t*> constructed_;
};
template<typename T>
std::set<Copyable_t<T>*> Copyable_t<T>::constructed_;
using Copyable = Copyable_t<>;
// Self-referential object that tests whether moves are semantically correct.
template<typename T = char>
struct Movable_t {
Movable_t() : self(this) { constructed_++; }
template<typename U,
typename=std::enable_if_t<
std::is_same<std::decay_t<U>, T>::value
>
>
Movable_t(U&& t) : Movable_t() {
inner = std::forward<U>(t);
}
Movable_t(const Movable_t&) = delete;
Movable_t& operator=(const Movable_t&) = delete;
Movable_t(Movable_t&& other) : self(other.verify() ? this : nullptr),
inner(other.inner)
{
other.self = nullptr;
constructed_++;
}
Movable_t& operator=(Movable_t&& other) {
self = other.verify() ? this : nullptr;
inner = other.inner;
other.self = nullptr;
return *this;
}
~Movable_t() { constructed_--; }
bool verify() const noexcept { return self == this; }
static int constructed() noexcept { return constructed_; }
friend bool operator==(Movable_t const& c, T const& t)
{
return c.inner == t;
}
friend bool operator==(T const& t, Movable_t const& c)
{
return c.inner == t;
}
friend std::ostream& operator<<(std::ostream& os, Movable_t const& c){
os << "Movable(" << c.inner << (c.verify() ? "" : " !") << ")";
return os;
}
private:
const Movable_t* self;
T inner = {};
static int constructed_;
};
template<typename T>
int Movable_t<T>::constructed_ = 0;
using Movable = Movable_t<>;
template<typename T>
static_vector<T, 10> get_123_vector()
{
// Do the emplacing to allow for move only types
static_vector<T, 10> ret;
ret.emplace(std::end(ret), static_cast<char>(1));
ret.emplace(std::end(ret), static_cast<char>(2));
ret.emplace(std::end(ret), static_cast<char>(3));
return std::move(ret);
}
template<typename T>
static_vector<T, 10> get_full_vector()
{
// Do the emplacing to allow for move only types
static_vector<T, 10> ret;
ret.emplace(std::end(ret), static_cast<char>(1));
ret.emplace(std::end(ret), static_cast<char>(2));
ret.emplace(std::end(ret), static_cast<char>(3));
ret.emplace(std::end(ret), static_cast<char>(4));
ret.emplace(std::end(ret), static_cast<char>(5));
ret.emplace(std::end(ret), static_cast<char>(6));
ret.emplace(std::end(ret), static_cast<char>(7));
ret.emplace(std::end(ret), static_cast<char>(8));
ret.emplace(std::end(ret), static_cast<char>(9));
ret.emplace(std::end(ret), static_cast<char>(10));
return std::move(ret);
}
template<typename T>
static_vector<T, 10> get_mostly_full_vector()
{
// Do the emplacing to allow for move only types
static_vector<T, 10> ret;
ret.emplace(std::end(ret), static_cast<char>(1));
ret.emplace(std::end(ret), static_cast<char>(2));
ret.emplace(std::end(ret), static_cast<char>(3));
ret.emplace(std::end(ret), static_cast<char>(4));
ret.emplace(std::end(ret), static_cast<char>(5));
ret.emplace(std::end(ret), static_cast<char>(6));
ret.emplace(std::end(ret), static_cast<char>(7));
ret.emplace(std::end(ret), static_cast<char>(8));
ret.emplace(std::end(ret), static_cast<char>(9));
return std::move(ret);
}
template<typename T>
static_vector<T, 10> get_empty_vector()
{
return {};
}
template<typename V, typename F, size_t N>
void erase_single_test(V const& verify, int index,
F const& get_initial_vector, std::array<char, N> const& final_status)
{
auto v = get_initial_vector();
auto initial_size = v.size();
v.erase(v.begin() + index);
ASSERT_MESSAGE(
std::equal(v.begin(), v.end(), final_status.begin(), final_status.end()),
v, final_status);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify), v);
}
template<typename V, typename I, typename F, size_t N>
void insert_single_test(V const& verify, int index, char data, I const& insert,
F const& get_initial_vector, std::array<char, N> const& final_status)
{
auto v = get_initial_vector();
auto initial_size = v.size();
try
{
insert(v, v.begin() + index, data);
ASSERT_MESSAGE(initial_size + 1 <= 10 && "Should have thrown before getting here", v, initial_size);
}
catch (...)
{
ASSERT_MESSAGE(initial_size + 1 > 10, v, initial_size);
}
ASSERT_MESSAGE(
std::equal(v.begin(), v.end(), final_status.begin(), final_status.end()),
v, final_status);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify), v);
}
template<typename T, typename V, typename I>
void insert_single_tests(V const& verify_func, I const& insert, bool only_back = false)
{
if (!only_back)
{
insert_single_test(verify_func, 0, 100, insert, get_empty_vector<T>,
make_c_array(100));
insert_single_test(verify_func, 0, 100, insert, get_123_vector<T>,
make_c_array( 100, 1, 2, 3 ));
insert_single_test(verify_func, 1, 100, insert, get_123_vector<T>,
make_c_array( 1, 100, 2, 3 ));
insert_single_test(verify_func, 2, 100, insert, get_123_vector<T>,
make_c_array( 1, 2, 100, 3 ));
insert_single_test(verify_func, 0, 100, insert, get_full_vector<T>,
make_c_array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ));
insert_single_test(verify_func, 1, 100, insert, get_full_vector<T>,
make_c_array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ));
insert_single_test(verify_func, 2, 100, insert, get_full_vector<T>,
make_c_array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ));
}
insert_single_test(verify_func, 3, 100, insert, get_123_vector<T>,
make_c_array( 1, 2, 3, 100 ));
insert_single_test(verify_func, 10, 100, insert, get_full_vector<T>,
make_c_array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ));
}
template<typename V, typename F, size_t N1, size_t N2>
void insert_range_test(V const& verify, int index, std::array<char, N1> data,
F const& get_initial_vector, std::array<char, N2> const& final_status)
{
auto v = get_initial_vector();
auto initial_size = v.size();
try
{
v.insert(v.begin() + index, data.begin(), data.end());
ASSERT_MESSAGE(initial_size + N1 <= 10 && "Should have thrown before getting here", v, initial_size, N1);
}
catch (...)
{
ASSERT_MESSAGE(initial_size + N1 > 10, v, N1, initial_size);
}
ASSERT_MESSAGE(
std::equal(v.begin(), v.end(), final_status.begin(), final_status.end()),
v, final_status);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify), v);
}
template<typename V, typename F, size_t N2>
void insert_n_test(V const& verify, int index, char data, int count,
F const& get_initial_vector, std::array<char, N2> const& final_status)
{
auto v = get_initial_vector();
auto initial_size = v.size();
try
{
v.insert(v.begin() + index, count, data);
ASSERT_MESSAGE(initial_size + count <= 10 && "Should have thrown before getting here", v, initial_size, count);
}
catch (...)
{
ASSERT_MESSAGE(initial_size + count > 10, v, count, initial_size);
}
ASSERT_MESSAGE(
std::equal(v.begin(), v.end(), final_status.begin(), final_status.end()),
v, final_status);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify), v);
}
template<typename T, typename F>
void n_default_test(F const& verify_func, int n)
{
// "N copy of X" ctor
static_vector<T, 10> v(n);
ASSERT_EQUAL(v.size(), n);
for (auto const& x : v)
ASSERT_EQUAL(x, 0);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
template<typename T, typename F>
void n_copy_test(F const& verify_func, int n)
{
// "N copy of X" ctor
static_vector<T, 10> v(n, T(static_cast<char>(100)));
ASSERT_EQUAL(v.size(), n);
for (auto const& x : v)
ASSERT_EQUAL(x, 100);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
template<typename T, typename F, typename... L>
void initializer_list_test(F const& verify_func, L const&... l)
{
try
{
// Initializer list constructor
static_vector<T, 10> v{
T(static_cast<char>(l))...
};
ASSERT(sizeof...(l) <= 10 && "Should have thrown because size to large");
auto arry = make_c_array(l...);
ASSERT_MESSAGE(
std::equal(v.begin(), v.end(), arry.begin(), arry.end()), v, arry);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
catch (...)
{
ASSERT(sizeof...(l) > 10 && "Exception should not have been thrown");
}
}
template<typename T, typename F, typename... L>
void iterator_const_test(F const& verify_func, L const&... l)
{
try
{
auto arry = make_c_array(l...);
// Note that this works even for the move only type, because the
// original array is not of the same type as the value_type of the
// vector, so it is actually newly constructed objects being inserted
static_vector<T, 10> v{
std::begin(arry), std:end(arry)
};
ASSERT_MESSAGE(sizeof...(l) <= 10 && "Should have thrown because size to large", v);
ASSERT_MESSAGE(
std::equal(v.begin(), v.end(), arry.begin(), arry.end()), v, arry);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
catch (...)
{
ASSERT(sizeof...(l) > 10 && "Exception should not have been thrown");
}
}
// If the type is copyable
template<typename T, typename F>
void copyable_tests(std::true_type, F const& verify_func)
{
n_default_test<T>(verify_func, 0);
n_default_test<T>(verify_func, 3);
n_default_test<T>(verify_func, 10);
{
try
{
// "N default" ctor
static_vector<T, 10> v(11);
ASSERT_MESSAGE(false, v);
}
catch (...)
{
}
}
n_copy_test<T>(verify_func, 0);
n_copy_test<T>(verify_func, 3);
n_copy_test<T>(verify_func, 10);
{
try
{
// "N copy of X" ctor
static_vector<T, 10> v(11, T(static_cast<char>(100)));
ASSERT_MESSAGE(false, v);
}
catch (...)
{
}
}
initializer_list_test<T>(verify_func);
initializer_list_test<T>(verify_func, 1, 2, 3);
initializer_list_test<T>(verify_func, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
initializer_list_test<T>(verify_func, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
{
// Iterator constructor
char a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
static_vector<T, 10> v{std::begin(a), std::end(a)};
ASSERT_EQUAL(v.size(), 10);
int i = 1;
for (auto x : v)
{
ASSERT_EQUAL(x, i);
i++;
}
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
{
// Copy ctor
static_vector<T, 10> u{
T(static_cast<char>(1)),
T(static_cast<char>(2)),
T(static_cast<char>(3)),
T(static_cast<char>(4)),
T(static_cast<char>(5)),
T(static_cast<char>(6)),
T(static_cast<char>(7)),
T(static_cast<char>(8)),
T(static_cast<char>(9)),
T(static_cast<char>(10))
};
static_vector<T, 10> v{u};
ASSERT_EQUAL(v.size(), 10);
int i = 1;
for (auto x : v)
{
ASSERT_EQUAL(x, i);
i++;
}
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
{
// Copy assignment
static_vector<T, 10> u{
T(static_cast<char>(1)),
T(static_cast<char>(2)),
T(static_cast<char>(3)),
T(static_cast<char>(4)),
T(static_cast<char>(5)),
T(static_cast<char>(6)),
T(static_cast<char>(7)),
T(static_cast<char>(8)),
T(static_cast<char>(9)),
T(static_cast<char>(10))
};
static_vector<T, 10> v;
v = u;
ASSERT_EQUAL(v.size(), 10);
int i = 1;
for (auto x : v)
{
ASSERT_EQUAL(x, i);
i++;
}
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
insert_range_test(verify_func, 0, make_c_array(100, 101), get_empty_vector<T>,
make_c_array( 100, 101 ));
insert_n_test(verify_func, 0, 100, 2, get_empty_vector<T>, make_c_array( 100, 100));
insert_range_test(verify_func, 0, make_c_array(100, 101), get_123_vector<T>,
make_c_array( 100, 101, 1, 2, 3 ));
insert_n_test(verify_func, 0, 100, 2, get_123_vector<T>,
make_c_array( 100, 100, 1, 2, 3 ));
insert_range_test(verify_func, 1, make_c_array(100, 101), get_123_vector<T>,
make_c_array( 1, 100, 101, 2, 3 ));
insert_n_test(verify_func, 1, 100, 2, get_123_vector<T>,
make_c_array( 1, 100, 100, 2, 3 ));
insert_range_test(verify_func, 2, make_c_array(100, 101), get_123_vector<T>,
make_c_array( 1, 2, 100, 101, 3 ));
insert_n_test(verify_func, 2, 100, 2, get_123_vector<T>,
make_c_array( 1, 2, 100, 100, 3 ));
insert_range_test(verify_func, 3, make_c_array(100, 101), get_123_vector<T>,
make_c_array( 1, 2, 3, 100, 101 ));
insert_n_test(verify_func, 3, 100, 2, get_123_vector<T>,
make_c_array( 1, 2, 3, 100, 100 ));
insert_range_test(verify_func, 0, make_c_array(100, 101), get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_n_test(verify_func, 0, 100, 2, get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_range_test(verify_func, 1, make_c_array(100, 101), get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_n_test(verify_func, 1, 100, 2, get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_range_test(verify_func, 2, make_c_array(100, 101), get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_n_test(verify_func, 2, 100, 2, get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_range_test(verify_func, 10, make_c_array(100, 101), get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_n_test(verify_func, 10, 100, 2, get_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9,10));
insert_range_test(verify_func, 0, make_c_array(100, 101), get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
insert_n_test(verify_func, 0, 100, 2, get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
insert_range_test(verify_func, 1, make_c_array(100, 101), get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
insert_n_test(verify_func, 1, 100, 2, get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
insert_range_test(verify_func, 2, make_c_array(100, 101), get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
insert_n_test(verify_func, 2, 100, 2, get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
insert_range_test(verify_func, 10, make_c_array(100, 101), get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
insert_n_test(verify_func, 10, 100, 2, get_mostly_full_vector<T>,
make_c_array(1,2,3,4,5,6,7,8,9));
}
// If the type is move only
template<typename T, typename F>
void copyable_tests(std::false_type, F const& verify_func)
{
}
template<typename T, typename F, typename Copyable=std::is_copy_constructible<T>>
void generic_test(F const& verify_func)
{
{
// Default ctor; capacity
static_vector<T, 10> v;
ASSERT_EQUAL(v.capacity(), 10);
ASSERT_EQUAL(v.size(), 0);
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
{
// Move ctor
static_vector<T, 10> u;
u.emplace(std::end(u), T(static_cast<char>(1)));
u.emplace(std::end(u), T(static_cast<char>(2)));
u.emplace(std::end(u), T(static_cast<char>(3)));
u.emplace(std::end(u), T(static_cast<char>(4)));
u.emplace(std::end(u), T(static_cast<char>(5)));
u.emplace(std::end(u), T(static_cast<char>(6)));
u.emplace(std::end(u), T(static_cast<char>(7)));
u.emplace(std::end(u), T(static_cast<char>(8)));
u.emplace(std::end(u), T(static_cast<char>(9)));
u.emplace(std::end(u), T(static_cast<char>(10)));
static_vector<T, 10> v{ std::move(u) };
ASSERT_EQUAL(v.size(), 10);
int i = 1;
for (auto const& x : v)
{
ASSERT_EQUAL(x, i);
i++;
}
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
{
// Move assignment
static_vector<T, 10> u;
u.emplace(std::end(u), T(static_cast<char>(1)));
u.emplace(std::end(u), T(static_cast<char>(2)));
u.emplace(std::end(u), T(static_cast<char>(3)));
u.emplace(std::end(u), T(static_cast<char>(4)));
u.emplace(std::end(u), T(static_cast<char>(5)));
u.emplace(std::end(u), T(static_cast<char>(6)));
u.emplace(std::end(u), T(static_cast<char>(7)));
u.emplace(std::end(u), T(static_cast<char>(8)));
u.emplace(std::end(u), T(static_cast<char>(9)));
u.emplace(std::end(u), T(static_cast<char>(10)));
static_vector<T, 10> v;
v = std::move(u);
ASSERT_EQUAL(v.size(), 10);
int i = 1;
for (auto const& x : v)
{
ASSERT_EQUAL(x, i);
i++;
}
ASSERT_MESSAGE(std::all_of(v.begin(), v.end(), verify_func), v);
}
iterator_const_test<T>(verify_func);
iterator_const_test<T>(verify_func, 1,2,3,4,5,6,7,8,9,10);
iterator_const_test<T>(verify_func, 1,2,3,4,5,6,7,8,9,10,11);
iterator_const_test<T>(verify_func, 1,2,3,4,5,6,7,8,9,10,11,12);
insert_single_tests<T>(verify_func, [](auto& v, auto it, char data){
v.insert(it, data);
});
// This is not to test the total interface of emplace, just that it inserts
// properly....the other aspects of emplace are going to be tested
// elsewhere
insert_single_tests<T>(verify_func, [](auto& v, auto it, char data){
v.emplace(it, data);
});
insert_single_tests<T>(verify_func, [](auto& v, auto it, char data){
v.emplace_back(data);
}, true);
insert_single_tests<T>(verify_func, [](auto& v, auto it, char data){
v.push_back(data);
}, true);
erase_single_test(verify_func, 0, get_123_vector<T>, make_c_array(2,3));
erase_single_test(verify_func, 1, get_123_vector<T>, make_c_array(1,3));
erase_single_test(verify_func, 2, get_123_vector<T>, make_c_array(1,2));
copyable_tests<T>(Copyable{}, verify_func);
}
template<typename T>
void generic_test()
{
return generic_test<T>([](auto const& x) { return true; });
}
template<typename E>
void emplace_test(E const& emplace)
{
{
// Emplace element
static_vector<std::tuple<Movable, Copyable>, 10> v;
const Copyable c(char(12));
emplace(v, v.end(), Movable{char(42)}, c);
ASSERT_EQUAL(v.size(), 1);
ASSERT_EQUAL(std::get<0>(v[0]), 42);
ASSERT_EQUAL(std::get<1>(v[0]), 12);
ASSERT(std::get<0>(v[0]).verify());
ASSERT(std::get<1>(v[0]).verify());
}
{
static_vector<std::string, 10> v;
emplace(v, v.end(), 10, 'a');
ASSERT_EQUAL(v[0], "aaaaaaaaaa");
}
}
int main(int, char* []) {
//
try {
generic_test<char>();
generic_test<Copyable>([](auto const& x){ return x.verify(); });
generic_test<Movable>([](auto const& x){ return x.verify(); });
{
// Insert multiple copies of trivial types into middle
static_vector<int, 10> v{1, 2, 3};
v.insert(v.begin() + 1, 2, 100);
ASSERT_EQUAL(v.size(), 5);
ASSERT_EQUAL(v[0], 1);
ASSERT_EQUAL(v[1], 100);
ASSERT_EQUAL(v[2], 100);
ASSERT_EQUAL(v[3], 2);
ASSERT_EQUAL(v[4], 3);
// TODO add more exhaustive tests for this method
// TODO test return value
}
emplace_test([](auto& v, auto it, auto&&... data){
v.emplace(it, std::forward<decltype(data)>(data)...);
});
emplace_test([](auto& v, auto it, auto&&... data){
v.emplace_back(std::forward<decltype(data)>(data)...);
});
{
static_vector<Copyable, 10> v{3};
v.erase(v.begin() + 1);
ASSERT_EQUAL(v.size(), 2);
for (const auto& x : v)
ASSERT(x.verify());
}
{
// Test STL algorithm support: std::rotate
// Example code taken from:
static_vector<int, 20> v{2, 4, 2, 0, 5, 10, 7, 3, 7, 1};
static_vector<int, 20> w = v;
// insertion sort from:
// https://en.cppreference.com/w/cpp/algorithm/rotate
for (auto i = v.begin(); i != v.end(); ++i) {
std::rotate(std::upper_bound(v.begin(), i, *i), i, i + 1);
}
using std::begin;
using std::end;
std::sort(begin(w), end(w)); // regular sort
// see if the two sorts result in a different order
static_vector<bool, 20> z;
std::transform(
begin(v), end(v), begin(w), std::back_inserter(z),
std::equal_to<>{});
// check that equality values are there
ASSERT_EQUAL(z.size(), v.size());
// check that the two sorts produced the same result
ASSERT(std::all_of(begin(z), end(z), [](bool b) { return b; }));
}
// TODO test all public methods with all reasonable inputs including
// edge cases
} catch (std::exception& e) {
std::cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
{
// Check that all destructors ran properly
// This should be the last test case!
ASSERT_EQUAL(Copyable::constructed(), 0);
ASSERT_EQUAL(Movable::constructed(), 0);
}
return 0;
}