xref: /xnu-8020.140.41/tests/safe_allocation_src/ctor.allocate_zero.cpp (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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 #include <algorithm>
10 
11 struct T {
12 	int i;
13 };
14 
15 template <typename T>
16 static void
tests()17 tests()
18 {
19 	{
20 		test_safe_allocation<T> const array(10, libkern::allocate_memory_zero);
21 		CHECK(array.data() != nullptr);
22 		CHECK(array.size() == 10);
23 		CHECK(array.begin() == array.data());
24 		CHECK(array.end() == array.data() + 10);
25 
26 		auto const byteArray = reinterpret_cast<uint8_t const*>(array.data());
27 		size_t const byteLength = array.size() * sizeof(T);
28 		CHECK(std::all_of(byteArray, byteArray + byteLength, [](const auto& elem){
29 			return elem == 0;
30 		}));
31 	}
32 }
33 
34 T_DECL(ctor_allocate_zero, "safe_allocation.ctor.allocate_zero") {
35 	tests<T>();
36 	tests<T const>();
37 	tests<int>();
38 	tests<int const>();
39 }
40