From c4ce46a223a398c632c750c8440b8b7526ccc799 Mon Sep 17 00:00:00 2001 From: LunaTheFoxgirl Date: Tue, 9 Apr 2024 20:24:23 +0200 Subject: [PATCH] Add unit test for unique pointer move --- source/numem/mem/ptr.d | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/source/numem/mem/ptr.d b/source/numem/mem/ptr.d index 8a34c28..1e74575 100644 --- a/source/numem/mem/ptr.d +++ b/source/numem/mem/ptr.d @@ -567,29 +567,51 @@ public: } } - +// Tests whether a shared pointer can be created. @("Shared pointer") unittest { - import numem.mem.ptr; class A { } shared_ptr!A p = shared_new!A(); assert(p.get); } +// Tests whether a unique pointer can be created. @("Unique pointer") unittest { - import numem.mem.ptr; class A { } unique_ptr!A p = unique_new!A(); assert(p.get); } +// Tests whether the subtype of a unique pointer can properly be aliased +// and the contents be accessed without .get() @("Unique pointer (alias)") unittest { struct A { bool b() { return true; } } + unique_ptr!A vp = unique_new!A(); assert(vp.b() == true, "Expected vp.b() to return true!"); +} + +// Tests whether unqiue_ptr can be moved via assignment. +@("Unique pointer move") +unittest { + struct A { int b; } + + // Assignment via creation + unique_ptr!A first = unique_new!A(); + assert(first.get(), "Expected first to return non-null value!"); + + // Move via creation + unique_ptr!A second = first; + assert(!first.get(), "Expected first to return null value!"); + assert(second.get(), "Expected second to return non-null value!"); + + // Move via assignment. + first = second; + assert(first.get(), "Expected first to return non-null value!"); + assert(!second.get(), "Expected second to return null value!"); } \ No newline at end of file