|
27 | 27 | #include "absl/base/internal/exception_testing.h"
|
28 | 28 | #include "absl/memory/memory.h"
|
29 | 29 |
|
| 30 | +using ::testing::ElementsAreArray; |
| 31 | + |
30 | 32 | namespace {
|
31 | 33 |
|
32 | 34 | // Helper routine to determine if a absl::FixedArray used stack allocation.
|
@@ -89,6 +91,41 @@ class ThreeInts {
|
89 | 91 |
|
90 | 92 | int ThreeInts::counter = 0;
|
91 | 93 |
|
| 94 | +TEST(FixedArrayTest, CopyCtor) { |
| 95 | + absl::FixedArray<int, 10> on_stack(5); |
| 96 | + std::iota(on_stack.begin(), on_stack.end(), 0); |
| 97 | + absl::FixedArray<int, 10> stack_copy = on_stack; |
| 98 | + EXPECT_THAT(stack_copy, ElementsAreArray(on_stack)); |
| 99 | + EXPECT_TRUE(IsOnStack(stack_copy)); |
| 100 | + |
| 101 | + absl::FixedArray<int, 10> allocated(15); |
| 102 | + std::iota(allocated.begin(), allocated.end(), 0); |
| 103 | + absl::FixedArray<int, 10> alloced_copy = allocated; |
| 104 | + EXPECT_THAT(alloced_copy, ElementsAreArray(allocated)); |
| 105 | + EXPECT_FALSE(IsOnStack(alloced_copy)); |
| 106 | +} |
| 107 | + |
| 108 | +TEST(FixedArrayTest, MoveCtor) { |
| 109 | + absl::FixedArray<std::unique_ptr<int>, 10> on_stack(5); |
| 110 | + for (int i = 0; i < 5; ++i) { |
| 111 | + on_stack[i] = absl::make_unique<int>(i); |
| 112 | + } |
| 113 | + |
| 114 | + absl::FixedArray<std::unique_ptr<int>, 10> stack_copy = std::move(on_stack); |
| 115 | + for (int i = 0; i < 5; ++i) EXPECT_EQ(*(stack_copy[i]), i); |
| 116 | + EXPECT_EQ(stack_copy.size(), on_stack.size()); |
| 117 | + |
| 118 | + absl::FixedArray<std::unique_ptr<int>, 10> allocated(15); |
| 119 | + for (int i = 0; i < 15; ++i) { |
| 120 | + allocated[i] = absl::make_unique<int>(i); |
| 121 | + } |
| 122 | + |
| 123 | + absl::FixedArray<std::unique_ptr<int>, 10> alloced_copy = |
| 124 | + std::move(allocated); |
| 125 | + for (int i = 0; i < 15; ++i) EXPECT_EQ(*(alloced_copy[i]), i); |
| 126 | + EXPECT_EQ(allocated.size(), alloced_copy.size()); |
| 127 | +} |
| 128 | + |
92 | 129 | TEST(FixedArrayTest, SmallObjects) {
|
93 | 130 | // Small object arrays
|
94 | 131 | {
|
|
0 commit comments