xref: /xnu-8020.121.3/tests/safe_allocation_src/ctor.adopt.cpp (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions //
2*fdd8201dSApple OSS Distributions // Tests for
3*fdd8201dSApple OSS Distributions //  explicit safe_allocation(T* data, size_t n, adopt_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 
10*fdd8201dSApple OSS Distributions struct T {
11*fdd8201dSApple OSS Distributions 	int i;
12*fdd8201dSApple OSS Distributions };
13*fdd8201dSApple OSS Distributions 
14*fdd8201dSApple OSS Distributions template <typename T>
15*fdd8201dSApple OSS Distributions static void
tests()16*fdd8201dSApple OSS Distributions tests()
17*fdd8201dSApple OSS Distributions {
18*fdd8201dSApple OSS Distributions 	{
19*fdd8201dSApple OSS Distributions 		T* memory = reinterpret_cast<T*>(tracking_allocator::allocate(10 * sizeof(T)));
20*fdd8201dSApple OSS Distributions 		tracking_allocator::reset();
21*fdd8201dSApple OSS Distributions 		{
22*fdd8201dSApple OSS Distributions 			tracked_safe_allocation<T> array(memory, 10, libkern::adopt_memory);
23*fdd8201dSApple OSS Distributions 			CHECK(!tracking_allocator::did_allocate);
24*fdd8201dSApple OSS Distributions 			CHECK(array.data() == memory);
25*fdd8201dSApple OSS Distributions 			CHECK(array.size() == 10);
26*fdd8201dSApple OSS Distributions 			CHECK(array.begin() == array.data());
27*fdd8201dSApple OSS Distributions 			CHECK(array.end() == array.data() + 10);
28*fdd8201dSApple OSS Distributions 		}
29*fdd8201dSApple OSS Distributions 		CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
30*fdd8201dSApple OSS Distributions 	}
31*fdd8201dSApple OSS Distributions }
32*fdd8201dSApple OSS Distributions 
33*fdd8201dSApple OSS Distributions T_DECL(ctor_adopt, "safe_allocation.ctor.adopt") {
34*fdd8201dSApple OSS Distributions 	tests<T>();
35*fdd8201dSApple OSS Distributions 	tests<T const>();
36*fdd8201dSApple OSS Distributions }
37