xref: /xnu-10063.121.3/tests/bounded_array_ref_src/for_loop.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions //
2*2c2f96dcSApple OSS Distributions // Make sure `bounded_array_ref` works nicely with the range-based for-loop.
3*2c2f96dcSApple OSS Distributions //
4*2c2f96dcSApple OSS Distributions 
5*2c2f96dcSApple OSS Distributions #include <libkern/c++/bounded_array_ref.h>
6*2c2f96dcSApple OSS Distributions #include <darwintest.h>
7*2c2f96dcSApple OSS Distributions #include "test_policy.h"
8*2c2f96dcSApple OSS Distributions 
9*2c2f96dcSApple OSS Distributions struct T {
10*2c2f96dcSApple OSS Distributions 	int i;
11*2c2f96dcSApple OSS Distributions };
12*2c2f96dcSApple OSS Distributions 
13*2c2f96dcSApple OSS Distributions template <typename T>
14*2c2f96dcSApple OSS Distributions static void
tests()15*2c2f96dcSApple OSS Distributions tests()
16*2c2f96dcSApple OSS Distributions {
17*2c2f96dcSApple OSS Distributions 	T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}};
18*2c2f96dcSApple OSS Distributions 	test_bounded_array_ref<T> view(array);
19*2c2f96dcSApple OSS Distributions 	for (T& element : view) {
20*2c2f96dcSApple OSS Distributions 		element = T{3};
21*2c2f96dcSApple OSS Distributions 	}
22*2c2f96dcSApple OSS Distributions 
23*2c2f96dcSApple OSS Distributions 	for (T const& element : view) {
24*2c2f96dcSApple OSS Distributions 		CHECK(element.i == 3);
25*2c2f96dcSApple OSS Distributions 	}
26*2c2f96dcSApple OSS Distributions }
27*2c2f96dcSApple OSS Distributions 
28*2c2f96dcSApple OSS Distributions T_DECL(for_loop, "bounded_array_ref.for_loop") {
29*2c2f96dcSApple OSS Distributions 	tests<T>();
30*2c2f96dcSApple OSS Distributions }
31