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