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