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