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