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