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