1*1b191cb5SApple OSS Distributions // 2*1b191cb5SApple OSS Distributions // Tests for 3*1b191cb5SApple OSS Distributions // explicit safe_allocation(size_t n, allocate_memory_t); 4*1b191cb5SApple OSS Distributions // 5*1b191cb5SApple OSS Distributions 6*1b191cb5SApple OSS Distributions #include <libkern/c++/safe_allocation.h> 7*1b191cb5SApple OSS Distributions #include <darwintest.h> 8*1b191cb5SApple OSS Distributions #include "test_utils.h" 9*1b191cb5SApple OSS Distributions #include <cstddef> 10*1b191cb5SApple OSS Distributions #include <limits> 11*1b191cb5SApple OSS Distributions 12*1b191cb5SApple OSS Distributions struct T { 13*1b191cb5SApple OSS Distributions int i; 14*1b191cb5SApple OSS Distributions }; 15*1b191cb5SApple OSS Distributions 16*1b191cb5SApple OSS Distributions struct TrackInit { 17*1b191cb5SApple OSS Distributions bool initialized; TrackInitTrackInit18*1b191cb5SApple OSS Distributions TrackInit() : initialized(true) 19*1b191cb5SApple OSS Distributions { 20*1b191cb5SApple OSS Distributions } 21*1b191cb5SApple OSS Distributions }; 22*1b191cb5SApple OSS Distributions 23*1b191cb5SApple OSS Distributions template <typename T> 24*1b191cb5SApple OSS Distributions static void tests()25*1b191cb5SApple OSS Distributionstests() 26*1b191cb5SApple OSS Distributions { 27*1b191cb5SApple OSS Distributions { 28*1b191cb5SApple OSS Distributions tracking_allocator::reset(); 29*1b191cb5SApple OSS Distributions { 30*1b191cb5SApple OSS Distributions tracked_safe_allocation<T> array(10, libkern::allocate_memory); 31*1b191cb5SApple OSS Distributions CHECK(tracking_allocator::allocated_size == 10 * sizeof(T)); 32*1b191cb5SApple OSS Distributions CHECK(array.data() != nullptr); 33*1b191cb5SApple OSS Distributions CHECK(array.size() == 10); 34*1b191cb5SApple OSS Distributions CHECK(array.begin() == array.data()); 35*1b191cb5SApple OSS Distributions CHECK(array.end() == array.data() + 10); 36*1b191cb5SApple OSS Distributions } 37*1b191cb5SApple OSS Distributions CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T)); 38*1b191cb5SApple OSS Distributions } 39*1b191cb5SApple OSS Distributions 40*1b191cb5SApple OSS Distributions // Check with a huge number of elements that will overflow size_t 41*1b191cb5SApple OSS Distributions { 42*1b191cb5SApple OSS Distributions std::size_t max_n = std::numeric_limits<std::size_t>::max() / sizeof(T); 43*1b191cb5SApple OSS Distributions tracking_allocator::reset(); 44*1b191cb5SApple OSS Distributions 45*1b191cb5SApple OSS Distributions { 46*1b191cb5SApple OSS Distributions tracked_safe_allocation<T> array(max_n + 1, libkern::allocate_memory); 47*1b191cb5SApple OSS Distributions CHECK(array.data() == nullptr); 48*1b191cb5SApple OSS Distributions CHECK(array.size() == 0); 49*1b191cb5SApple OSS Distributions CHECK(array.begin() == array.end()); 50*1b191cb5SApple OSS Distributions CHECK(!tracking_allocator::did_allocate); 51*1b191cb5SApple OSS Distributions } 52*1b191cb5SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate); 53*1b191cb5SApple OSS Distributions } 54*1b191cb5SApple OSS Distributions } 55*1b191cb5SApple OSS Distributions 56*1b191cb5SApple OSS Distributions T_DECL(ctor_allocate, "safe_allocation.ctor.allocate") { 57*1b191cb5SApple OSS Distributions tests<T>(); 58*1b191cb5SApple OSS Distributions tests<T const>(); 59*1b191cb5SApple OSS Distributions 60*1b191cb5SApple OSS Distributions // Make sure we value-initialize elements 61*1b191cb5SApple OSS Distributions { 62*1b191cb5SApple OSS Distributions tracked_safe_allocation<TrackInit> array(10, libkern::allocate_memory); 63*1b191cb5SApple OSS Distributions for (int i = 0; i != 10; ++i) { 64*1b191cb5SApple OSS Distributions CHECK(array[i].initialized == true); 65*1b191cb5SApple OSS Distributions } 66*1b191cb5SApple OSS Distributions } 67*1b191cb5SApple OSS Distributions } 68