xref: /xnu-8020.121.3/tests/bounded_array_src/begin_end.cpp (revision fdd8201d7b966f0c3ea610489d29bd841d358941) !
1*fdd8201dSApple OSS Distributions //
2*fdd8201dSApple OSS Distributions // Tests for
3*fdd8201dSApple OSS Distributions //  iterator begin();
4*fdd8201dSApple OSS Distributions //  const_iterator begin() const;
5*fdd8201dSApple OSS Distributions //
6*fdd8201dSApple OSS Distributions //  iterator end();
7*fdd8201dSApple OSS Distributions //  const_iterator end() const;
8*fdd8201dSApple OSS Distributions //
9*fdd8201dSApple OSS Distributions 
10*fdd8201dSApple OSS Distributions #include <libkern/c++/bounded_array.h>
11*fdd8201dSApple OSS Distributions #include "test_policy.h"
12*fdd8201dSApple OSS Distributions #include <darwintest.h>
13*fdd8201dSApple OSS Distributions #include <type_traits>
14*fdd8201dSApple OSS Distributions 
15*fdd8201dSApple OSS Distributions struct T { int i; };
16*fdd8201dSApple OSS Distributions 
17*fdd8201dSApple OSS Distributions template <typename T>
18*fdd8201dSApple OSS Distributions static void
tests()19*fdd8201dSApple OSS Distributions tests()
20*fdd8201dSApple OSS Distributions {
21*fdd8201dSApple OSS Distributions 	// Check begin()/end() for a non-empty array
22*fdd8201dSApple OSS Distributions 	{
23*fdd8201dSApple OSS Distributions 		test_bounded_array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
24*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T> begin = array.begin();
25*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T> end = array.end();
26*fdd8201dSApple OSS Distributions 		CHECK(begin.discard_bounds() == array.data());
27*fdd8201dSApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == array.data() + 5);
28*fdd8201dSApple OSS Distributions 	}
29*fdd8201dSApple OSS Distributions 	{
30*fdd8201dSApple OSS Distributions 		test_bounded_array<T, 5> const array = {T{0}, T{1}, T{2}, T{3}, T{4}};
31*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T const> begin = array.begin();
32*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T const> end = array.end();
33*fdd8201dSApple OSS Distributions 		CHECK(begin.discard_bounds() == array.data());
34*fdd8201dSApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == array.data() + 5);
35*fdd8201dSApple OSS Distributions 	}
36*fdd8201dSApple OSS Distributions 
37*fdd8201dSApple OSS Distributions 	// Check begin()/end() for an empty array
38*fdd8201dSApple OSS Distributions 	{
39*fdd8201dSApple OSS Distributions 		test_bounded_array<T, 0> array = {};
40*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T> begin = array.begin();
41*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T> end = array.end();
42*fdd8201dSApple OSS Distributions 		CHECK(begin.unsafe_discard_bounds() == array.data());
43*fdd8201dSApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == array.data());
44*fdd8201dSApple OSS Distributions 	}
45*fdd8201dSApple OSS Distributions 	{
46*fdd8201dSApple OSS Distributions 		test_bounded_array<T, 0> const array = {};
47*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T const> begin = array.begin();
48*fdd8201dSApple OSS Distributions 		test_bounded_ptr<T const> end = array.end();
49*fdd8201dSApple OSS Distributions 		CHECK(begin.unsafe_discard_bounds() == array.data());
50*fdd8201dSApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == array.data());
51*fdd8201dSApple OSS Distributions 	}
52*fdd8201dSApple OSS Distributions 
53*fdd8201dSApple OSS Distributions 	// Check associated types
54*fdd8201dSApple OSS Distributions 	{
55*fdd8201dSApple OSS Distributions 		using A = test_bounded_array<T, 10>;
56*fdd8201dSApple OSS Distributions 		static_assert(std::is_same_v<typename A::iterator, test_bounded_ptr<T> >);
57*fdd8201dSApple OSS Distributions 		static_assert(std::is_same_v<typename A::const_iterator, test_bounded_ptr<T const> >);
58*fdd8201dSApple OSS Distributions 	}
59*fdd8201dSApple OSS Distributions }
60*fdd8201dSApple OSS Distributions 
61*fdd8201dSApple OSS Distributions T_DECL(begin_end, "bounded_array.begin_end") {
62*fdd8201dSApple OSS Distributions 	tests<T>();
63*fdd8201dSApple OSS Distributions }
64