1 // 2 // Tests for 3 // explicit safe_allocation(T* data, size_t n, adopt_memory_t); 4 // 5 6 #include <libkern/c++/safe_allocation.h> 7 #include <darwintest.h> 8 #include "test_utils.h" 9 10 struct T { 11 int i; 12 }; 13 14 template <typename T> 15 static void tests()16tests() 17 { 18 { 19 T* memory = reinterpret_cast<T*>(tracking_allocator::allocate(10 * sizeof(T))); 20 tracking_allocator::reset(); 21 { 22 tracked_safe_allocation<T> array(memory, 10, libkern::adopt_memory); 23 CHECK(!tracking_allocator::did_allocate); 24 CHECK(array.data() == memory); 25 CHECK(array.size() == 10); 26 CHECK(array.begin() == array.data()); 27 CHECK(array.end() == array.data() + 10); 28 } 29 CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); 30 } 31 } 32 33 T_DECL(ctor_adopt, "safe_allocation.ctor.adopt", T_META_TAG_VM_PREFERRED) { 34 tests<T>(); 35 tests<T const>(); 36 } 37