xref: /xnu-10002.81.5/tests/bounded_ptr_src/arith.add_assign.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587) !
1*5e3eaea3SApple OSS Distributions //
2*5e3eaea3SApple OSS Distributions // Tests for
3*5e3eaea3SApple OSS Distributions //  bounded_ptr& operator+=(std::ptrdiff_t n);
4*5e3eaea3SApple OSS Distributions //
5*5e3eaea3SApple OSS Distributions 
6*5e3eaea3SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
7*5e3eaea3SApple OSS Distributions #include <array>
8*5e3eaea3SApple OSS Distributions #include <cstddef>
9*5e3eaea3SApple OSS Distributions #include <cstdint>
10*5e3eaea3SApple OSS Distributions #include <limits>
11*5e3eaea3SApple OSS Distributions #include <darwintest.h>
12*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
13*5e3eaea3SApple OSS Distributions #include "test_utils.h"
14*5e3eaea3SApple OSS Distributions 
15*5e3eaea3SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
16*5e3eaea3SApple OSS Distributions 
17*5e3eaea3SApple OSS Distributions struct T { int i; };
18*5e3eaea3SApple OSS Distributions 
19*5e3eaea3SApple OSS Distributions namespace {
20*5e3eaea3SApple OSS Distributions struct tracking_policy {
21*5e3eaea3SApple OSS Distributions 	static bool did_trap;
22*5e3eaea3SApple OSS Distributions 	static void
trap__anon969693550111::tracking_policy23*5e3eaea3SApple OSS Distributions 	trap(char const*)
24*5e3eaea3SApple OSS Distributions 	{
25*5e3eaea3SApple OSS Distributions 		did_trap = true;
26*5e3eaea3SApple OSS Distributions 	}
27*5e3eaea3SApple OSS Distributions };
28*5e3eaea3SApple OSS Distributions bool tracking_policy::did_trap = false;
29*5e3eaea3SApple OSS Distributions }
30*5e3eaea3SApple OSS Distributions 
31*5e3eaea3SApple OSS Distributions template <typename T, typename QualT>
32*5e3eaea3SApple OSS Distributions static void
tests()33*5e3eaea3SApple OSS Distributions tests()
34*5e3eaea3SApple OSS Distributions {
35*5e3eaea3SApple OSS Distributions 	std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
36*5e3eaea3SApple OSS Distributions 
37*5e3eaea3SApple OSS Distributions 	// Add-assign positive offsets
38*5e3eaea3SApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
39*5e3eaea3SApple OSS Distributions 	//   ^                                                ^
40*5e3eaea3SApple OSS Distributions 	//   |                                                |
41*5e3eaea3SApple OSS Distributions 	// begin,ptr                                         end
42*5e3eaea3SApple OSS Distributions 	{
43*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
44*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += 0;
45*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
46*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[0]);
47*5e3eaea3SApple OSS Distributions 	}
48*5e3eaea3SApple OSS Distributions 	{
49*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
50*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += 1;
51*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
52*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[1]);
53*5e3eaea3SApple OSS Distributions 	}
54*5e3eaea3SApple OSS Distributions 	{
55*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
56*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += 2;
57*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
58*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[2]);
59*5e3eaea3SApple OSS Distributions 	}
60*5e3eaea3SApple OSS Distributions 	{
61*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
62*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += 3;
63*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
64*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[3]);
65*5e3eaea3SApple OSS Distributions 	}
66*5e3eaea3SApple OSS Distributions 	{
67*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
68*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += 4;
69*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
70*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[4]);
71*5e3eaea3SApple OSS Distributions 	}
72*5e3eaea3SApple OSS Distributions 	{
73*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
74*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += 5;
75*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
76*5e3eaea3SApple OSS Distributions 		_assert(ptr == array.end());
77*5e3eaea3SApple OSS Distributions 	}
78*5e3eaea3SApple OSS Distributions 
79*5e3eaea3SApple OSS Distributions 	// Add-assign negative offsets
80*5e3eaea3SApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
81*5e3eaea3SApple OSS Distributions 	//   ^                                                ^
82*5e3eaea3SApple OSS Distributions 	//   |                                                |
83*5e3eaea3SApple OSS Distributions 	// begin                                           end,ptr
84*5e3eaea3SApple OSS Distributions 	{
85*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
86*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += 0;
87*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
88*5e3eaea3SApple OSS Distributions 		_assert(ptr == array.end());
89*5e3eaea3SApple OSS Distributions 	}
90*5e3eaea3SApple OSS Distributions 	{
91*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
92*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += -1;
93*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
94*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[4]);
95*5e3eaea3SApple OSS Distributions 	}
96*5e3eaea3SApple OSS Distributions 	{
97*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
98*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += -2;
99*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
100*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[3]);
101*5e3eaea3SApple OSS Distributions 	}
102*5e3eaea3SApple OSS Distributions 	{
103*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
104*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += -3;
105*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
106*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[2]);
107*5e3eaea3SApple OSS Distributions 	}
108*5e3eaea3SApple OSS Distributions 	{
109*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
110*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += -4;
111*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
112*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[1]);
113*5e3eaea3SApple OSS Distributions 	}
114*5e3eaea3SApple OSS Distributions 	{
115*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
116*5e3eaea3SApple OSS Distributions 		auto& ref = ptr += -5;
117*5e3eaea3SApple OSS Distributions 		_assert(&ref == &ptr);
118*5e3eaea3SApple OSS Distributions 		_assert(&*ptr == &array[0]);
119*5e3eaea3SApple OSS Distributions 	}
120*5e3eaea3SApple OSS Distributions 
121*5e3eaea3SApple OSS Distributions 	// Make sure we trap on arithmetic overflow in the number of bytes calculation
122*5e3eaea3SApple OSS Distributions 	{
123*5e3eaea3SApple OSS Distributions 		std::ptrdiff_t sizeof_T = sizeof(T); // avoid promotion to unsigned in calculations
124*5e3eaea3SApple OSS Distributions 
125*5e3eaea3SApple OSS Distributions 		// largest (most positive) n for the number of bytes `n * sizeof(T)` not to overflow ptrdiff_t
126*5e3eaea3SApple OSS Distributions 		std::ptrdiff_t max_n = std::numeric_limits<std::ptrdiff_t>::max() / sizeof_T;
127*5e3eaea3SApple OSS Distributions 
128*5e3eaea3SApple OSS Distributions 		// smallest (most negative) n for the number of bytes `n * sizeof(T)` not to overflow ptrdiff_t
129*5e3eaea3SApple OSS Distributions 		std::ptrdiff_t min_n = std::numeric_limits<std::ptrdiff_t>::min() / sizeof_T;
130*5e3eaea3SApple OSS Distributions 
131*5e3eaea3SApple OSS Distributions 		// Overflow with a positive offset
132*5e3eaea3SApple OSS Distributions 		{
133*5e3eaea3SApple OSS Distributions 			libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
134*5e3eaea3SApple OSS Distributions 			tracking_policy::did_trap = false;
135*5e3eaea3SApple OSS Distributions 			ptr += max_n + 1;
136*5e3eaea3SApple OSS Distributions 			_assert(tracking_policy::did_trap);
137*5e3eaea3SApple OSS Distributions 		}
138*5e3eaea3SApple OSS Distributions 
139*5e3eaea3SApple OSS Distributions 		// Overflow with a negative offset
140*5e3eaea3SApple OSS Distributions 		{
141*5e3eaea3SApple OSS Distributions 			libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
142*5e3eaea3SApple OSS Distributions 			tracking_policy::did_trap = false;
143*5e3eaea3SApple OSS Distributions 			ptr += min_n - 1;
144*5e3eaea3SApple OSS Distributions 			_assert(tracking_policy::did_trap);
145*5e3eaea3SApple OSS Distributions 		}
146*5e3eaea3SApple OSS Distributions 	}
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions 	// Make sure we trap on arithmetic overflow in the offset calculation
149*5e3eaea3SApple OSS Distributions 	//
150*5e3eaea3SApple OSS Distributions 	// To avoid running into the overflow of `n * sizeof(T)` when ptrdiff_t
151*5e3eaea3SApple OSS Distributions 	// is the same size as int32_t, we test the offset overflow check by
152*5e3eaea3SApple OSS Distributions 	// successive addition of smaller offsets.
153*5e3eaea3SApple OSS Distributions 	//
154*5e3eaea3SApple OSS Distributions 	// We basically push the offset right to its limit, and then push it
155*5e3eaea3SApple OSS Distributions 	// past its limit to watch it overflow.
156*5e3eaea3SApple OSS Distributions 	{
157*5e3eaea3SApple OSS Distributions 		std::int64_t sizeof_T = sizeof(T); // avoid promotion to unsigned in calculations
158*5e3eaea3SApple OSS Distributions 
159*5e3eaea3SApple OSS Distributions 		// largest (most positive) n for the number of bytes `n * sizeof(T)` not to overflow the int32_t offset
160*5e3eaea3SApple OSS Distributions 		std::int64_t max_n = std::numeric_limits<std::int32_t>::max() / sizeof_T;
161*5e3eaea3SApple OSS Distributions 
162*5e3eaea3SApple OSS Distributions 		// smallest (most negative) n for the number of bytes `n * sizeof(T)` not to overflow the int32_t offset
163*5e3eaea3SApple OSS Distributions 		std::int64_t min_n = std::numeric_limits<std::int32_t>::min() / sizeof_T;
164*5e3eaea3SApple OSS Distributions 
165*5e3eaea3SApple OSS Distributions 		// Add positive offsets
166*5e3eaea3SApple OSS Distributions 		{
167*5e3eaea3SApple OSS Distributions 			libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
168*5e3eaea3SApple OSS Distributions 			tracking_policy::did_trap = false;
169*5e3eaea3SApple OSS Distributions 			ptr += static_cast<ptrdiff_t>(max_n / 2);
170*5e3eaea3SApple OSS Distributions 			_assert(!tracking_policy::did_trap);
171*5e3eaea3SApple OSS Distributions 			ptr += static_cast<ptrdiff_t>(max_n / 2);
172*5e3eaea3SApple OSS Distributions 			_assert(!tracking_policy::did_trap);
173*5e3eaea3SApple OSS Distributions 			ptr += (max_n % 2);
174*5e3eaea3SApple OSS Distributions 			_assert(!tracking_policy::did_trap); // offset is now right at its positive limit
175*5e3eaea3SApple OSS Distributions 			ptr += 1;
176*5e3eaea3SApple OSS Distributions 			_assert(tracking_policy::did_trap);
177*5e3eaea3SApple OSS Distributions 		}
178*5e3eaea3SApple OSS Distributions 
179*5e3eaea3SApple OSS Distributions 		// Add negative offsets
180*5e3eaea3SApple OSS Distributions 		{
181*5e3eaea3SApple OSS Distributions 			libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
182*5e3eaea3SApple OSS Distributions 			tracking_policy::did_trap = false;
183*5e3eaea3SApple OSS Distributions 			ptr += static_cast<ptrdiff_t>(min_n / 2);
184*5e3eaea3SApple OSS Distributions 			_assert(!tracking_policy::did_trap);
185*5e3eaea3SApple OSS Distributions 			ptr += static_cast<ptrdiff_t>(min_n / 2);
186*5e3eaea3SApple OSS Distributions 			_assert(!tracking_policy::did_trap);
187*5e3eaea3SApple OSS Distributions 			ptr += (min_n % 2);
188*5e3eaea3SApple OSS Distributions 			_assert(!tracking_policy::did_trap); // offset is now right at its negative limit
189*5e3eaea3SApple OSS Distributions 			ptr += -1;
190*5e3eaea3SApple OSS Distributions 			_assert(tracking_policy::did_trap);
191*5e3eaea3SApple OSS Distributions 		}
192*5e3eaea3SApple OSS Distributions 	}
193*5e3eaea3SApple OSS Distributions }
194*5e3eaea3SApple OSS Distributions 
195*5e3eaea3SApple OSS Distributions T_DECL(arith_add_assign, "bounded_ptr.arith.add_assign") {
196*5e3eaea3SApple OSS Distributions 	tests<T, T>();
197*5e3eaea3SApple OSS Distributions 	tests<T, T const>();
198*5e3eaea3SApple OSS Distributions 	tests<T, T volatile>();
199*5e3eaea3SApple OSS Distributions 	tests<T, T const volatile>();
200*5e3eaea3SApple OSS Distributions }
201