1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  friend bounded_ptr operator-(bounded_ptr p, std::ptrdiff_t n);
4bb611c8fSApple OSS Distributions //
5bb611c8fSApple OSS Distributions 
6bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
7bb611c8fSApple OSS Distributions #include "test_utils.h"
8bb611c8fSApple OSS Distributions #include <array>
9bb611c8fSApple OSS Distributions #include <cstddef>
10bb611c8fSApple OSS Distributions #include <darwintest.h>
11bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
12bb611c8fSApple OSS Distributions 
13bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
14bb611c8fSApple OSS Distributions 
15bb611c8fSApple OSS Distributions struct T {
16bb611c8fSApple OSS Distributions 	int i;
17bb611c8fSApple OSS Distributions };
18bb611c8fSApple OSS Distributions 
19bb611c8fSApple OSS Distributions template <typename T, typename QualT>
20bb611c8fSApple OSS Distributions static void
tests()21bb611c8fSApple OSS Distributions tests()
22bb611c8fSApple OSS Distributions {
23bb611c8fSApple OSS Distributions 	std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
24bb611c8fSApple OSS Distributions 
25bb611c8fSApple OSS Distributions 	// Subtract positive offsets
26bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
27bb611c8fSApple OSS Distributions 	//   ^                                                ^
28bb611c8fSApple OSS Distributions 	//   |                                                |
29bb611c8fSApple OSS Distributions 	// begin                                           end,ptr
30bb611c8fSApple OSS Distributions 	{
31bb611c8fSApple OSS Distributions 		test_bounded_ptr<QualT> const ptr(array.end(), array.begin(), array.end());
32bb611c8fSApple OSS Distributions 
33bb611c8fSApple OSS Distributions 		{
34bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - static_cast<std::ptrdiff_t>(0);
35bb611c8fSApple OSS Distributions 			_assert(ptr == array.end());
36bb611c8fSApple OSS Distributions 		}
37bb611c8fSApple OSS Distributions 		{
38bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - 1;
39bb611c8fSApple OSS Distributions 			_assert(&*res == &array[4]);
40bb611c8fSApple OSS Distributions 		}
41bb611c8fSApple OSS Distributions 		{
42bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - 2;
43bb611c8fSApple OSS Distributions 			_assert(&*res == &array[3]);
44bb611c8fSApple OSS Distributions 		}
45bb611c8fSApple OSS Distributions 		{
46bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - 3;
47bb611c8fSApple OSS Distributions 			_assert(&*res == &array[2]);
48bb611c8fSApple OSS Distributions 		}
49bb611c8fSApple OSS Distributions 		{
50bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - 4;
51bb611c8fSApple OSS Distributions 			_assert(&*res == &array[1]);
52bb611c8fSApple OSS Distributions 		}
53bb611c8fSApple OSS Distributions 		{
54bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - 5;
55bb611c8fSApple OSS Distributions 			_assert(&*res == &array[0]);
56bb611c8fSApple OSS Distributions 		}
57bb611c8fSApple OSS Distributions 	}
58bb611c8fSApple OSS Distributions 
59bb611c8fSApple OSS Distributions 	// Subtract negative offsets
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,ptr                                         end
64bb611c8fSApple OSS Distributions 	{
65bb611c8fSApple OSS Distributions 		test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
66bb611c8fSApple OSS Distributions 
67bb611c8fSApple OSS Distributions 		{
68bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - static_cast<std::ptrdiff_t>(0);
69bb611c8fSApple OSS Distributions 			_assert(&*res == &array[0]);
70bb611c8fSApple OSS Distributions 		}
71bb611c8fSApple OSS Distributions 		{
72bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - -1;
73bb611c8fSApple OSS Distributions 			_assert(&*res == &array[1]);
74bb611c8fSApple OSS Distributions 		}
75bb611c8fSApple OSS Distributions 		{
76bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - -2;
77bb611c8fSApple OSS Distributions 			_assert(&*res == &array[2]);
78bb611c8fSApple OSS Distributions 		}
79bb611c8fSApple OSS Distributions 		{
80bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - -3;
81bb611c8fSApple OSS Distributions 			_assert(&*res == &array[3]);
82bb611c8fSApple OSS Distributions 		}
83bb611c8fSApple OSS Distributions 		{
84bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - -4;
85bb611c8fSApple OSS Distributions 			_assert(&*res == &array[4]);
86bb611c8fSApple OSS Distributions 		}
87bb611c8fSApple OSS Distributions 		{
88bb611c8fSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr - -5;
89bb611c8fSApple OSS Distributions 			_assert(res == array.end());
90bb611c8fSApple OSS Distributions 		}
91bb611c8fSApple OSS Distributions 	}
92bb611c8fSApple OSS Distributions 
93bb611c8fSApple OSS Distributions 	// Make sure the original pointer isn't modified
94bb611c8fSApple OSS Distributions 	{
95bb611c8fSApple OSS Distributions 		test_bounded_ptr<QualT> const ptr(array.begin() + 4, array.begin(), array.end());
96bb611c8fSApple OSS Distributions 		(void)(ptr - 2);
97bb611c8fSApple OSS Distributions 		_assert(&*ptr == &array[4]);
98bb611c8fSApple OSS Distributions 	}
99bb611c8fSApple OSS Distributions }
100bb611c8fSApple OSS Distributions 
101*8d741a5dSApple OSS Distributions T_DECL(arith_subtract, "bounded_ptr.arith.subtract", T_META_TAG_VM_PREFERRED) {
102bb611c8fSApple OSS Distributions 	tests<T, T>();
103bb611c8fSApple OSS Distributions 	tests<T, T const>();
104bb611c8fSApple OSS Distributions 	tests<T, T volatile>();
105bb611c8fSApple OSS Distributions 	tests<T, T const volatile>();
106bb611c8fSApple OSS Distributions }
107