xref: /xnu-8796.141.3/tests/bounded_array_ref_src/ctor.raw_ptr.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions //
2*1b191cb5SApple OSS Distributions // Tests for
3*1b191cb5SApple OSS Distributions //  explicit bounded_array_ref(T* data, size_t n);
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 <cstddef>
9*1b191cb5SApple OSS Distributions #include <darwintest.h>
10*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
11*1b191cb5SApple OSS Distributions 
12*1b191cb5SApple OSS Distributions struct T { int i; };
13*1b191cb5SApple OSS Distributions inline bool
operator ==(T const & a,T const & b)14*1b191cb5SApple OSS Distributions operator==(T const& a, T const& b)
15*1b191cb5SApple OSS Distributions {
16*1b191cb5SApple OSS Distributions 	return a.i == b.i;
17*1b191cb5SApple OSS Distributions };
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions template <typename T>
20*1b191cb5SApple OSS Distributions static void
tests()21*1b191cb5SApple OSS Distributions tests()
22*1b191cb5SApple OSS Distributions {
23*1b191cb5SApple OSS Distributions 	T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}};
24*1b191cb5SApple OSS Distributions 
25*1b191cb5SApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
26*1b191cb5SApple OSS Distributions 	//   ^
27*1b191cb5SApple OSS Distributions 	//   |
28*1b191cb5SApple OSS Distributions 	//  ptr
29*1b191cb5SApple OSS Distributions 	//
30*1b191cb5SApple OSS Distributions 	//   ^------------------- view -----------------------^
31*1b191cb5SApple OSS Distributions 	{
32*1b191cb5SApple OSS Distributions 		T* ptr = &array[0];
33*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 5);
34*1b191cb5SApple OSS Distributions 		CHECK(view.data() == &array[0]);
35*1b191cb5SApple OSS Distributions 		CHECK(view.size() == 5);
36*1b191cb5SApple OSS Distributions 		CHECK(view[0] == T{0});
37*1b191cb5SApple OSS Distributions 		CHECK(view[1] == T{1});
38*1b191cb5SApple OSS Distributions 		CHECK(view[2] == T{2});
39*1b191cb5SApple OSS Distributions 		CHECK(view[3] == T{3});
40*1b191cb5SApple OSS Distributions 		CHECK(view[4] == T{4});
41*1b191cb5SApple OSS Distributions 	}
42*1b191cb5SApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
43*1b191cb5SApple OSS Distributions 	//   ^
44*1b191cb5SApple OSS Distributions 	//   |
45*1b191cb5SApple OSS Distributions 	//  ptr
46*1b191cb5SApple OSS Distributions 	//
47*1b191cb5SApple OSS Distributions 	//   ^----- view -----^
48*1b191cb5SApple OSS Distributions 	{
49*1b191cb5SApple OSS Distributions 		T* ptr = &array[0];
50*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 3);
51*1b191cb5SApple OSS Distributions 		CHECK(view.data() == &array[0]);
52*1b191cb5SApple OSS Distributions 		CHECK(view.size() == 3);
53*1b191cb5SApple OSS Distributions 		CHECK(view[0] == T{0});
54*1b191cb5SApple OSS Distributions 		CHECK(view[1] == T{1});
55*1b191cb5SApple OSS Distributions 		CHECK(view[2] == T{2});
56*1b191cb5SApple OSS Distributions 	}
57*1b191cb5SApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
58*1b191cb5SApple OSS Distributions 	//                              ^
59*1b191cb5SApple OSS Distributions 	//                              |
60*1b191cb5SApple OSS Distributions 	//                             ptr
61*1b191cb5SApple OSS Distributions 	//
62*1b191cb5SApple OSS Distributions 	//                              ^------- view --------^
63*1b191cb5SApple OSS Distributions 	{
64*1b191cb5SApple OSS Distributions 		T* ptr = &array[3];
65*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 2);
66*1b191cb5SApple OSS Distributions 		CHECK(view.data() == &array[3]);
67*1b191cb5SApple OSS Distributions 		CHECK(view.size() == 2);
68*1b191cb5SApple OSS Distributions 		CHECK(view[0] == T{3});
69*1b191cb5SApple OSS Distributions 		CHECK(view[1] == T{4});
70*1b191cb5SApple OSS Distributions 	}
71*1b191cb5SApple OSS Distributions 	// Check with a valid pointer and a size of 0.
72*1b191cb5SApple OSS Distributions 	{
73*1b191cb5SApple OSS Distributions 		T* ptr = &array[0];
74*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, static_cast<std::size_t>(0));
75*1b191cb5SApple OSS Distributions 		CHECK(view.size() == 0);
76*1b191cb5SApple OSS Distributions 	}
77*1b191cb5SApple OSS Distributions 	// Check with a null pointer and a size of 0.
78*1b191cb5SApple OSS Distributions 	{
79*1b191cb5SApple OSS Distributions 		T* ptr = nullptr;
80*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, static_cast<std::size_t>(0));
81*1b191cb5SApple OSS Distributions 		CHECK(view.size() == 0);
82*1b191cb5SApple OSS Distributions 	}
83*1b191cb5SApple OSS Distributions 	// Check with a non-null but invalid pointer and a size of 0.
84*1b191cb5SApple OSS Distributions 	{
85*1b191cb5SApple OSS Distributions 		T* ptr = &array[5];
86*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, static_cast<std::size_t>(0));
87*1b191cb5SApple OSS Distributions 		CHECK(view.size() == 0);
88*1b191cb5SApple OSS Distributions 	}
89*1b191cb5SApple OSS Distributions 	// Make sure there's no ambiguity between constructors.
90*1b191cb5SApple OSS Distributions 	{
91*1b191cb5SApple OSS Distributions 		T* ptr = &array[0];
92*1b191cb5SApple OSS Distributions 		std::ptrdiff_t size = 5;
93*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, size);
94*1b191cb5SApple OSS Distributions 		CHECK(view.data() == &array[0]);
95*1b191cb5SApple OSS Distributions 		CHECK(view.size() == 5);
96*1b191cb5SApple OSS Distributions 	}
97*1b191cb5SApple OSS Distributions 
98*1b191cb5SApple OSS Distributions 	// Make sure we can create nested bounded_array_refs
99*1b191cb5SApple OSS Distributions 	{
100*1b191cb5SApple OSS Distributions 		int array1[] = {1, 2, 3, 4, 5};
101*1b191cb5SApple OSS Distributions 		int array2[] = {6, 7, 8};
102*1b191cb5SApple OSS Distributions 		int array3[] = {9, 10, 11, 12, 13, 14};
103*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<int> views[] = {
104*1b191cb5SApple OSS Distributions 			test_bounded_array_ref<int>(array1, 5),
105*1b191cb5SApple OSS Distributions 			test_bounded_array_ref<int>(array2, 3),
106*1b191cb5SApple OSS Distributions 			test_bounded_array_ref<int>(array3, 6)
107*1b191cb5SApple OSS Distributions 		};
108*1b191cb5SApple OSS Distributions 
109*1b191cb5SApple OSS Distributions 		test_bounded_array_ref<test_bounded_array_ref<int> > two_dim(views, 3);
110*1b191cb5SApple OSS Distributions 		CHECK(two_dim.size() == 3);
111*1b191cb5SApple OSS Distributions 		CHECK(two_dim.data() == &views[0]);
112*1b191cb5SApple OSS Distributions 		CHECK(&two_dim[0] == &views[0]);
113*1b191cb5SApple OSS Distributions 		CHECK(&two_dim[1] == &views[1]);
114*1b191cb5SApple OSS Distributions 		CHECK(&two_dim[2] == &views[2]);
115*1b191cb5SApple OSS Distributions 	}
116*1b191cb5SApple OSS Distributions }
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions T_DECL(ctor_raw_ptr, "bounded_array_ref.ctor.raw_ptr") {
119*1b191cb5SApple OSS Distributions 	tests<T>();
120*1b191cb5SApple OSS Distributions 	tests<T const>();
121*1b191cb5SApple OSS Distributions }
122