summaryrefslogtreecommitdiff
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.cc42
1 files changed, 40 insertions, 2 deletions
diff --git a/absl/memory/memory_test.cc b/absl/memory/memory_test.cc
index dee9b486..21fe32f9 100644
--- a/absl/memory/memory_test.cc
+++ b/absl/memory/memory_test.cc
@@ -69,6 +69,45 @@ TEST(MakeUniqueTest, Basic) {
EXPECT_EQ("hi", *p);
}
+// InitializationVerifier fills in a pattern when allocated so we can
+// distinguish between its default and value initialized states (without
+// accessing truly uninitialized memory).
+struct InitializationVerifier {
+ static constexpr int kDefaultScalar = 0x43;
+ static constexpr int kDefaultArray = 0x4B;
+
+ static void* operator new(size_t n) {
+ void* ret = ::operator new(n);
+ memset(ret, kDefaultScalar, n);
+ return ret;
+ }
+
+ static void* operator new[](size_t n) {
+ void* ret = ::operator new[](n);
+ memset(ret, kDefaultArray, n);
+ return ret;
+ }
+
+ int a;
+ int b;
+};
+
+TEST(Initialization, MakeUnique) {
+ auto p = absl::make_unique<InitializationVerifier>();
+
+ EXPECT_EQ(0, p->a);
+ EXPECT_EQ(0, p->b);
+}
+
+TEST(Initialization, MakeUniqueArray) {
+ auto p = absl::make_unique<InitializationVerifier[]>(2);
+
+ EXPECT_EQ(0, p[0].a);
+ EXPECT_EQ(0, p[0].b);
+ EXPECT_EQ(0, p[1].a);
+ EXPECT_EQ(0, p[1].b);
+}
+
struct MoveOnly {
MoveOnly() = default;
explicit MoveOnly(int i1) : ip1{new int{i1}} {}
@@ -145,11 +184,10 @@ TEST(Make_UniqueTest, NotAmbiguousWithStdMakeUnique) {
explicit TakesStdType(const std::vector<int> &vec) {}
};
using absl::make_unique;
- make_unique<TakesStdType>(std::vector<int>());
+ (void)make_unique<TakesStdType>(std::vector<int>());
}
#if 0
-// TODO(billydonahue): Make a proper NC test.
// These tests shouldn't compile.
TEST(MakeUniqueTestNC, AcceptMoveOnlyLvalue) {
auto m = MoveOnly();