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