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