1*0f4c859eSApple OSS Distributions // 2*0f4c859eSApple OSS Distributions // Tests for 3*0f4c859eSApple OSS Distributions // safe_allocation(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-construct from a non-null allocation (with different syntaxes) 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 tracking_allocator::reset(); 24*0f4c859eSApple OSS Distributions 25*0f4c859eSApple OSS Distributions T* memory = from.data(); 26*0f4c859eSApple OSS Distributions 27*0f4c859eSApple OSS Distributions { 28*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> to(std::move(from)); 29*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 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_deallocate); 36*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 37*0f4c859eSApple OSS Distributions } 38*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 39*0f4c859eSApple OSS Distributions } 40*0f4c859eSApple OSS Distributions { 41*0f4c859eSApple OSS Distributions { 42*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> from(10, libkern::allocate_memory); 43*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 44*0f4c859eSApple OSS Distributions 45*0f4c859eSApple OSS Distributions T* memory = from.data(); 46*0f4c859eSApple OSS Distributions 47*0f4c859eSApple OSS Distributions { 48*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> to{std::move(from)}; 49*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 50*0f4c859eSApple OSS Distributions CHECK(to.data() == memory); 51*0f4c859eSApple OSS Distributions CHECK(to.size() == 10); 52*0f4c859eSApple OSS Distributions CHECK(from.data() == nullptr); 53*0f4c859eSApple OSS Distributions CHECK(from.size() == 0); 54*0f4c859eSApple OSS Distributions } 55*0f4c859eSApple OSS Distributions CHECK(tracking_allocator::did_deallocate); 56*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 57*0f4c859eSApple OSS Distributions } 58*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 59*0f4c859eSApple OSS Distributions } 60*0f4c859eSApple OSS Distributions { 61*0f4c859eSApple OSS Distributions { 62*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> from(10, libkern::allocate_memory); 63*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 64*0f4c859eSApple OSS Distributions 65*0f4c859eSApple OSS Distributions T* memory = from.data(); 66*0f4c859eSApple OSS Distributions 67*0f4c859eSApple OSS Distributions { 68*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> to = std::move(from); 69*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 70*0f4c859eSApple OSS Distributions CHECK(to.data() == memory); 71*0f4c859eSApple OSS Distributions CHECK(to.size() == 10); 72*0f4c859eSApple OSS Distributions CHECK(from.data() == nullptr); 73*0f4c859eSApple OSS Distributions CHECK(from.size() == 0); 74*0f4c859eSApple OSS Distributions } 75*0f4c859eSApple OSS Distributions CHECK(tracking_allocator::did_deallocate); 76*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 77*0f4c859eSApple OSS Distributions } 78*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 79*0f4c859eSApple OSS Distributions } 80*0f4c859eSApple OSS Distributions 81*0f4c859eSApple OSS Distributions // Move-construct from a null allocation 82*0f4c859eSApple OSS Distributions { 83*0f4c859eSApple OSS Distributions { 84*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> from = nullptr; 85*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 86*0f4c859eSApple OSS Distributions 87*0f4c859eSApple OSS Distributions { 88*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> to(std::move(from)); 89*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 90*0f4c859eSApple OSS Distributions CHECK(to.data() == nullptr); 91*0f4c859eSApple OSS Distributions CHECK(to.size() == 0); 92*0f4c859eSApple OSS Distributions CHECK(from.data() == nullptr); 93*0f4c859eSApple OSS Distributions CHECK(from.size() == 0); 94*0f4c859eSApple OSS Distributions } 95*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 96*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 97*0f4c859eSApple OSS Distributions } 98*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 99*0f4c859eSApple OSS Distributions } 100*0f4c859eSApple OSS Distributions } 101*0f4c859eSApple OSS Distributions 102*0f4c859eSApple OSS Distributions T_DECL(ctor_move, "safe_allocation.ctor.move") { 103*0f4c859eSApple OSS Distributions tests<T>(); 104*0f4c859eSApple OSS Distributions tests<T const>(); 105*0f4c859eSApple OSS Distributions } 106