1*0f4c859eSApple OSS Distributions // 2*0f4c859eSApple OSS Distributions // Tests for 3*0f4c859eSApple OSS Distributions // safe_allocation& operator=(std::nullptr_t); 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 10*0f4c859eSApple OSS Distributions struct T { 11*0f4c859eSApple OSS Distributions int i; 12*0f4c859eSApple OSS Distributions }; 13*0f4c859eSApple OSS Distributions 14*0f4c859eSApple OSS Distributions template <typename T> 15*0f4c859eSApple OSS Distributions static void tests()16*0f4c859eSApple OSS Distributionstests() 17*0f4c859eSApple OSS Distributions { 18*0f4c859eSApple OSS Distributions // Assign to a non-null allocation 19*0f4c859eSApple OSS Distributions { 20*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> array(10, libkern::allocate_memory); 21*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 22*0f4c859eSApple OSS Distributions 23*0f4c859eSApple OSS Distributions tracked_safe_allocation<T>& ref = (array = nullptr); 24*0f4c859eSApple OSS Distributions CHECK(&ref == &array); 25*0f4c859eSApple OSS Distributions CHECK(array.data() == nullptr); 26*0f4c859eSApple OSS Distributions CHECK(array.size() == 0); 27*0f4c859eSApple OSS Distributions CHECK(tracking_allocator::did_deallocate); 28*0f4c859eSApple OSS Distributions } 29*0f4c859eSApple OSS Distributions 30*0f4c859eSApple OSS Distributions // Assign to a null allocation 31*0f4c859eSApple OSS Distributions { 32*0f4c859eSApple OSS Distributions tracked_safe_allocation<T> array = nullptr; 33*0f4c859eSApple OSS Distributions tracking_allocator::reset(); 34*0f4c859eSApple OSS Distributions 35*0f4c859eSApple OSS Distributions tracked_safe_allocation<T>& ref = (array = nullptr); 36*0f4c859eSApple OSS Distributions CHECK(&ref == &array); 37*0f4c859eSApple OSS Distributions CHECK(array.data() == nullptr); 38*0f4c859eSApple OSS Distributions CHECK(array.size() == 0); 39*0f4c859eSApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 40*0f4c859eSApple OSS Distributions } 41*0f4c859eSApple OSS Distributions } 42*0f4c859eSApple OSS Distributions 43*0f4c859eSApple OSS Distributions T_DECL(assign_nullptr, "safe_allocation.assign.nullptr") { 44*0f4c859eSApple OSS Distributions tests<T>(); 45*0f4c859eSApple OSS Distributions tests<T const>(); 46*0f4c859eSApple OSS Distributions } 47