forked from abseil/abseil-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inlined_vector_test.cc
2025 lines (1774 loc) · 62.2 KB
/
inlined_vector_test.cc
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The Abseil Authors.
//
// 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
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/inlined_vector.h"
#include <algorithm>
#include <forward_list>
#include <iterator>
#include <list>
#include <memory>
#include <scoped_allocator>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/attributes.h"
#include "absl/base/internal/exception_testing.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/macros.h"
#include "absl/base/options.h"
#include "absl/container/internal/counting_allocator.h"
#include "absl/container/internal/test_instance_tracker.h"
#include "absl/hash/hash_testing.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
namespace {
using absl::container_internal::CountingAllocator;
using absl::test_internal::CopyableMovableInstance;
using absl::test_internal::CopyableOnlyInstance;
using absl::test_internal::InstanceTracker;
using testing::AllOf;
using testing::Each;
using testing::ElementsAre;
using testing::ElementsAreArray;
using testing::Eq;
using testing::Gt;
using testing::Pointwise;
using testing::PrintToString;
using IntVec = absl::InlinedVector<int, 8>;
MATCHER_P(SizeIs, n, "") {
return testing::ExplainMatchResult(n, arg.size(), result_listener);
}
MATCHER_P(CapacityIs, n, "") {
return testing::ExplainMatchResult(n, arg.capacity(), result_listener);
}
MATCHER_P(ValueIs, e, "") {
return testing::ExplainMatchResult(e, arg.value(), result_listener);
}
// TODO(bsamwel): Add support for movable-only types.
// Test fixture for typed tests on BaseCountedInstance derived classes, see
// test_instance_tracker.h.
template <typename T>
class InstanceTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(InstanceTest);
// A simple reference counted class to make sure that the proper elements are
// destroyed in the erase(begin, end) test.
class RefCounted {
public:
RefCounted(int value, int* count) : value_(value), count_(count) { Ref(); }
RefCounted(const RefCounted& v) : value_(v.value_), count_(v.count_) {
Ref();
}
~RefCounted() {
Unref();
count_ = nullptr;
}
friend void swap(RefCounted& a, RefCounted& b) {
using std::swap;
swap(a.value_, b.value_);
swap(a.count_, b.count_);
}
RefCounted& operator=(RefCounted v) {
using std::swap;
swap(*this, v);
return *this;
}
void Ref() const {
ABSL_RAW_CHECK(count_ != nullptr, "");
++(*count_);
}
void Unref() const {
--(*count_);
ABSL_RAW_CHECK(*count_ >= 0, "");
}
int value_;
int* count_;
};
using RefCountedVec = absl::InlinedVector<RefCounted, 8>;
// A class with a vtable pointer
class Dynamic {
public:
virtual ~Dynamic() {}
};
using DynamicVec = absl::InlinedVector<Dynamic, 8>;
// Append 0..len-1 to *v
template <typename Container>
static void Fill(Container* v, size_t len, int offset = 0) {
for (size_t i = 0; i < len; i++) {
v->push_back(static_cast<int>(i) + offset);
}
}
static IntVec Fill(size_t len, int offset = 0) {
IntVec v;
Fill(&v, len, offset);
return v;
}
TEST(IntVec, SimpleOps) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
const IntVec& cv = v; // const alias
Fill(&v, len);
EXPECT_EQ(len, v.size());
EXPECT_LE(len, v.capacity());
for (size_t i = 0; i < len; i++) {
EXPECT_EQ(static_cast<int>(i), v[i]);
EXPECT_EQ(static_cast<int>(i), v.at(i));
}
EXPECT_EQ(v.begin(), v.data());
EXPECT_EQ(cv.begin(), cv.data());
size_t counter = 0;
for (IntVec::iterator iter = v.begin(); iter != v.end(); ++iter) {
EXPECT_EQ(static_cast<int>(counter), *iter);
counter++;
}
EXPECT_EQ(counter, len);
counter = 0;
for (IntVec::const_iterator iter = v.begin(); iter != v.end(); ++iter) {
EXPECT_EQ(static_cast<int>(counter), *iter);
counter++;
}
EXPECT_EQ(counter, len);
counter = 0;
for (IntVec::const_iterator iter = v.cbegin(); iter != v.cend(); ++iter) {
EXPECT_EQ(static_cast<int>(counter), *iter);
counter++;
}
EXPECT_EQ(counter, len);
if (len > 0) {
EXPECT_EQ(0, v.front());
EXPECT_EQ(static_cast<int>(len - 1), v.back());
v.pop_back();
EXPECT_EQ(len - 1, v.size());
for (size_t i = 0; i < v.size(); ++i) {
EXPECT_EQ(static_cast<int>(i), v[i]);
EXPECT_EQ(static_cast<int>(i), v.at(i));
}
}
}
}
TEST(IntVec, PopBackNoOverflow) {
IntVec v = {1};
v.pop_back();
EXPECT_EQ(v.size(), 0u);
}
TEST(IntVec, AtThrows) {
IntVec v = {1, 2, 3};
EXPECT_EQ(v.at(2), 3);
ABSL_BASE_INTERNAL_EXPECT_FAIL(v.at(3), std::out_of_range,
"failed bounds check");
}
TEST(IntVec, ReverseIterator) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
size_t counter = len;
for (IntVec::reverse_iterator iter = v.rbegin(); iter != v.rend(); ++iter) {
counter--;
EXPECT_EQ(static_cast<int>(counter), *iter);
}
EXPECT_EQ(counter, 0u);
counter = len;
for (IntVec::const_reverse_iterator iter = v.rbegin(); iter != v.rend();
++iter) {
counter--;
EXPECT_EQ(static_cast<int>(counter), *iter);
}
EXPECT_EQ(counter, 0u);
counter = len;
for (IntVec::const_reverse_iterator iter = v.crbegin(); iter != v.crend();
++iter) {
counter--;
EXPECT_EQ(static_cast<int>(counter), *iter);
}
EXPECT_EQ(counter, 0u);
}
}
TEST(IntVec, Erase) {
for (size_t len = 1; len < 20; len++) {
for (size_t i = 0; i < len; ++i) {
IntVec v;
Fill(&v, len);
v.erase(v.begin() + i);
EXPECT_EQ(len - 1, v.size());
for (size_t j = 0; j < i; ++j) {
EXPECT_EQ(static_cast<int>(j), v[j]);
}
for (size_t j = i; j < len - 1; ++j) {
EXPECT_EQ(static_cast<int>(j + 1), v[j]);
}
}
}
}
TEST(IntVec, Hardened) {
IntVec v;
Fill(&v, 10);
EXPECT_EQ(v[9], 9);
#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
EXPECT_DEATH_IF_SUPPORTED(v[10], "");
EXPECT_DEATH_IF_SUPPORTED(v[static_cast<size_t>(-1)], "");
EXPECT_DEATH_IF_SUPPORTED(v.resize(v.max_size() + 1), "");
#endif
}
// At the end of this test loop, the elements between [erase_begin, erase_end)
// should have reference counts == 0, and all others elements should have
// reference counts == 1.
TEST(RefCountedVec, EraseBeginEnd) {
for (size_t len = 1; len < 20; ++len) {
for (size_t erase_begin = 0; erase_begin < len; ++erase_begin) {
for (size_t erase_end = erase_begin; erase_end <= len; ++erase_end) {
std::vector<int> counts(len, 0);
RefCountedVec v;
for (size_t i = 0; i < len; ++i) {
v.push_back(RefCounted(static_cast<int>(i), &counts[i]));
}
size_t erase_len = erase_end - erase_begin;
v.erase(v.begin() + erase_begin, v.begin() + erase_end);
EXPECT_EQ(len - erase_len, v.size());
// Check the elements before the first element erased.
for (size_t i = 0; i < erase_begin; ++i) {
EXPECT_EQ(static_cast<int>(i), v[i].value_);
}
// Check the elements after the first element erased.
for (size_t i = erase_begin; i < v.size(); ++i) {
EXPECT_EQ(static_cast<int>(i + erase_len), v[i].value_);
}
// Check that the elements at the beginning are preserved.
for (size_t i = 0; i < erase_begin; ++i) {
EXPECT_EQ(1, counts[i]);
}
// Check that the erased elements are destroyed
for (size_t i = erase_begin; i < erase_end; ++i) {
EXPECT_EQ(0, counts[i]);
}
// Check that the elements at the end are preserved.
for (size_t i = erase_end; i < len; ++i) {
EXPECT_EQ(1, counts[i]);
}
}
}
}
}
struct NoDefaultCtor {
explicit NoDefaultCtor(int) {}
};
struct NoCopy {
NoCopy() {}
NoCopy(const NoCopy&) = delete;
};
struct NoAssign {
NoAssign() {}
NoAssign& operator=(const NoAssign&) = delete;
};
struct MoveOnly {
MoveOnly() {}
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(MoveOnly&&) = default;
};
TEST(InlinedVectorTest, NoDefaultCtor) {
absl::InlinedVector<NoDefaultCtor, 1> v(10, NoDefaultCtor(2));
(void)v;
}
TEST(InlinedVectorTest, NoCopy) {
absl::InlinedVector<NoCopy, 1> v(10);
(void)v;
}
TEST(InlinedVectorTest, NoAssign) {
absl::InlinedVector<NoAssign, 1> v(10);
(void)v;
}
TEST(InlinedVectorTest, MoveOnly) {
absl::InlinedVector<MoveOnly, 2> v;
v.push_back(MoveOnly{});
v.push_back(MoveOnly{});
v.push_back(MoveOnly{});
v.erase(v.begin());
v.push_back(MoveOnly{});
v.erase(v.begin(), v.begin() + 1);
v.insert(v.begin(), MoveOnly{});
v.emplace(v.begin());
v.emplace(v.begin(), MoveOnly{});
}
TEST(InlinedVectorTest, Noexcept) {
EXPECT_TRUE(std::is_nothrow_move_constructible<IntVec>::value);
EXPECT_TRUE((std::is_nothrow_move_constructible<
absl::InlinedVector<MoveOnly, 2>>::value));
struct MoveCanThrow {
MoveCanThrow(MoveCanThrow&&) {}
};
EXPECT_EQ(absl::default_allocator_is_nothrow::value,
(std::is_nothrow_move_constructible<
absl::InlinedVector<MoveCanThrow, 2>>::value));
}
TEST(InlinedVectorTest, EmplaceBack) {
absl::InlinedVector<std::pair<std::string, int>, 1> v;
auto& inlined_element = v.emplace_back("answer", 42);
EXPECT_EQ(&inlined_element, &v[0]);
EXPECT_EQ(inlined_element.first, "answer");
EXPECT_EQ(inlined_element.second, 42);
auto& allocated_element = v.emplace_back("taxicab", 1729);
EXPECT_EQ(&allocated_element, &v[1]);
EXPECT_EQ(allocated_element.first, "taxicab");
EXPECT_EQ(allocated_element.second, 1729);
}
TEST(InlinedVectorTest, ShrinkToFitGrowingVector) {
absl::InlinedVector<std::pair<std::string, int>, 1> v;
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 1u);
v.emplace_back("answer", 42);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 1u);
v.emplace_back("taxicab", 1729);
EXPECT_GE(v.capacity(), 2u);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2u);
v.reserve(100);
EXPECT_GE(v.capacity(), 100u);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2u);
}
TEST(InlinedVectorTest, ShrinkToFitEdgeCases) {
{
absl::InlinedVector<std::pair<std::string, int>, 1> v;
v.emplace_back("answer", 42);
v.emplace_back("taxicab", 1729);
EXPECT_GE(v.capacity(), 2u);
v.pop_back();
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 1u);
EXPECT_EQ(v[0].first, "answer");
EXPECT_EQ(v[0].second, 42);
}
{
absl::InlinedVector<std::string, 2> v(100);
v.resize(0);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2u); // inlined capacity
}
{
absl::InlinedVector<std::string, 2> v(100);
v.resize(1);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2u); // inlined capacity
}
{
absl::InlinedVector<std::string, 2> v(100);
v.resize(2);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2u);
}
{
absl::InlinedVector<std::string, 2> v(100);
v.resize(3);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 3u);
}
}
TEST(IntVec, Insert) {
for (size_t len = 0; len < 20; len++) {
for (ptrdiff_t pos = 0; pos <= static_cast<ptrdiff_t>(len); pos++) {
{
// Single element
std::vector<int> std_v;
Fill(&std_v, len);
IntVec v;
Fill(&v, len);
std_v.insert(std_v.begin() + pos, 9999);
IntVec::iterator it = v.insert(v.cbegin() + pos, 9999);
EXPECT_THAT(v, ElementsAreArray(std_v));
EXPECT_EQ(it, v.cbegin() + pos);
}
{
// n elements
std::vector<int> std_v;
Fill(&std_v, len);
IntVec v;
Fill(&v, len);
IntVec::size_type n = 5;
std_v.insert(std_v.begin() + pos, n, 9999);
IntVec::iterator it = v.insert(v.cbegin() + pos, n, 9999);
EXPECT_THAT(v, ElementsAreArray(std_v));
EXPECT_EQ(it, v.cbegin() + pos);
}
{
// Iterator range (random access iterator)
std::vector<int> std_v;
Fill(&std_v, len);
IntVec v;
Fill(&v, len);
const std::vector<int> input = {9999, 8888, 7777};
std_v.insert(std_v.begin() + pos, input.cbegin(), input.cend());
IntVec::iterator it =
v.insert(v.cbegin() + pos, input.cbegin(), input.cend());
EXPECT_THAT(v, ElementsAreArray(std_v));
EXPECT_EQ(it, v.cbegin() + pos);
}
{
// Iterator range (forward iterator)
std::vector<int> std_v;
Fill(&std_v, len);
IntVec v;
Fill(&v, len);
const std::forward_list<int> input = {9999, 8888, 7777};
std_v.insert(std_v.begin() + pos, input.cbegin(), input.cend());
IntVec::iterator it =
v.insert(v.cbegin() + pos, input.cbegin(), input.cend());
EXPECT_THAT(v, ElementsAreArray(std_v));
EXPECT_EQ(it, v.cbegin() + pos);
}
{
// Iterator range (input iterator)
std::vector<int> std_v;
Fill(&std_v, len);
IntVec v;
Fill(&v, len);
std_v.insert(std_v.begin() + pos, {9999, 8888, 7777});
std::istringstream input("9999 8888 7777");
IntVec::iterator it =
v.insert(v.cbegin() + pos, std::istream_iterator<int>(input),
std::istream_iterator<int>());
EXPECT_THAT(v, ElementsAreArray(std_v));
EXPECT_EQ(it, v.cbegin() + pos);
}
{
// Initializer list
std::vector<int> std_v;
Fill(&std_v, len);
IntVec v;
Fill(&v, len);
std_v.insert(std_v.begin() + pos, {9999, 8888});
IntVec::iterator it = v.insert(v.cbegin() + pos, {9999, 8888});
EXPECT_THAT(v, ElementsAreArray(std_v));
EXPECT_EQ(it, v.cbegin() + pos);
}
}
}
}
TEST(RefCountedVec, InsertConstructorDestructor) {
// Make sure the proper construction/destruction happen during insert
// operations.
for (size_t len = 0; len < 20; len++) {
SCOPED_TRACE(len);
for (size_t pos = 0; pos <= len; pos++) {
SCOPED_TRACE(pos);
std::vector<int> counts(len, 0);
int inserted_count = 0;
RefCountedVec v;
for (size_t i = 0; i < len; ++i) {
SCOPED_TRACE(i);
v.push_back(RefCounted(static_cast<int>(i), &counts[i]));
}
EXPECT_THAT(counts, Each(Eq(1)));
RefCounted insert_element(9999, &inserted_count);
EXPECT_EQ(1, inserted_count);
v.insert(v.begin() + pos, insert_element);
EXPECT_EQ(2, inserted_count);
// Check that the elements at the end are preserved.
EXPECT_THAT(counts, Each(Eq(1)));
EXPECT_EQ(2, inserted_count);
}
}
}
TEST(IntVec, Resize) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
// Try resizing up and down by k elements
static const int kResizeElem = 1000000;
for (size_t k = 0; k < 10; k++) {
// Enlarging resize
v.resize(len + k, kResizeElem);
EXPECT_EQ(len + k, v.size());
EXPECT_LE(len + k, v.capacity());
for (size_t i = 0; i < len + k; i++) {
if (i < len) {
EXPECT_EQ(static_cast<int>(i), v[i]);
} else {
EXPECT_EQ(kResizeElem, v[i]);
}
}
// Shrinking resize
v.resize(len, kResizeElem);
EXPECT_EQ(len, v.size());
EXPECT_LE(len, v.capacity());
for (size_t i = 0; i < len; i++) {
EXPECT_EQ(static_cast<int>(i), v[i]);
}
}
}
}
TEST(IntVec, InitWithLength) {
for (size_t len = 0; len < 20; len++) {
IntVec v(len, 7);
EXPECT_EQ(len, v.size());
EXPECT_LE(len, v.capacity());
for (size_t i = 0; i < len; i++) {
EXPECT_EQ(7, v[i]);
}
}
}
TEST(IntVec, CopyConstructorAndAssignment) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
EXPECT_EQ(len, v.size());
EXPECT_LE(len, v.capacity());
IntVec v2(v);
EXPECT_TRUE(v == v2) << PrintToString(v) << PrintToString(v2);
for (size_t start_len = 0; start_len < 20; start_len++) {
IntVec v3;
Fill(&v3, start_len, 99); // Add dummy elements that should go away
v3 = v;
EXPECT_TRUE(v == v3) << PrintToString(v) << PrintToString(v3);
}
}
}
TEST(IntVec, AliasingCopyAssignment) {
for (size_t len = 0; len < 20; ++len) {
IntVec original;
Fill(&original, len);
IntVec dup = original;
dup = *&dup;
EXPECT_EQ(dup, original);
}
}
TEST(IntVec, MoveConstructorAndAssignment) {
for (size_t len = 0; len < 20; len++) {
IntVec v_in;
const size_t inlined_capacity = v_in.capacity();
Fill(&v_in, len);
EXPECT_EQ(len, v_in.size());
EXPECT_LE(len, v_in.capacity());
{
IntVec v_temp(v_in);
auto* old_data = v_temp.data();
IntVec v_out(std::move(v_temp));
EXPECT_TRUE(v_in == v_out) << PrintToString(v_in) << PrintToString(v_out);
if (v_in.size() > inlined_capacity) {
// Allocation is moved as a whole, data stays in place.
EXPECT_TRUE(v_out.data() == old_data);
} else {
EXPECT_FALSE(v_out.data() == old_data);
}
}
for (size_t start_len = 0; start_len < 20; start_len++) {
IntVec v_out;
Fill(&v_out, start_len, 99); // Add dummy elements that should go away
IntVec v_temp(v_in);
auto* old_data = v_temp.data();
v_out = std::move(v_temp);
EXPECT_TRUE(v_in == v_out) << PrintToString(v_in) << PrintToString(v_out);
if (v_in.size() > inlined_capacity) {
// Allocation is moved as a whole, data stays in place.
EXPECT_TRUE(v_out.data() == old_data);
} else {
EXPECT_FALSE(v_out.data() == old_data);
}
}
}
}
class NotTriviallyDestructible {
public:
NotTriviallyDestructible() : p_(new int(1)) {}
explicit NotTriviallyDestructible(int i) : p_(new int(i)) {}
NotTriviallyDestructible(const NotTriviallyDestructible& other)
: p_(new int(*other.p_)) {}
NotTriviallyDestructible& operator=(const NotTriviallyDestructible& other) {
p_ = absl::make_unique<int>(*other.p_);
return *this;
}
bool operator==(const NotTriviallyDestructible& other) const {
return *p_ == *other.p_;
}
private:
std::unique_ptr<int> p_;
};
TEST(AliasingTest, Emplace) {
for (size_t i = 2; i < 20; ++i) {
absl::InlinedVector<NotTriviallyDestructible, 10> vec;
for (size_t j = 0; j < i; ++j) {
vec.push_back(NotTriviallyDestructible(static_cast<int>(j)));
}
vec.emplace(vec.begin(), vec[0]);
EXPECT_EQ(vec[0], vec[1]);
vec.emplace(vec.begin() + i / 2, vec[i / 2]);
EXPECT_EQ(vec[i / 2], vec[i / 2 + 1]);
vec.emplace(vec.end() - 1, vec.back());
EXPECT_EQ(vec[vec.size() - 2], vec.back());
}
}
TEST(AliasingTest, InsertWithCount) {
for (size_t i = 1; i < 20; ++i) {
absl::InlinedVector<NotTriviallyDestructible, 10> vec;
for (size_t j = 0; j < i; ++j) {
vec.push_back(NotTriviallyDestructible(static_cast<int>(j)));
}
for (size_t n = 0; n < 5; ++n) {
// We use back where we can because it's guaranteed to become invalidated
vec.insert(vec.begin(), n, vec.back());
auto b = vec.begin();
EXPECT_TRUE(
std::all_of(b, b + n, [&vec](const NotTriviallyDestructible& x) {
return x == vec.back();
}));
auto m_idx = vec.size() / 2;
vec.insert(vec.begin() + m_idx, n, vec.back());
auto m = vec.begin() + m_idx;
EXPECT_TRUE(
std::all_of(m, m + n, [&vec](const NotTriviallyDestructible& x) {
return x == vec.back();
}));
// We want distinct values so the equality test is meaningful,
// vec[vec.size() - 1] is also almost always invalidated.
auto old_e = vec.size() - 1;
auto val = vec[old_e];
vec.insert(vec.end(), n, vec[old_e]);
auto e = vec.begin() + old_e;
EXPECT_TRUE(std::all_of(
e, e + n,
[&val](const NotTriviallyDestructible& x) { return x == val; }));
}
}
}
TEST(OverheadTest, Storage) {
// Check for size overhead.
// In particular, ensure that std::allocator doesn't cost anything to store.
// The union should be absorbing some of the allocation bookkeeping overhead
// in the larger vectors, leaving only the size_ field as overhead.
struct T { void* val; };
size_t expected_overhead = sizeof(T);
EXPECT_EQ((2 * expected_overhead),
sizeof(absl::InlinedVector<T, 1>) - sizeof(T[1]));
EXPECT_EQ(expected_overhead,
sizeof(absl::InlinedVector<T, 2>) - sizeof(T[2]));
EXPECT_EQ(expected_overhead,
sizeof(absl::InlinedVector<T, 3>) - sizeof(T[3]));
EXPECT_EQ(expected_overhead,
sizeof(absl::InlinedVector<T, 4>) - sizeof(T[4]));
EXPECT_EQ(expected_overhead,
sizeof(absl::InlinedVector<T, 5>) - sizeof(T[5]));
EXPECT_EQ(expected_overhead,
sizeof(absl::InlinedVector<T, 6>) - sizeof(T[6]));
EXPECT_EQ(expected_overhead,
sizeof(absl::InlinedVector<T, 7>) - sizeof(T[7]));
EXPECT_EQ(expected_overhead,
sizeof(absl::InlinedVector<T, 8>) - sizeof(T[8]));
}
TEST(IntVec, Clear) {
for (size_t len = 0; len < 20; len++) {
SCOPED_TRACE(len);
IntVec v;
Fill(&v, len);
v.clear();
EXPECT_EQ(0u, v.size());
EXPECT_EQ(v.begin(), v.end());
}
}
TEST(IntVec, Reserve) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
for (size_t newlen = 0; newlen < 100; newlen++) {
const int* start_rep = v.data();
v.reserve(newlen);
const int* final_rep = v.data();
if (newlen <= len) {
EXPECT_EQ(start_rep, final_rep);
}
EXPECT_LE(newlen, v.capacity());
// Filling up to newlen should not change rep
while (v.size() < newlen) {
v.push_back(0);
}
EXPECT_EQ(final_rep, v.data());
}
}
}
TEST(StringVec, SelfRefPushBack) {
std::vector<std::string> std_v;
absl::InlinedVector<std::string, 4> v;
const std::string s = "A quite long string to ensure heap.";
std_v.push_back(s);
v.push_back(s);
for (int i = 0; i < 20; ++i) {
EXPECT_THAT(v, ElementsAreArray(std_v));
v.push_back(v.back());
std_v.push_back(std_v.back());
}
EXPECT_THAT(v, ElementsAreArray(std_v));
}
TEST(StringVec, SelfRefPushBackWithMove) {
std::vector<std::string> std_v;
absl::InlinedVector<std::string, 4> v;
const std::string s = "A quite long string to ensure heap.";
std_v.push_back(s);
v.push_back(s);
for (int i = 0; i < 20; ++i) {
EXPECT_EQ(v.back(), std_v.back());
v.push_back(std::move(v.back()));
std_v.push_back(std::move(std_v.back()));
}
EXPECT_EQ(v.back(), std_v.back());
}
TEST(StringVec, SelfMove) {
const std::string s = "A quite long string to ensure heap.";
for (int len = 0; len < 20; len++) {
SCOPED_TRACE(len);
absl::InlinedVector<std::string, 8> v;
for (int i = 0; i < len; ++i) {
SCOPED_TRACE(i);
v.push_back(s);
}
// Indirection necessary to avoid compiler warning.
v = std::move(*(&v));
// Ensure that the inlined vector is still in a valid state by copying it.
// We don't expect specific contents since a self-move results in an
// unspecified valid state.
std::vector<std::string> copy(v.begin(), v.end());
}
}
TEST(IntVec, Swap) {
for (size_t l1 = 0; l1 < 20; l1++) {
SCOPED_TRACE(l1);
for (size_t l2 = 0; l2 < 20; l2++) {
SCOPED_TRACE(l2);
IntVec a = Fill(l1, 0);
IntVec b = Fill(l2, 100);
{
using std::swap;
swap(a, b);
}
EXPECT_EQ(l1, b.size());
EXPECT_EQ(l2, a.size());
for (size_t i = 0; i < l1; i++) {
SCOPED_TRACE(i);
EXPECT_EQ(static_cast<int>(i), b[i]);
}
for (size_t i = 0; i < l2; i++) {
SCOPED_TRACE(i);
EXPECT_EQ(100 + static_cast<int>(i), a[i]);
}
}
}
}
TYPED_TEST_P(InstanceTest, Swap) {
using Instance = TypeParam;
using InstanceVec = absl::InlinedVector<Instance, 8>;
for (size_t l1 = 0; l1 < 20; l1++) {
SCOPED_TRACE(l1);
for (size_t l2 = 0; l2 < 20; l2++) {
SCOPED_TRACE(l2);
InstanceTracker tracker;
InstanceVec a, b;
const size_t inlined_capacity = a.capacity();
auto min_len = std::min(l1, l2);
auto max_len = std::max(l1, l2);
for (size_t i = 0; i < l1; i++)
a.push_back(Instance(static_cast<int>(i)));
for (size_t i = 0; i < l2; i++)
b.push_back(Instance(100 + static_cast<int>(i)));
EXPECT_EQ(tracker.instances(), static_cast<int>(l1 + l2));
tracker.ResetCopiesMovesSwaps();
{
using std::swap;
swap(a, b);
}
EXPECT_EQ(tracker.instances(), static_cast<int>(l1 + l2));
if (a.size() > inlined_capacity && b.size() > inlined_capacity) {
EXPECT_EQ(tracker.swaps(), 0); // Allocations are swapped.
EXPECT_EQ(tracker.moves(), 0);
} else if (a.size() <= inlined_capacity && b.size() <= inlined_capacity) {
EXPECT_EQ(tracker.swaps(), static_cast<int>(min_len));
EXPECT_EQ((tracker.moves() ? tracker.moves() : tracker.copies()),
static_cast<int>(max_len - min_len));
} else {
// One is allocated and the other isn't. The allocation is transferred
// without copying elements, and the inlined instances are copied/moved.
EXPECT_EQ(tracker.swaps(), 0);
EXPECT_EQ((tracker.moves() ? tracker.moves() : tracker.copies()),
static_cast<int>(min_len));
}
EXPECT_EQ(l1, b.size());
EXPECT_EQ(l2, a.size());
for (size_t i = 0; i < l1; i++) {
EXPECT_EQ(static_cast<int>(i), b[i].value());
}
for (size_t i = 0; i < l2; i++) {
EXPECT_EQ(100 + static_cast<int>(i), a[i].value());
}
}
}
}
TEST(IntVec, EqualAndNotEqual) {
IntVec a, b;
EXPECT_TRUE(a == b);
EXPECT_FALSE(a != b);
a.push_back(3);
EXPECT_FALSE(a == b);
EXPECT_TRUE(a != b);
b.push_back(3);
EXPECT_TRUE(a == b);
EXPECT_FALSE(a != b);
b.push_back(7);
EXPECT_FALSE(a == b);
EXPECT_TRUE(a != b);
a.push_back(6);
EXPECT_FALSE(a == b);
EXPECT_TRUE(a != b);
a.clear();
b.clear();
for (size_t i = 0; i < 100; i++) {
a.push_back(static_cast<int>(i));
b.push_back(static_cast<int>(i));
EXPECT_TRUE(a == b);
EXPECT_FALSE(a != b);
b[i] = b[i] + 1;
EXPECT_FALSE(a == b);
EXPECT_TRUE(a != b);
b[i] = b[i] - 1; // Back to before
EXPECT_TRUE(a == b);
EXPECT_FALSE(a != b);
}
}
TEST(IntVec, RelationalOps) {
IntVec a, b;
EXPECT_FALSE(a < b);
EXPECT_FALSE(b < a);
EXPECT_FALSE(a > b);
EXPECT_FALSE(b > a);
EXPECT_TRUE(a <= b);
EXPECT_TRUE(b <= a);
EXPECT_TRUE(a >= b);
EXPECT_TRUE(b >= a);
b.push_back(3);
EXPECT_TRUE(a < b);
EXPECT_FALSE(b < a);
EXPECT_FALSE(a > b);
EXPECT_TRUE(b > a);
EXPECT_TRUE(a <= b);
EXPECT_FALSE(b <= a);
EXPECT_FALSE(a >= b);
EXPECT_TRUE(b >= a);
}
TYPED_TEST_P(InstanceTest, CountConstructorsDestructors) {
using Instance = TypeParam;
using InstanceVec = absl::InlinedVector<Instance, 8>;
InstanceTracker tracker;
for (size_t len = 0; len < 20; len++) {
SCOPED_TRACE(len);
tracker.ResetCopiesMovesSwaps();
InstanceVec v;
const size_t inlined_capacity = v.capacity();
for (size_t i = 0; i < len; i++) {
v.push_back(Instance(static_cast<int>(i)));
}
EXPECT_EQ(tracker.instances(), static_cast<int>(len));
EXPECT_GE(tracker.copies() + tracker.moves(),
static_cast<int>(len)); // More due to reallocation.
tracker.ResetCopiesMovesSwaps();
// Enlarging resize() must construct some objects