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