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