about summary refs log tree commit diff
path: root/absl/memory/memory_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/memory/memory_test.cc')
-rw-r--r--absl/memory/memory_test.cc43
1 files changed, 43 insertions, 0 deletions
diff --git a/absl/memory/memory_test.cc b/absl/memory/memory_test.cc
index dee9b486a30d..8ff1945debe5 100644
--- a/absl/memory/memory_test.cc
+++ b/absl/memory/memory_test.cc
@@ -611,4 +611,47 @@ TEST(AllocatorNoThrowTest, CustomAllocator) {
   EXPECT_FALSE(absl::allocator_is_nothrow<UnspecifiedAllocator>::value);
 }
 
+TEST(MemoryInternal, UninitDefaultConstructNTrivial) {
+  constexpr int kInitialValue = 123;
+  constexpr int kExpectedValue = kInitialValue;  // Expect no-op behavior
+  constexpr int len = 5;
+
+  struct TestObj {
+    int val;
+  };
+  static_assert(absl::is_trivially_default_constructible<TestObj>::value, "");
+  static_assert(absl::is_trivially_destructible<TestObj>::value, "");
+
+  TestObj objs[len];
+  for (auto& obj : objs) {
+    obj.val = kInitialValue;
+  }
+
+  absl::memory_internal::uninitialized_default_construct_n(objs, len);
+  for (auto& obj : objs) {
+    EXPECT_EQ(obj.val, kExpectedValue);
+  }
+}
+
+TEST(MemoryInternal, UninitDefaultConstructNNonTrivial) {
+  constexpr int kInitialValue = 123;
+  constexpr int kExpectedValue = 0;  // Expect value-construction behavior
+  constexpr int len = 5;
+
+  struct TestObj {
+    int val{kExpectedValue};
+  };
+  static_assert(absl::is_trivially_destructible<TestObj>::value, "");
+
+  TestObj objs[len];
+  for (auto& obj : objs) {
+    obj.val = kInitialValue;
+  }
+
+  absl::memory_internal::uninitialized_default_construct_n(objs, len);
+  for (auto& obj : objs) {
+    EXPECT_EQ(obj.val, kExpectedValue);
+  }
+}
+
 }  // namespace