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