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