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