xref: /xnu-8796.121.2/tests/bounded_ptr_src/arith.add.cpp (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions //
2*c54f35caSApple OSS Distributions // Tests for
3*c54f35caSApple OSS Distributions //  friend bounded_ptr operator+(bounded_ptr p, std::ptrdiff_t n);
4*c54f35caSApple OSS Distributions //  friend bounded_ptr operator+(std::ptrdiff_t n, bounded_ptr p);
5*c54f35caSApple OSS Distributions //
6*c54f35caSApple OSS Distributions // The heavy lifting is done in operator+=, so we only check basic functioning.
7*c54f35caSApple OSS Distributions //
8*c54f35caSApple OSS Distributions 
9*c54f35caSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
10*c54f35caSApple OSS Distributions #include <array>
11*c54f35caSApple OSS Distributions #include <darwintest.h>
12*c54f35caSApple OSS Distributions #include <darwintest_utils.h>
13*c54f35caSApple OSS Distributions #include "test_utils.h"
14*c54f35caSApple OSS Distributions 
15*c54f35caSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
16*c54f35caSApple OSS Distributions 
17*c54f35caSApple OSS Distributions struct T {
18*c54f35caSApple OSS Distributions 	int i;
19*c54f35caSApple OSS Distributions };
20*c54f35caSApple OSS Distributions 
21*c54f35caSApple OSS Distributions template <typename T, typename QualT>
22*c54f35caSApple OSS Distributions static void
tests()23*c54f35caSApple OSS Distributions tests()
24*c54f35caSApple OSS Distributions {
25*c54f35caSApple OSS Distributions 	std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
26*c54f35caSApple OSS Distributions 
27*c54f35caSApple OSS Distributions 	// Add positive offsets
28*c54f35caSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
29*c54f35caSApple OSS Distributions 	//   ^                                                ^
30*c54f35caSApple OSS Distributions 	//   |                                                |
31*c54f35caSApple OSS Distributions 	// begin, ptr                                        end
32*c54f35caSApple OSS Distributions 	{
33*c54f35caSApple OSS Distributions 		test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
34*c54f35caSApple OSS Distributions 
35*c54f35caSApple OSS Distributions 		{
36*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + 0;
37*c54f35caSApple OSS Distributions 			_assert(&*res == &array[0]);
38*c54f35caSApple OSS Distributions 		}
39*c54f35caSApple OSS Distributions 		{
40*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + 1;
41*c54f35caSApple OSS Distributions 			_assert(&*res == &array[1]);
42*c54f35caSApple OSS Distributions 		}
43*c54f35caSApple OSS Distributions 		{
44*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + 2;
45*c54f35caSApple OSS Distributions 			_assert(&*res == &array[2]);
46*c54f35caSApple OSS Distributions 		}
47*c54f35caSApple OSS Distributions 		{
48*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + 3;
49*c54f35caSApple OSS Distributions 			_assert(&*res == &array[3]);
50*c54f35caSApple OSS Distributions 		}
51*c54f35caSApple OSS Distributions 		{
52*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + 4;
53*c54f35caSApple OSS Distributions 			_assert(&*res == &array[4]);
54*c54f35caSApple OSS Distributions 		}
55*c54f35caSApple OSS Distributions 		{
56*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + 5;
57*c54f35caSApple OSS Distributions 			_assert(res == array.end());
58*c54f35caSApple OSS Distributions 		}
59*c54f35caSApple OSS Distributions 	}
60*c54f35caSApple OSS Distributions 
61*c54f35caSApple OSS Distributions 	// Add negative offsets
62*c54f35caSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
63*c54f35caSApple OSS Distributions 	//   ^                                                ^
64*c54f35caSApple OSS Distributions 	//   |                                                |
65*c54f35caSApple OSS Distributions 	// begin                                           end,ptr
66*c54f35caSApple OSS Distributions 	{
67*c54f35caSApple OSS Distributions 		test_bounded_ptr<QualT> const ptr(array.end(), array.begin(), array.end());
68*c54f35caSApple OSS Distributions 
69*c54f35caSApple OSS Distributions 		{
70*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + 0;
71*c54f35caSApple OSS Distributions 			_assert(res == array.end());
72*c54f35caSApple OSS Distributions 		}
73*c54f35caSApple OSS Distributions 		{
74*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + -1;
75*c54f35caSApple OSS Distributions 			_assert(&*res == &array[4]);
76*c54f35caSApple OSS Distributions 		}
77*c54f35caSApple OSS Distributions 		{
78*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + -2;
79*c54f35caSApple OSS Distributions 			_assert(&*res == &array[3]);
80*c54f35caSApple OSS Distributions 		}
81*c54f35caSApple OSS Distributions 		{
82*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + -3;
83*c54f35caSApple OSS Distributions 			_assert(&*res == &array[2]);
84*c54f35caSApple OSS Distributions 		}
85*c54f35caSApple OSS Distributions 		{
86*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + -4;
87*c54f35caSApple OSS Distributions 			_assert(&*res == &array[1]);
88*c54f35caSApple OSS Distributions 		}
89*c54f35caSApple OSS Distributions 		{
90*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = ptr + -5;
91*c54f35caSApple OSS Distributions 			_assert(&*res == &array[0]);
92*c54f35caSApple OSS Distributions 		}
93*c54f35caSApple OSS Distributions 	}
94*c54f35caSApple OSS Distributions 
95*c54f35caSApple OSS Distributions 	// Make sure the original pointer isn't modified
96*c54f35caSApple OSS Distributions 	{
97*c54f35caSApple OSS Distributions 		test_bounded_ptr<QualT> const ptr(array.begin() + 1, array.begin(), array.end());
98*c54f35caSApple OSS Distributions 		(void)(ptr + 3);
99*c54f35caSApple OSS Distributions 		_assert(&*ptr == &array[1]);
100*c54f35caSApple OSS Distributions 	}
101*c54f35caSApple OSS Distributions 
102*c54f35caSApple OSS Distributions 	// Make sure the operator is commutative
103*c54f35caSApple OSS Distributions 	{
104*c54f35caSApple OSS Distributions 		{
105*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
106*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = 0 + ptr;
107*c54f35caSApple OSS Distributions 			_assert(&*res == &array[0]);
108*c54f35caSApple OSS Distributions 		}
109*c54f35caSApple OSS Distributions 		{
110*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
111*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = 3 + ptr;
112*c54f35caSApple OSS Distributions 			_assert(&*res == &array[3]);
113*c54f35caSApple OSS Distributions 		}
114*c54f35caSApple OSS Distributions 		{
115*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> const ptr(array.begin() + 3, array.begin(), array.end());
116*c54f35caSApple OSS Distributions 			test_bounded_ptr<QualT> res = -2 + ptr;
117*c54f35caSApple OSS Distributions 			_assert(&*res == &array[1]);
118*c54f35caSApple OSS Distributions 		}
119*c54f35caSApple OSS Distributions 	}
120*c54f35caSApple OSS Distributions }
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions T_DECL(arith_add, "bounded_ptr.arith.add") {
123*c54f35caSApple OSS Distributions 	tests<T, T>();
124*c54f35caSApple OSS Distributions 	tests<T, T const>();
125*c54f35caSApple OSS Distributions 	tests<T, T volatile>();
126*c54f35caSApple OSS Distributions 	tests<T, T const volatile>();
127*c54f35caSApple OSS Distributions }
128