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