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