1bb611c8fSApple OSS Distributions // 2bb611c8fSApple OSS Distributions // Tests for 3bb611c8fSApple OSS Distributions // safe_allocation& operator=(safe_allocation&& other); 4bb611c8fSApple OSS Distributions // 5bb611c8fSApple OSS Distributions 6bb611c8fSApple OSS Distributions #include <libkern/c++/safe_allocation.h> 7bb611c8fSApple OSS Distributions #include <darwintest.h> 8bb611c8fSApple OSS Distributions #include "test_utils.h" 9bb611c8fSApple OSS Distributions #include <utility> 10bb611c8fSApple OSS Distributions 11bb611c8fSApple OSS Distributions struct T { 12bb611c8fSApple OSS Distributions int i; 13bb611c8fSApple OSS Distributions }; 14bb611c8fSApple OSS Distributions 15bb611c8fSApple OSS Distributions template <typename T> 16bb611c8fSApple OSS Distributions static void tests()17bb611c8fSApple OSS Distributionstests() 18bb611c8fSApple OSS Distributions { 19bb611c8fSApple OSS Distributions // Move-assign non-null to non-null 20bb611c8fSApple OSS Distributions { 21bb611c8fSApple OSS Distributions { 22bb611c8fSApple OSS Distributions tracked_safe_allocation<T> from(10, libkern::allocate_memory); 23bb611c8fSApple OSS Distributions T* memory = from.data(); 24bb611c8fSApple OSS Distributions { 25bb611c8fSApple OSS Distributions tracked_safe_allocation<T> to(20, libkern::allocate_memory); 26bb611c8fSApple OSS Distributions tracking_allocator::reset(); 27bb611c8fSApple OSS Distributions 28bb611c8fSApple OSS Distributions tracked_safe_allocation<T>& ref = (to = std::move(from)); 29bb611c8fSApple OSS Distributions CHECK(&ref == &to); 30bb611c8fSApple OSS Distributions CHECK(to.data() == memory); 31bb611c8fSApple OSS Distributions CHECK(to.size() == 10); 32bb611c8fSApple OSS Distributions CHECK(from.data() == nullptr); 33bb611c8fSApple OSS Distributions CHECK(from.size() == 0); 34bb611c8fSApple OSS Distributions 35bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 36bb611c8fSApple OSS Distributions CHECK(tracking_allocator::deallocated_size == 20 * sizeof(T)); 37bb611c8fSApple OSS Distributions tracking_allocator::reset(); 38bb611c8fSApple OSS Distributions } 39bb611c8fSApple OSS Distributions CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); 40bb611c8fSApple OSS Distributions tracking_allocator::reset(); 41bb611c8fSApple OSS Distributions } 42bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 43bb611c8fSApple OSS Distributions } 44bb611c8fSApple OSS Distributions 45bb611c8fSApple OSS Distributions // Move-assign null to non-null 46bb611c8fSApple OSS Distributions { 47bb611c8fSApple OSS Distributions { 48bb611c8fSApple OSS Distributions tracked_safe_allocation<T> from = nullptr; 49bb611c8fSApple OSS Distributions { 50bb611c8fSApple OSS Distributions tracked_safe_allocation<T> to(20, libkern::allocate_memory); 51bb611c8fSApple OSS Distributions tracking_allocator::reset(); 52bb611c8fSApple OSS Distributions 53bb611c8fSApple OSS Distributions tracked_safe_allocation<T>& ref = (to = std::move(from)); 54bb611c8fSApple OSS Distributions CHECK(&ref == &to); 55bb611c8fSApple OSS Distributions CHECK(to.data() == nullptr); 56bb611c8fSApple OSS Distributions CHECK(to.size() == 0); 57bb611c8fSApple OSS Distributions CHECK(from.data() == nullptr); 58bb611c8fSApple OSS Distributions CHECK(from.size() == 0); 59bb611c8fSApple OSS Distributions 60bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 61bb611c8fSApple OSS Distributions CHECK(tracking_allocator::deallocated_size == 20 * sizeof(T)); 62bb611c8fSApple OSS Distributions tracking_allocator::reset(); 63bb611c8fSApple OSS Distributions } 64bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 65bb611c8fSApple OSS Distributions tracking_allocator::reset(); 66bb611c8fSApple OSS Distributions } 67bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 68bb611c8fSApple OSS Distributions } 69bb611c8fSApple OSS Distributions 70bb611c8fSApple OSS Distributions // Move-assign non-null to null 71bb611c8fSApple OSS Distributions { 72bb611c8fSApple OSS Distributions { 73bb611c8fSApple OSS Distributions tracked_safe_allocation<T> from(10, libkern::allocate_memory); 74bb611c8fSApple OSS Distributions T* memory = from.data(); 75bb611c8fSApple OSS Distributions { 76bb611c8fSApple OSS Distributions tracked_safe_allocation<T> to = nullptr; 77bb611c8fSApple OSS Distributions tracking_allocator::reset(); 78bb611c8fSApple OSS Distributions 79bb611c8fSApple OSS Distributions tracked_safe_allocation<T>& ref = (to = std::move(from)); 80bb611c8fSApple OSS Distributions CHECK(&ref == &to); 81bb611c8fSApple OSS Distributions CHECK(to.data() == memory); 82bb611c8fSApple OSS Distributions CHECK(to.size() == 10); 83bb611c8fSApple OSS Distributions CHECK(from.data() == nullptr); 84bb611c8fSApple OSS Distributions CHECK(from.size() == 0); 85bb611c8fSApple OSS Distributions 86bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 87bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 88bb611c8fSApple OSS Distributions tracking_allocator::reset(); 89bb611c8fSApple OSS Distributions } 90bb611c8fSApple OSS Distributions CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); 91bb611c8fSApple OSS Distributions tracking_allocator::reset(); 92bb611c8fSApple OSS Distributions } 93bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 94bb611c8fSApple OSS Distributions } 95bb611c8fSApple OSS Distributions 96bb611c8fSApple OSS Distributions // Move-assign null to null 97bb611c8fSApple OSS Distributions { 98bb611c8fSApple OSS Distributions { 99bb611c8fSApple OSS Distributions tracked_safe_allocation<T> from = nullptr; 100bb611c8fSApple OSS Distributions { 101bb611c8fSApple OSS Distributions tracked_safe_allocation<T> to = nullptr; 102bb611c8fSApple OSS Distributions tracking_allocator::reset(); 103bb611c8fSApple OSS Distributions 104bb611c8fSApple OSS Distributions tracked_safe_allocation<T>& ref = (to = std::move(from)); 105bb611c8fSApple OSS Distributions CHECK(&ref == &to); 106bb611c8fSApple OSS Distributions CHECK(to.data() == nullptr); 107bb611c8fSApple OSS Distributions CHECK(to.size() == 0); 108bb611c8fSApple OSS Distributions CHECK(from.data() == nullptr); 109bb611c8fSApple OSS Distributions CHECK(from.size() == 0); 110bb611c8fSApple OSS Distributions 111bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 112bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 113bb611c8fSApple OSS Distributions tracking_allocator::reset(); 114bb611c8fSApple OSS Distributions } 115bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 116bb611c8fSApple OSS Distributions tracking_allocator::reset(); 117bb611c8fSApple OSS Distributions } 118bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 119bb611c8fSApple OSS Distributions } 120bb611c8fSApple OSS Distributions 121bb611c8fSApple OSS Distributions // Move-assign to self 122bb611c8fSApple OSS Distributions { 123bb611c8fSApple OSS Distributions { 124bb611c8fSApple OSS Distributions tracked_safe_allocation<T> obj(10, libkern::allocate_memory); 125bb611c8fSApple OSS Distributions T* memory = obj.data(); 126bb611c8fSApple OSS Distributions 127bb611c8fSApple OSS Distributions tracking_allocator::reset(); 128bb611c8fSApple OSS Distributions tracked_safe_allocation<T>& ref = (obj = std::move(obj)); 129bb611c8fSApple OSS Distributions CHECK(&ref == &obj); 130bb611c8fSApple OSS Distributions CHECK(obj.data() == memory); 131bb611c8fSApple OSS Distributions CHECK(obj.size() == 10); 132bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 133bb611c8fSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 134bb611c8fSApple OSS Distributions tracking_allocator::reset(); 135bb611c8fSApple OSS Distributions } 136bb611c8fSApple OSS Distributions CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); 137bb611c8fSApple OSS Distributions } 138bb611c8fSApple OSS Distributions } 139bb611c8fSApple OSS Distributions 140*8d741a5dSApple OSS Distributions T_DECL(assign_move, "safe_allocation.assign.move", T_META_TAG_VM_PREFERRED) { 141bb611c8fSApple OSS Distributions tests<T>(); 142bb611c8fSApple OSS Distributions tests<T const>(); 143bb611c8fSApple OSS Distributions } 144