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