1 // 2 // Tests for 3 // size_t size() const; 4 // 5 6 #include <libkern/c++/safe_allocation.h> 7 #include <darwintest.h> 8 #include "test_utils.h" 9 #include <cstddef> 10 #include <type_traits> 11 #include <utility> 12 13 struct T { 14 int i; 15 }; 16 17 template <typename T> 18 static void tests()19tests() 20 { 21 { 22 test_safe_allocation<T> const array(10, libkern::allocate_memory); 23 CHECK(array.size() == 10); 24 } 25 { 26 T* memory = reinterpret_cast<T*>(malloc_allocator::allocate(10 * sizeof(T))); 27 test_safe_allocation<T> const array(memory, 10, libkern::adopt_memory); 28 CHECK(array.size() == 10); 29 } 30 { 31 test_safe_allocation<T> const array(nullptr, 0, libkern::adopt_memory); 32 CHECK(array.size() == 0); 33 } 34 { 35 test_safe_allocation<T> const array; 36 CHECK(array.size() == 0); 37 } 38 39 { 40 using Size = decltype(std::declval<test_safe_allocation<T> const&>().size()); 41 static_assert(std::is_same_v<Size, std::size_t>); 42 } 43 } 44 45 T_DECL(size, "safe_allocation.size", T_META_TAG_VM_PREFERRED) { 46 tests<T>(); 47 tests<T const>(); 48 } 49