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