diff --git a/test/ctl/unique_ptr_test.cc b/test/ctl/unique_ptr_test.cc index 75ed674ba6c..239902b8329 100644 --- a/test/ctl/unique_ptr_test.cc +++ b/test/ctl/unique_ptr_test.cc @@ -24,72 +24,56 @@ // #include // #define ctl std -template> -using Ptr = ctl::unique_ptr; +using ctl::unique_ptr; +using ctl::make_unique; +using ctl::make_unique_for_overwrite; -template -Ptr -Mk(Args&&... args) -{ - return ctl::make_unique(ctl::forward(args)...); -} - -template -Ptr -MkRaw() -{ - return ctl::make_unique_for_overwrite(); -} - -// #undef ctl +#undef ctl +// The following few definitions are used to get observability into aspects of +// an object's lifecycle, to make sure that e.g. constructing a unique_ptr of a +// type does not construct an object, and that make_unique does construct an +// object. static int g = 0; -struct SetsGDeleter +struct ConstructG { - void operator()(auto* x) const noexcept + ConstructG() { ++g; - delete x; } }; -struct StatefulDeleter +struct DestructG { - char state; - void operator()(auto* x) const noexcept + ~DestructG() { + ++g; } }; -struct FinalDeleter final +struct CallG { void operator()(auto* x) const noexcept { + ++g; } }; -static_assert(sizeof(Ptr) == sizeof(int*)); - -// not everyone uses [[no_unique_address]]... -static_assert(!ctl::is_same_v, ctl::unique_ptr> || - sizeof(Ptr) == sizeof(int*)); +// A unique_ptr with an empty deleter should be the same size as a raw pointer. +static_assert(sizeof(unique_ptr) == sizeof(int*)); -struct SetsGCtor +struct FinalDeleter final { - SetsGCtor() + void operator()(auto* x) const noexcept { - ++g; } }; -struct SetsGDtor -{ - ~SetsGDtor() - { - ++g; - } -}; +// ctl::unique_ptr does not need to inherit from its deleter for this property; +// the STL often does, though, so we don't hold them to the following. +static_assert(!ctl::is_same_v, ctl::unique_ptr> || + sizeof(unique_ptr) == sizeof(int*)); struct Base {}; @@ -100,13 +84,16 @@ struct Derived : Base int main() { + int a; { - Ptr x(new int(5)); + // Shouldn't cause any memory leaks. + unique_ptr x(new int(5)); } { - Ptr x(new int()); + // Deleter is called if the pointer is non-null when reset. + unique_ptr x(&a); x.reset(); if (g != 1) return 1; @@ -114,22 +101,45 @@ main() { g = 0; - Ptr x(new int()); - delete x.release(); + // Deleter is not called if the pointer is null when reset. + unique_ptr x(&a); + x.release(); x.reset(); if (g) return 17; } { - Ptr x(new int(5)), y(new int(6)); + g = 0; + // Deleter is called when the pointer goes out of scope. + { + unique_ptr x(&a); + } + if (!g) + return 18; + } + + { + g = 0; + // Deleter is called if scope ends exceptionally. + try { + unique_ptr x(&a); + throw 'a'; + } catch (char) { + } + if (!g) + return 19; + } + + { + unique_ptr x(new int(5)), y(new int(6)); x.swap(y); if (*x != 6 || *y != 5) return 2; } { - Ptr x; + unique_ptr x; if (x) return 3; x.reset(new int(5)); @@ -139,17 +149,17 @@ main() { g = 0; - Ptr x; + unique_ptr x; if (g) return 5; - x = Mk(); + x = make_unique(); if (g != 1) return 6; } { g = 0; - auto x = Mk(); + auto x = make_unique(); if (g) return 7; x.reset(); @@ -161,9 +171,9 @@ main() { g = 0; - Ptr x, y; - x = Mk(); - y = Mk(); + unique_ptr x, y; + x = make_unique(); + y = make_unique(); #if 0 // shouldn't compile x = y; @@ -178,7 +188,7 @@ main() { g = 0; { - auto x = Mk(); + auto x = make_unique(); } if (g != 1) return 12; @@ -187,7 +197,7 @@ main() { g = 0; { - auto x = Mk(); + auto x = make_unique(); delete x.release(); } if (g != 1) @@ -199,13 +209,13 @@ main() // side effects it has are illegal to detect? { g = 0; - auto x = MkRaw(); + auto x = make_unique_for_overwrite(); if (g) return 14; x.reset(); if (g) return 15; - x = Mk(); + x = make_unique(); if (g != 1) return 16; } @@ -214,16 +224,15 @@ main() { int a; // Should compile. - Ptr x(&a); - Ptr y(&a); + unique_ptr x(&a); } { - Ptr x(new Base); + unique_ptr x(new Base); x.reset(new Derived); - Ptr y(new Derived); - Ptr z(ctl::move(y)); + unique_ptr y(new Derived); + unique_ptr z(ctl::move(y)); } CheckForMemoryLeaks();