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