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