xref: /xnu-8796.141.3/tests/bounded_ptr_src/ctor.begin_end.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions //
2*1b191cb5SApple OSS Distributions // Tests for
3*1b191cb5SApple OSS Distributions //  explicit bounded_ptr(T* pointer, T const* begin, T const* end);
4*1b191cb5SApple OSS Distributions //
5*1b191cb5SApple OSS Distributions 
6*1b191cb5SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
7*1b191cb5SApple OSS Distributions #include <array>
8*1b191cb5SApple OSS Distributions #include <darwintest.h>
9*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
10*1b191cb5SApple OSS Distributions #include "test_utils.h"
11*1b191cb5SApple OSS Distributions 
12*1b191cb5SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
13*1b191cb5SApple OSS Distributions 
14*1b191cb5SApple OSS Distributions struct T {
15*1b191cb5SApple OSS Distributions 	int i;
16*1b191cb5SApple OSS Distributions 	friend constexpr bool
operator ==(T const volatile & a,T const & b)17*1b191cb5SApple OSS Distributions 	operator==(T const volatile& a, T const& b)
18*1b191cb5SApple OSS Distributions 	{
19*1b191cb5SApple OSS Distributions 		return a.i == b.i;
20*1b191cb5SApple OSS Distributions 	}
21*1b191cb5SApple OSS Distributions };
22*1b191cb5SApple OSS Distributions 
23*1b191cb5SApple OSS Distributions template <typename T, typename QualT>
24*1b191cb5SApple OSS Distributions static void
tests()25*1b191cb5SApple OSS Distributions tests()
26*1b191cb5SApple OSS Distributions {
27*1b191cb5SApple OSS Distributions 	std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
28*1b191cb5SApple OSS Distributions 	{
29*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.begin() + 0, array.begin(), array.end());
30*1b191cb5SApple OSS Distributions 		_assert(*p == T{0});
31*1b191cb5SApple OSS Distributions 	}
32*1b191cb5SApple OSS Distributions 	{
33*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.begin() + 1, array.begin(), array.end());
34*1b191cb5SApple OSS Distributions 		_assert(*p == T{1});
35*1b191cb5SApple OSS Distributions 	}
36*1b191cb5SApple OSS Distributions 	{
37*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.begin() + 2, array.begin(), array.end());
38*1b191cb5SApple OSS Distributions 		_assert(*p == T{2});
39*1b191cb5SApple OSS Distributions 	}
40*1b191cb5SApple OSS Distributions 	{
41*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.begin() + 3, array.begin(), array.end());
42*1b191cb5SApple OSS Distributions 		_assert(*p == T{3});
43*1b191cb5SApple OSS Distributions 	}
44*1b191cb5SApple OSS Distributions 	{
45*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.begin() + 4, array.begin(), array.end());
46*1b191cb5SApple OSS Distributions 		_assert(*p == T{4});
47*1b191cb5SApple OSS Distributions 	}
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions 	// It must be valid to construct out-of-bounds pointers, but we obviously
50*1b191cb5SApple OSS Distributions 	// can't dereference them.
51*1b191cb5SApple OSS Distributions 	{
52*1b191cb5SApple OSS Distributions 		// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
53*1b191cb5SApple OSS Distributions 		//            ^                 ^                     ^
54*1b191cb5SApple OSS Distributions 		//            |                 |                     |
55*1b191cb5SApple OSS Distributions 		//           ptr              begin                  end
56*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.begin() + 1, array.begin() + 3, array.end());
57*1b191cb5SApple OSS Distributions 		_assert(p.unsafe_discard_bounds() == array.begin() + 1);
58*1b191cb5SApple OSS Distributions 	}
59*1b191cb5SApple OSS Distributions 	{
60*1b191cb5SApple OSS Distributions 		// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
61*1b191cb5SApple OSS Distributions 		//   ^                          ^        ^
62*1b191cb5SApple OSS Distributions 		//   |                          |        |
63*1b191cb5SApple OSS Distributions 		// begin                       end      ptr
64*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.begin() + 4, array.begin(), array.begin() + 3);
65*1b191cb5SApple OSS Distributions 		_assert(p.unsafe_discard_bounds() == array.begin() + 4);
66*1b191cb5SApple OSS Distributions 	}
67*1b191cb5SApple OSS Distributions 	{
68*1b191cb5SApple OSS Distributions 		// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
69*1b191cb5SApple OSS Distributions 		//   ^                                                ^
70*1b191cb5SApple OSS Distributions 		//   |                                                |
71*1b191cb5SApple OSS Distributions 		// begin                                             end,ptr
72*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(array.end(), array.begin(), array.end());
73*1b191cb5SApple OSS Distributions 		_assert(p.unsafe_discard_bounds() == array.end());
74*1b191cb5SApple OSS Distributions 	}
75*1b191cb5SApple OSS Distributions 
76*1b191cb5SApple OSS Distributions 	// Test creating a bounded_ptr from a null pointer.
77*1b191cb5SApple OSS Distributions 	{
78*1b191cb5SApple OSS Distributions 		test_bounded_ptr<QualT> p(nullptr, nullptr, nullptr);
79*1b191cb5SApple OSS Distributions 		_assert(p.unsafe_discard_bounds() == nullptr);
80*1b191cb5SApple OSS Distributions 	}
81*1b191cb5SApple OSS Distributions }
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions struct Base { };
84*1b191cb5SApple OSS Distributions struct Derived : Base { };
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions T_DECL(ctor_begin_end, "bounded_ptr.ctor.begin_end") {
87*1b191cb5SApple OSS Distributions 	tests<T, T>();
88*1b191cb5SApple OSS Distributions 	tests<T, T const>();
89*1b191cb5SApple OSS Distributions 	tests<T, T volatile>();
90*1b191cb5SApple OSS Distributions 	tests<T, T const volatile>();
91*1b191cb5SApple OSS Distributions 
92*1b191cb5SApple OSS Distributions 	// Make sure we can construct a `bounded_ptr<Base>` from `Derived*` pointers
93*1b191cb5SApple OSS Distributions 	{
94*1b191cb5SApple OSS Distributions 		std::array<Derived, 5> array = {};
95*1b191cb5SApple OSS Distributions 		test_bounded_ptr<Base> p(static_cast<Derived*>(array.begin()),
96*1b191cb5SApple OSS Distributions 		    static_cast<Derived*>(array.begin()),
97*1b191cb5SApple OSS Distributions 		    static_cast<Derived*>(array.end()));
98*1b191cb5SApple OSS Distributions 	}
99*1b191cb5SApple OSS Distributions }
100