1*bb611c8fSApple OSS Distributions //
2*bb611c8fSApple OSS Distributions // Tests for
3*bb611c8fSApple OSS Distributions //  explicit bounded_array_ref(T* first, T* last);
4*bb611c8fSApple OSS Distributions //
5*bb611c8fSApple OSS Distributions 
6*bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_array_ref.h>
7*bb611c8fSApple OSS Distributions #include "test_policy.h"
8*bb611c8fSApple OSS Distributions #include <darwintest.h>
9*bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
10*bb611c8fSApple OSS Distributions 
11*bb611c8fSApple OSS Distributions struct T { int i; };
12*bb611c8fSApple OSS Distributions inline bool
13*bb611c8fSApple OSS Distributions operator==(T const& a, T const& b)
14*bb611c8fSApple OSS Distributions {
15*bb611c8fSApple OSS Distributions 	return a.i == b.i;
16*bb611c8fSApple OSS Distributions };
17*bb611c8fSApple OSS Distributions 
18*bb611c8fSApple OSS Distributions template <typename T>
19*bb611c8fSApple OSS Distributions static void
20*bb611c8fSApple OSS Distributions tests()
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 
24*bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
25*bb611c8fSApple OSS Distributions 	//   ^                                                ^
26*bb611c8fSApple OSS Distributions 	//   |                                                |
27*bb611c8fSApple OSS Distributions 	// first                                             last
28*bb611c8fSApple OSS Distributions 	{
29*bb611c8fSApple OSS Distributions 		T* first = &array[0];
30*bb611c8fSApple OSS Distributions 		T* last = &array[5];
31*bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
32*bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[0]);
33*bb611c8fSApple OSS Distributions 		CHECK(view.size() == 5);
34*bb611c8fSApple OSS Distributions 		CHECK(view[0] == T{0});
35*bb611c8fSApple OSS Distributions 		CHECK(view[1] == T{1});
36*bb611c8fSApple OSS Distributions 		CHECK(view[2] == T{2});
37*bb611c8fSApple OSS Distributions 		CHECK(view[3] == T{3});
38*bb611c8fSApple OSS Distributions 		CHECK(view[4] == T{4});
39*bb611c8fSApple OSS Distributions 	}
40*bb611c8fSApple OSS Distributions 
41*bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
42*bb611c8fSApple OSS Distributions 	//   ^        ^
43*bb611c8fSApple OSS Distributions 	//   |        |
44*bb611c8fSApple OSS Distributions 	// first     last
45*bb611c8fSApple OSS Distributions 	{
46*bb611c8fSApple OSS Distributions 		T* first = &array[0];
47*bb611c8fSApple OSS Distributions 		T* last = &array[1];
48*bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
49*bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[0]);
50*bb611c8fSApple OSS Distributions 		CHECK(view.size() == 1);
51*bb611c8fSApple OSS Distributions 		CHECK(view[0] == T{0});
52*bb611c8fSApple OSS Distributions 	}
53*bb611c8fSApple OSS Distributions 
54*bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
55*bb611c8fSApple OSS Distributions 	//   ^
56*bb611c8fSApple OSS Distributions 	//   |
57*bb611c8fSApple OSS Distributions 	// first,last
58*bb611c8fSApple OSS Distributions 	{
59*bb611c8fSApple OSS Distributions 		T* first = &array[0];
60*bb611c8fSApple OSS Distributions 		T* last = &array[0];
61*bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
62*bb611c8fSApple OSS Distributions 		CHECK(view.size() == 0);
63*bb611c8fSApple OSS Distributions 	}
64*bb611c8fSApple OSS Distributions 
65*bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
66*bb611c8fSApple OSS Distributions 	//                                                    ^
67*bb611c8fSApple OSS Distributions 	//                                                    |
68*bb611c8fSApple OSS Distributions 	//                                               first,last
69*bb611c8fSApple OSS Distributions 	{
70*bb611c8fSApple OSS Distributions 		T* first = &array[5];
71*bb611c8fSApple OSS Distributions 		T* last = &array[5];
72*bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
73*bb611c8fSApple OSS Distributions 		CHECK(view.size() == 0);
74*bb611c8fSApple OSS Distributions 	}
75*bb611c8fSApple OSS Distributions }
76*bb611c8fSApple OSS Distributions 
77*bb611c8fSApple OSS Distributions T_DECL(ctor_begin_end, "bounded_array_ref.ctor.begin_end") {
78*bb611c8fSApple OSS Distributions 	tests<T>();
79*bb611c8fSApple OSS Distributions 	tests<T const>();
80*bb611c8fSApple OSS Distributions }
81