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