xref: /xnu-10063.121.3/tests/safe_allocation_src/ctor.allocate_zero.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions //
2*2c2f96dcSApple OSS Distributions // Tests for
3*2c2f96dcSApple OSS Distributions //  explicit safe_allocation(size_t n, allocate_memory_zero_t);
4*2c2f96dcSApple OSS Distributions //
5*2c2f96dcSApple OSS Distributions 
6*2c2f96dcSApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7*2c2f96dcSApple OSS Distributions #include <darwintest.h>
8*2c2f96dcSApple OSS Distributions #include "test_utils.h"
9*2c2f96dcSApple OSS Distributions 
10*2c2f96dcSApple OSS Distributions struct T {
11*2c2f96dcSApple OSS Distributions 	int i;
12*2c2f96dcSApple OSS Distributions };
13*2c2f96dcSApple OSS Distributions 
14*2c2f96dcSApple OSS Distributions template <typename T>
15*2c2f96dcSApple OSS Distributions static void
tests()16*2c2f96dcSApple OSS Distributions tests()
17*2c2f96dcSApple OSS Distributions {
18*2c2f96dcSApple OSS Distributions 	{
19*2c2f96dcSApple OSS Distributions 		test_safe_allocation<T> const array(10, libkern::allocate_memory_zero);
20*2c2f96dcSApple OSS Distributions 		CHECK(array.data() != nullptr);
21*2c2f96dcSApple OSS Distributions 		CHECK(array.size() == 10);
22*2c2f96dcSApple OSS Distributions 		CHECK(array.begin() == array.data());
23*2c2f96dcSApple OSS Distributions 		CHECK(array.end() == array.data() + 10);
24*2c2f96dcSApple OSS Distributions 
25*2c2f96dcSApple OSS Distributions 		auto const byteArray = reinterpret_cast<uint8_t const*>(array.data());
26*2c2f96dcSApple OSS Distributions 		size_t const byteLength = array.size() * sizeof(T);
27*2c2f96dcSApple OSS Distributions 		for (size_t i = 0; i != byteLength; ++i) {
28*2c2f96dcSApple OSS Distributions 			CHECK(byteArray[i] == 0);
29*2c2f96dcSApple OSS Distributions 		}
30*2c2f96dcSApple OSS Distributions 	}
31*2c2f96dcSApple OSS Distributions }
32*2c2f96dcSApple OSS Distributions 
33*2c2f96dcSApple OSS Distributions T_DECL(ctor_allocate_zero, "safe_allocation.ctor.allocate_zero") {
34*2c2f96dcSApple OSS Distributions 	tests<T>();
35*2c2f96dcSApple OSS Distributions 	tests<T const>();
36*2c2f96dcSApple OSS Distributions 	tests<int>();
37*2c2f96dcSApple OSS Distributions 	tests<int const>();
38*2c2f96dcSApple OSS Distributions }
39