// // Tests for // safe_allocation& operator=(safe_allocation&& other); // #include #include #include "test_utils.h" #include struct T { int i; }; template static void tests() { // Move-assign non-null to non-null { { tracked_safe_allocation from(10, libkern::allocate_memory); T* memory = from.data(); { tracked_safe_allocation to(20, libkern::allocate_memory); tracking_allocator::reset(); tracked_safe_allocation& ref = (to = std::move(from)); CHECK(&ref == &to); CHECK(to.data() == memory); CHECK(to.size() == 10); CHECK(from.data() == nullptr); CHECK(from.size() == 0); CHECK(!tracking_allocator::did_allocate); CHECK(tracking_allocator::deallocated_size == 20 * sizeof(T)); tracking_allocator::reset(); } CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); tracking_allocator::reset(); } CHECK(!tracking_allocator::did_deallocate); } // Move-assign null to non-null { { tracked_safe_allocation from = nullptr; { tracked_safe_allocation to(20, libkern::allocate_memory); tracking_allocator::reset(); tracked_safe_allocation& ref = (to = std::move(from)); CHECK(&ref == &to); CHECK(to.data() == nullptr); CHECK(to.size() == 0); CHECK(from.data() == nullptr); CHECK(from.size() == 0); CHECK(!tracking_allocator::did_allocate); CHECK(tracking_allocator::deallocated_size == 20 * sizeof(T)); tracking_allocator::reset(); } CHECK(!tracking_allocator::did_deallocate); tracking_allocator::reset(); } CHECK(!tracking_allocator::did_deallocate); } // Move-assign non-null to null { { tracked_safe_allocation from(10, libkern::allocate_memory); T* memory = from.data(); { tracked_safe_allocation to = nullptr; tracking_allocator::reset(); tracked_safe_allocation& ref = (to = std::move(from)); CHECK(&ref == &to); CHECK(to.data() == memory); CHECK(to.size() == 10); CHECK(from.data() == nullptr); CHECK(from.size() == 0); CHECK(!tracking_allocator::did_allocate); CHECK(!tracking_allocator::did_deallocate); tracking_allocator::reset(); } CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); tracking_allocator::reset(); } CHECK(!tracking_allocator::did_deallocate); } // Move-assign null to null { { tracked_safe_allocation from = nullptr; { tracked_safe_allocation to = nullptr; tracking_allocator::reset(); tracked_safe_allocation& ref = (to = std::move(from)); CHECK(&ref == &to); CHECK(to.data() == nullptr); CHECK(to.size() == 0); CHECK(from.data() == nullptr); CHECK(from.size() == 0); CHECK(!tracking_allocator::did_allocate); CHECK(!tracking_allocator::did_deallocate); tracking_allocator::reset(); } CHECK(!tracking_allocator::did_deallocate); tracking_allocator::reset(); } CHECK(!tracking_allocator::did_deallocate); } // Move-assign to self { { tracked_safe_allocation obj(10, libkern::allocate_memory); T* memory = obj.data(); tracking_allocator::reset(); tracked_safe_allocation& ref = (obj = std::move(obj)); CHECK(&ref == &obj); CHECK(obj.data() == memory); CHECK(obj.size() == 10); CHECK(!tracking_allocator::did_allocate); CHECK(!tracking_allocator::did_deallocate); tracking_allocator::reset(); } CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); } } T_DECL(assign_move, "safe_allocation.assign.move", T_META_TAG_VM_PREFERRED) { tests(); tests(); }