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