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