15 #include "absl/container/fixed_array.h"
23 #include <scoped_allocator>
28 #include "gmock/gmock.h"
29 #include "gtest/gtest.h"
30 #include "absl/base/config.h"
31 #include "absl/base/internal/exception_testing.h"
32 #include "absl/base/options.h"
33 #include "absl/container/internal/counting_allocator.h"
34 #include "absl/hash/hash_testing.h"
35 #include "absl/memory/memory.h"
42 template <
typename ArrayType>
43 static bool IsOnStack(
const ArrayType&
a) {
44 return a.size() <= ArrayType::inline_elements;
47 class ConstructionTester {
49 ConstructionTester() : self_ptr_(this),
value_(0) { constructions++; }
50 ~ConstructionTester() {
51 assert(self_ptr_ ==
this);
58 static int constructions;
59 static int destructions;
61 void CheckConstructed() { assert(self_ptr_ ==
this); }
69 ConstructionTester* self_ptr_;
73 int ConstructionTester::constructions = 0;
74 int ConstructionTester::destructions = 0;
95 TEST(FixedArrayTest, CopyCtor) {
97 std::iota(on_stack.begin(), on_stack.end(), 0);
103 std::iota(allocated.begin(), allocated.end(), 0);
109 TEST(FixedArrayTest, MoveCtor) {
111 for (
int i = 0;
i < 5; ++
i) {
112 on_stack[
i] = absl::make_unique<int>(
i);
120 for (
int i = 0;
i < 15; ++
i) {
121 allocated[
i] = absl::make_unique<int>(
i);
130 TEST(FixedArrayTest, SmallObjects) {
161 EXPECT_LE(
sizeof(array1),
sizeof(array2) + 100);
162 EXPECT_LE(
sizeof(array2),
sizeof(array1) + 100);
187 TEST(FixedArrayTest, AtThrows) {
191 "failed bounds check");
194 TEST(FixedArrayTest, Hardened) {
195 #if !defined(NDEBUG) || ABSL_OPTION_HARDENED
209 TEST(FixedArrayRelationalsTest, EqualArrays) {
210 for (
int i = 0;
i < 10; ++
i) {
212 std::iota(
a1.begin(),
a1.end(), 0);
230 TEST(FixedArrayRelationalsTest, UnequalArrays) {
231 for (
int i = 1;
i < 10; ++
i) {
233 std::iota(
a1.begin(),
a1.end(), 0);
252 template <
int stack_elements>
253 static void TestArray(
int n) {
256 ConstructionTester::constructions = 0;
257 ConstructionTester::destructions = 0;
266 for (
int i = 0;
i <
n;
i++) {
267 array[
i].CheckConstructed();
273 for (
int i = 0;
i <
n;
i++) {
276 for (
int i = 0;
i <
n;
i++) {
282 for (
int i = 0;
i <
n;
i++) {
285 for (
int i = 0;
i <
n;
i++) {
292 EXPECT_EQ(ConstructionTester::constructions,
293 ConstructionTester::destructions);
296 template <
int elements_per_inner_array,
int inline_elements>
297 static void TestArrayOfArrays(
int n) {
301 ConstructionTester::constructions = 0;
302 ConstructionTester::destructions = 0;
304 using InnerArray = ConstructionTester[elements_per_inner_array];
307 absl::make_unique<absl::FixedArray<InnerArray, inline_elements>>(
n);
308 auto&
array = *array_ptr;
312 sizeof(ConstructionTester) * elements_per_inner_array *
n);
316 for (
int i = 0;
i <
n;
i++) {
317 for (
int j = 0;
j < elements_per_inner_array;
j++) {
318 (
array[
i])[j].CheckConstructed();
322 ASSERT_EQ(ConstructionTester::constructions,
n * elements_per_inner_array);
325 for (
int i = 0;
i <
n;
i++) {
326 for (
int j = 0;
j < elements_per_inner_array;
j++) {
327 (
array[
i])[j].
set(
i * elements_per_inner_array + j);
330 for (
int i = 0;
i <
n;
i++) {
331 for (
int j = 0;
j < elements_per_inner_array;
j++) {
338 for (
int i = 0;
i <
n;
i++) {
339 for (
int j = 0;
j < elements_per_inner_array;
j++) {
340 (
array.data()[
i])[j].
set((
i + 1) * elements_per_inner_array +
j);
343 for (
int i = 0;
i <
n;
i++) {
344 for (
int j = 0;
j < elements_per_inner_array;
j++) {
347 (
i + 1) * elements_per_inner_array + j);
353 EXPECT_EQ(ConstructionTester::constructions,
354 ConstructionTester::destructions);
357 TEST(IteratorConstructorTest, NonInline) {
358 int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
367 TEST(IteratorConstructorTest, Inline) {
368 int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
377 TEST(IteratorConstructorTest, NonPod) {
378 char const*
kInput[] = {
"red",
"orange",
"yellow",
"green",
379 "blue",
"indigo",
"violet"};
388 TEST(IteratorConstructorTest, FromEmptyVector) {
389 std::vector<int>
const empty;
395 TEST(IteratorConstructorTest, FromNonEmptyVector) {
396 int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
400 for (
size_t i = 0;
i <
items.size(); ++
i) {
405 TEST(IteratorConstructorTest, FromBidirectionalIteratorRange) {
406 int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
412 TEST(InitListConstructorTest, InitListConstruction) {
417 TEST(FillConstructorTest, NonEmptyArrays) {
425 TEST(FillConstructorTest, EmptyArray) {
439 TEST(FillConstructorTest, Disambiguation) {
444 TEST(FixedArrayTest, ManySizedArrays) {
445 std::vector<int> sizes;
446 for (
int i = 1;
i < 100;
i++) sizes.push_back(
i);
447 for (
int i = 100;
i <= 1000;
i += 100) sizes.push_back(
i);
448 for (
int n : sizes) {
456 TEST(FixedArrayTest, ManySizedArraysOfArraysOf1) {
457 for (
int n = 1;
n < 1000;
n++) {
465 TEST(FixedArrayTest, ManySizedArraysOfArraysOf2) {
466 for (
int n = 1;
n < 1000;
n++) {
467 TestArrayOfArrays<2, 0>(
n);
468 TestArrayOfArrays<2, 1>(
n);
469 TestArrayOfArrays<2, 64>(
n);
470 TestArrayOfArrays<2, 1000>(
n);
479 TEST(FixedArrayTest, AvoidParanoidDiagnostics) {
481 sprintf(
buf.data(),
"foo");
484 TEST(FixedArrayTest, TooBigInlinedSpace) {
497 "0-sized absl::FixedArray should have same size as Data.");
499 "0-sized absl::FixedArray should have same alignment as Data.");
501 "default-sized absl::FixedArray should have same size as Data");
504 "default-sized absl::FixedArray should have same alignment as Data.");
511 void operator delete(
void*
p) {
513 ::operator
delete(
p);
515 void operator delete[](
void*
p) {
517 ::operator
delete[](
p);
523 TEST(FixedArrayTest, Data) {
524 static const int kInput[] = {2, 3, 5, 7, 11, 13, 17};
543 TEST(FixedArrayTest, FrontAndBack) {
556 TEST(FixedArrayTest, ReverseIteratorInlined) {
583 TEST(FixedArrayTest, ReverseIteratorAllocated) {
610 TEST(FixedArrayTest, Fill) {
613 inlined.fill(fill_val);
617 allocated.fill(fill_val);
622 empty.fill(fill_val);
626 TEST(FixedArrayTest, DefaultCtorDoesNotValueInit) {
630 constexpr
auto scrubbed_bits = 0x95;
633 alignas(FixedArrType)
unsigned char buff[
sizeof(FixedArrType)];
634 std::memset(std::addressof(buff), scrubbed_bits,
sizeof(FixedArrType));
637 ::new (
static_cast<void*
>(std::addressof(buff))) FixedArrType(
length);
639 arr->~FixedArrType();
643 TEST(AllocatorSupportTest, CountInlineAllocations) {
644 constexpr
size_t inlined_size = 4;
652 const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7};
656 AllocFxdArr arr(ia, ia + inlined_size,
alloc);
657 static_cast<void>(arr);
664 TEST(AllocatorSupportTest, CountOutoflineAllocations) {
665 constexpr
size_t inlined_size = 4;
673 const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7};
678 EXPECT_EQ(allocated, arr.size() *
sizeof(
int));
679 static_cast<void>(arr);
685 TEST(AllocatorSupportTest, CountCopyInlineAllocations) {
686 constexpr
size_t inlined_size = 4;
694 Alloc alloc2(&allocated2, &active_instances);
697 int initial_value = 1;
699 AllocFxdArr arr1(inlined_size / 2, initial_value,
alloc);
703 AllocFxdArr arr2(arr1, alloc2);
706 static_cast<void>(arr1);
707 static_cast<void>(arr2);
713 TEST(AllocatorSupportTest, CountCopyOutoflineAllocations) {
714 constexpr
size_t inlined_size = 4;
722 Alloc alloc2(&allocated2, &active_instances);
725 int initial_value = 1;
727 AllocFxdArr arr1(inlined_size * 2, initial_value,
alloc);
729 EXPECT_EQ(allocated1, arr1.size() *
sizeof(
int));
731 AllocFxdArr arr2(arr1, alloc2);
733 EXPECT_EQ(allocated2, inlined_size * 2 *
sizeof(
int));
734 static_cast<void>(arr1);
735 static_cast<void>(arr2);
741 TEST(AllocatorSupportTest, SizeValAllocConstructor) {
746 constexpr
size_t inlined_size = 4;
751 auto len = inlined_size / 2;
754 AllocFxdArr arr(
len, val,
Alloc(&allocated));
761 auto len = inlined_size * 2;
764 AllocFxdArr arr(
len, val,
Alloc(&allocated));
771 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
772 TEST(FixedArrayTest, AddressSanitizerAnnotations1) {
783 TEST(FixedArrayTest, AddressSanitizerAnnotations2) {
785 char* raw =
a.data();
794 TEST(FixedArrayTest, AddressSanitizerAnnotations3) {
803 TEST(FixedArrayTest, AddressSanitizerAnnotations4) {
805 ThreeInts* raw =
a.data();
806 raw[0] = ThreeInts();
807 raw[9] = ThreeInts();
818 #endif // ABSL_HAVE_ADDRESS_SANITIZER
820 TEST(FixedArrayTest, AbslHashValueWorks) {
822 std::vector<V> cases;
826 for (
int i = 0;
i < 10; ++
i) {
828 for (
int j = 0;
j <
i; ++
j) {