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