xref: /xnu-12377.41.6/tests/safe_allocation_src/data.cpp (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions //
2*bbb1b6f9SApple OSS Distributions // Tests for
3*bbb1b6f9SApple OSS Distributions //      T* data();
4*bbb1b6f9SApple OSS Distributions //      T const* data() const;
5*bbb1b6f9SApple OSS Distributions //
6*bbb1b6f9SApple OSS Distributions 
7*bbb1b6f9SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
8*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
9*bbb1b6f9SApple OSS Distributions #include "test_utils.h"
10*bbb1b6f9SApple OSS Distributions 
11*bbb1b6f9SApple OSS Distributions struct T {
12*bbb1b6f9SApple OSS Distributions 	int i;
13*bbb1b6f9SApple OSS Distributions };
14*bbb1b6f9SApple OSS Distributions 
15*bbb1b6f9SApple OSS Distributions template <typename T>
16*bbb1b6f9SApple OSS Distributions static void
tests()17*bbb1b6f9SApple OSS Distributions tests()
18*bbb1b6f9SApple OSS Distributions {
19*bbb1b6f9SApple OSS Distributions 	{
20*bbb1b6f9SApple OSS Distributions 		test_safe_allocation<T> array(10, libkern::allocate_memory);
21*bbb1b6f9SApple OSS Distributions 		CHECK(array.data() != nullptr);
22*bbb1b6f9SApple OSS Distributions 	}
23*bbb1b6f9SApple OSS Distributions 	{
24*bbb1b6f9SApple OSS Distributions 		T* memory = reinterpret_cast<T*>(malloc_allocator::allocate(10 * sizeof(T)));
25*bbb1b6f9SApple OSS Distributions 		test_safe_allocation<T> array(memory, 10, libkern::adopt_memory);
26*bbb1b6f9SApple OSS Distributions 		T* data = array.data();
27*bbb1b6f9SApple OSS Distributions 		CHECK(data == memory);
28*bbb1b6f9SApple OSS Distributions 	}
29*bbb1b6f9SApple OSS Distributions 	{
30*bbb1b6f9SApple OSS Distributions 		T* memory = reinterpret_cast<T*>(malloc_allocator::allocate(10 * sizeof(T)));
31*bbb1b6f9SApple OSS Distributions 		test_safe_allocation<T> const array(memory, 10, libkern::adopt_memory);
32*bbb1b6f9SApple OSS Distributions 		T const* data = array.data();
33*bbb1b6f9SApple OSS Distributions 		CHECK(data == memory);
34*bbb1b6f9SApple OSS Distributions 	}
35*bbb1b6f9SApple OSS Distributions }
36*bbb1b6f9SApple OSS Distributions 
37*bbb1b6f9SApple OSS Distributions T_DECL(data, "safe_allocation.data", T_META_TAG_VM_PREFERRED) {
38*bbb1b6f9SApple OSS Distributions 	tests<T>();
39*bbb1b6f9SApple OSS Distributions 	tests<T const>();
40*bbb1b6f9SApple OSS Distributions }
41