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