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