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