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