1*43a90889SApple OSS Distributions // 2*43a90889SApple OSS Distributions // Tests for 3*43a90889SApple OSS Distributions // safe_allocation(safe_allocation&& other); 4*43a90889SApple OSS Distributions // 5*43a90889SApple OSS Distributions 6*43a90889SApple OSS Distributions #include <libkern/c++/safe_allocation.h> 7*43a90889SApple OSS Distributions #include <darwintest.h> 8*43a90889SApple OSS Distributions #include "test_utils.h" 9*43a90889SApple OSS Distributions #include <utility> 10*43a90889SApple OSS Distributions 11*43a90889SApple OSS Distributions struct T { 12*43a90889SApple OSS Distributions int i; 13*43a90889SApple OSS Distributions }; 14*43a90889SApple OSS Distributions 15*43a90889SApple OSS Distributions template <typename T> 16*43a90889SApple OSS Distributions static void tests()17*43a90889SApple OSS Distributionstests() 18*43a90889SApple OSS Distributions { 19*43a90889SApple OSS Distributions // Move-construct from a non-null allocation (with different syntaxes) 20*43a90889SApple OSS Distributions { 21*43a90889SApple OSS Distributions { 22*43a90889SApple OSS Distributions tracked_safe_allocation<T> from(10, libkern::allocate_memory); 23*43a90889SApple OSS Distributions tracking_allocator::reset(); 24*43a90889SApple OSS Distributions 25*43a90889SApple OSS Distributions T* memory = from.data(); 26*43a90889SApple OSS Distributions 27*43a90889SApple OSS Distributions { 28*43a90889SApple OSS Distributions tracked_safe_allocation<T> to(std::move(from)); 29*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 30*43a90889SApple OSS Distributions CHECK(to.data() == memory); 31*43a90889SApple OSS Distributions CHECK(to.size() == 10); 32*43a90889SApple OSS Distributions CHECK(from.data() == nullptr); 33*43a90889SApple OSS Distributions CHECK(from.size() == 0); 34*43a90889SApple OSS Distributions } 35*43a90889SApple OSS Distributions CHECK(tracking_allocator::did_deallocate); 36*43a90889SApple OSS Distributions tracking_allocator::reset(); 37*43a90889SApple OSS Distributions } 38*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 39*43a90889SApple OSS Distributions } 40*43a90889SApple OSS Distributions { 41*43a90889SApple OSS Distributions { 42*43a90889SApple OSS Distributions tracked_safe_allocation<T> from(10, libkern::allocate_memory); 43*43a90889SApple OSS Distributions tracking_allocator::reset(); 44*43a90889SApple OSS Distributions 45*43a90889SApple OSS Distributions T* memory = from.data(); 46*43a90889SApple OSS Distributions 47*43a90889SApple OSS Distributions { 48*43a90889SApple OSS Distributions tracked_safe_allocation<T> to{std::move(from)}; 49*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 50*43a90889SApple OSS Distributions CHECK(to.data() == memory); 51*43a90889SApple OSS Distributions CHECK(to.size() == 10); 52*43a90889SApple OSS Distributions CHECK(from.data() == nullptr); 53*43a90889SApple OSS Distributions CHECK(from.size() == 0); 54*43a90889SApple OSS Distributions } 55*43a90889SApple OSS Distributions CHECK(tracking_allocator::did_deallocate); 56*43a90889SApple OSS Distributions tracking_allocator::reset(); 57*43a90889SApple OSS Distributions } 58*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 59*43a90889SApple OSS Distributions } 60*43a90889SApple OSS Distributions { 61*43a90889SApple OSS Distributions { 62*43a90889SApple OSS Distributions tracked_safe_allocation<T> from(10, libkern::allocate_memory); 63*43a90889SApple OSS Distributions tracking_allocator::reset(); 64*43a90889SApple OSS Distributions 65*43a90889SApple OSS Distributions T* memory = from.data(); 66*43a90889SApple OSS Distributions 67*43a90889SApple OSS Distributions { 68*43a90889SApple OSS Distributions tracked_safe_allocation<T> to = std::move(from); 69*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 70*43a90889SApple OSS Distributions CHECK(to.data() == memory); 71*43a90889SApple OSS Distributions CHECK(to.size() == 10); 72*43a90889SApple OSS Distributions CHECK(from.data() == nullptr); 73*43a90889SApple OSS Distributions CHECK(from.size() == 0); 74*43a90889SApple OSS Distributions } 75*43a90889SApple OSS Distributions CHECK(tracking_allocator::did_deallocate); 76*43a90889SApple OSS Distributions tracking_allocator::reset(); 77*43a90889SApple OSS Distributions } 78*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 79*43a90889SApple OSS Distributions } 80*43a90889SApple OSS Distributions 81*43a90889SApple OSS Distributions // Move-construct from a null allocation 82*43a90889SApple OSS Distributions { 83*43a90889SApple OSS Distributions { 84*43a90889SApple OSS Distributions tracked_safe_allocation<T> from = nullptr; 85*43a90889SApple OSS Distributions tracking_allocator::reset(); 86*43a90889SApple OSS Distributions 87*43a90889SApple OSS Distributions { 88*43a90889SApple OSS Distributions tracked_safe_allocation<T> to(std::move(from)); 89*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 90*43a90889SApple OSS Distributions CHECK(to.data() == nullptr); 91*43a90889SApple OSS Distributions CHECK(to.size() == 0); 92*43a90889SApple OSS Distributions CHECK(from.data() == nullptr); 93*43a90889SApple OSS Distributions CHECK(from.size() == 0); 94*43a90889SApple OSS Distributions } 95*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 96*43a90889SApple OSS Distributions tracking_allocator::reset(); 97*43a90889SApple OSS Distributions } 98*43a90889SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 99*43a90889SApple OSS Distributions } 100*43a90889SApple OSS Distributions } 101*43a90889SApple OSS Distributions 102*43a90889SApple OSS Distributions T_DECL(ctor_move, "safe_allocation.ctor.move", T_META_TAG_VM_PREFERRED) { 103*43a90889SApple OSS Distributions tests<T>(); 104*43a90889SApple OSS Distributions tests<T const>(); 105*43a90889SApple OSS Distributions } 106