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