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