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