xref: /xnu-12377.81.4/tests/safe_allocation_src/usage.for_loop.cpp (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions //
2*043036a2SApple OSS Distributions // Make sure `safe_allocation` works nicely with the range-based for-loop.
3*043036a2SApple OSS Distributions //
4*043036a2SApple OSS Distributions 
5*043036a2SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
6*043036a2SApple OSS Distributions #include <darwintest.h>
7*043036a2SApple OSS Distributions #include "test_utils.h"
8*043036a2SApple OSS Distributions 
9*043036a2SApple OSS Distributions struct T {
10*043036a2SApple OSS Distributions 	int i;
11*043036a2SApple OSS Distributions };
12*043036a2SApple OSS Distributions 
13*043036a2SApple OSS Distributions template <typename T>
14*043036a2SApple OSS Distributions static void
tests()15*043036a2SApple OSS Distributions tests()
16*043036a2SApple OSS Distributions {
17*043036a2SApple OSS Distributions 	test_safe_allocation<T> array(10, libkern::allocate_memory);
18*043036a2SApple OSS Distributions 	for (T& element : array) {
19*043036a2SApple OSS Distributions 		element = T{3};
20*043036a2SApple OSS Distributions 	}
21*043036a2SApple OSS Distributions 
22*043036a2SApple OSS Distributions 	for (T const& element : array) {
23*043036a2SApple OSS Distributions 		CHECK(element.i == 3);
24*043036a2SApple OSS Distributions 	}
25*043036a2SApple OSS Distributions }
26*043036a2SApple OSS Distributions 
27*043036a2SApple OSS Distributions T_DECL(usage_for_loop, "safe_allocation.usage.for_loop", T_META_TAG_VM_PREFERRED) {
28*043036a2SApple OSS Distributions 	tests<T>();
29*043036a2SApple OSS Distributions }
30