xref: /xnu-8796.121.2/tests/bounded_array_ref_src/begin_end.cpp (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions //
2*c54f35caSApple OSS Distributions // Tests for
3*c54f35caSApple OSS Distributions //  iterator begin() const;
4*c54f35caSApple OSS Distributions //  iterator end() const;
5*c54f35caSApple OSS Distributions //
6*c54f35caSApple OSS Distributions 
7*c54f35caSApple OSS Distributions #include <libkern/c++/bounded_array_ref.h>
8*c54f35caSApple OSS Distributions #include "test_policy.h"
9*c54f35caSApple OSS Distributions #include <darwintest.h>
10*c54f35caSApple OSS Distributions #include <type_traits>
11*c54f35caSApple OSS Distributions 
12*c54f35caSApple OSS Distributions struct T { int i; };
13*c54f35caSApple OSS Distributions 
14*c54f35caSApple OSS Distributions template <typename T>
15*c54f35caSApple OSS Distributions static void
tests()16*c54f35caSApple OSS Distributions tests()
17*c54f35caSApple OSS Distributions {
18*c54f35caSApple OSS Distributions 	using AR = test_bounded_array_ref<T>;
19*c54f35caSApple OSS Distributions 
20*c54f35caSApple OSS Distributions 	// Check begin()/end() for a non-null array ref
21*c54f35caSApple OSS Distributions 	{
22*c54f35caSApple OSS Distributions 		T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}};
23*c54f35caSApple OSS Distributions 		AR const view(array);
24*c54f35caSApple OSS Distributions 		test_bounded_ptr<T> begin = view.begin();
25*c54f35caSApple OSS Distributions 		test_bounded_ptr<T> end = view.end();
26*c54f35caSApple OSS Distributions 		CHECK(begin.discard_bounds() == &array[0]);
27*c54f35caSApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == &array[5]);
28*c54f35caSApple OSS Distributions 	}
29*c54f35caSApple OSS Distributions 
30*c54f35caSApple OSS Distributions 	// Check begin()/end() for a null array ref
31*c54f35caSApple OSS Distributions 	{
32*c54f35caSApple OSS Distributions 		AR const view;
33*c54f35caSApple OSS Distributions 		test_bounded_ptr<T> begin = view.begin();
34*c54f35caSApple OSS Distributions 		test_bounded_ptr<T> end = view.end();
35*c54f35caSApple OSS Distributions 		CHECK(begin.unsafe_discard_bounds() == nullptr);
36*c54f35caSApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == nullptr);
37*c54f35caSApple OSS Distributions 	}
38*c54f35caSApple OSS Distributions 
39*c54f35caSApple OSS Distributions 	// Check associated types
40*c54f35caSApple OSS Distributions 	{
41*c54f35caSApple OSS Distributions 		static_assert(std::is_same_v<typename AR::iterator, test_bounded_ptr<T> >);
42*c54f35caSApple OSS Distributions 	}
43*c54f35caSApple OSS Distributions }
44*c54f35caSApple OSS Distributions 
45*c54f35caSApple OSS Distributions T_DECL(begin_end, "bounded_array_ref.begin_end") {
46*c54f35caSApple OSS Distributions 	tests<T>();
47*c54f35caSApple OSS Distributions }
48