xref: /xnu-10063.121.3/tests/bounded_ptr_src/arith.difference.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions //
2*2c2f96dcSApple OSS Distributions // Tests for
3*2c2f96dcSApple OSS Distributions //  friend std::ptrdiff_t operator-(bounded_ptr const& a, bounded_ptr const& b);
4*2c2f96dcSApple OSS Distributions //  friend std::ptrdiff_t operator-(bounded_ptr const& a, T* b);
5*2c2f96dcSApple OSS Distributions //  friend std::ptrdiff_t operator-(T* a, bounded_ptr const& b);
6*2c2f96dcSApple OSS Distributions //
7*2c2f96dcSApple OSS Distributions 
8*2c2f96dcSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
9*2c2f96dcSApple OSS Distributions #include <array>
10*2c2f96dcSApple OSS Distributions #include <darwintest.h>
11*2c2f96dcSApple OSS Distributions #include <darwintest_utils.h>
12*2c2f96dcSApple OSS Distributions #include "test_utils.h"
13*2c2f96dcSApple OSS Distributions 
14*2c2f96dcSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
15*2c2f96dcSApple OSS Distributions 
16*2c2f96dcSApple OSS Distributions struct T {
17*2c2f96dcSApple OSS Distributions 	int i;
18*2c2f96dcSApple OSS Distributions };
19*2c2f96dcSApple OSS Distributions 
20*2c2f96dcSApple OSS Distributions template <typename Stored, typename Left, typename Right>
21*2c2f96dcSApple OSS Distributions static void
tests()22*2c2f96dcSApple OSS Distributions tests()
23*2c2f96dcSApple OSS Distributions {
24*2c2f96dcSApple OSS Distributions 	std::array<Stored, 5> array = {Stored{0}, Stored{1}, Stored{2}, Stored{3}, Stored{4}};
25*2c2f96dcSApple OSS Distributions 
26*2c2f96dcSApple OSS Distributions 	// a >= b
27*2c2f96dcSApple OSS Distributions 	{
28*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
29*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
30*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
31*2c2f96dcSApple OSS Distributions 		_assert(diff == 0);
32*2c2f96dcSApple OSS Distributions 	}
33*2c2f96dcSApple OSS Distributions 	{
34*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin() + 1, array.begin(), array.end());
35*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
36*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
37*2c2f96dcSApple OSS Distributions 		_assert(diff == 1);
38*2c2f96dcSApple OSS Distributions 	}
39*2c2f96dcSApple OSS Distributions 	{
40*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin() + 2, array.begin(), array.end());
41*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
42*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
43*2c2f96dcSApple OSS Distributions 		_assert(diff == 2);
44*2c2f96dcSApple OSS Distributions 	}
45*2c2f96dcSApple OSS Distributions 	{
46*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin() + 3, array.begin(), array.end());
47*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
48*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
49*2c2f96dcSApple OSS Distributions 		_assert(diff == 3);
50*2c2f96dcSApple OSS Distributions 	}
51*2c2f96dcSApple OSS Distributions 	{
52*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin() + 4, array.begin(), array.end());
53*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
54*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
55*2c2f96dcSApple OSS Distributions 		_assert(diff == 4);
56*2c2f96dcSApple OSS Distributions 	}
57*2c2f96dcSApple OSS Distributions 	{
58*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.end(), array.begin(), array.end());
59*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
60*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
61*2c2f96dcSApple OSS Distributions 		_assert(diff == 5);
62*2c2f96dcSApple OSS Distributions 	}
63*2c2f96dcSApple OSS Distributions 
64*2c2f96dcSApple OSS Distributions 	// a < b
65*2c2f96dcSApple OSS Distributions 	{
66*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
67*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin() + 1, array.begin(), array.end());
68*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
69*2c2f96dcSApple OSS Distributions 		_assert(diff == -1);
70*2c2f96dcSApple OSS Distributions 	}
71*2c2f96dcSApple OSS Distributions 	{
72*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
73*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin() + 2, array.begin(), array.end());
74*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
75*2c2f96dcSApple OSS Distributions 		_assert(diff == -2);
76*2c2f96dcSApple OSS Distributions 	}
77*2c2f96dcSApple OSS Distributions 	{
78*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
79*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin() + 3, array.begin(), array.end());
80*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
81*2c2f96dcSApple OSS Distributions 		_assert(diff == -3);
82*2c2f96dcSApple OSS Distributions 	}
83*2c2f96dcSApple OSS Distributions 	{
84*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
85*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin() + 4, array.begin(), array.end());
86*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
87*2c2f96dcSApple OSS Distributions 		_assert(diff == -4);
88*2c2f96dcSApple OSS Distributions 	}
89*2c2f96dcSApple OSS Distributions 	{
90*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
91*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin() + 5, array.begin(), array.end());
92*2c2f96dcSApple OSS Distributions 		std::ptrdiff_t diff = a - b;
93*2c2f96dcSApple OSS Distributions 		_assert(diff == -5);
94*2c2f96dcSApple OSS Distributions 	}
95*2c2f96dcSApple OSS Distributions 
96*2c2f96dcSApple OSS Distributions 	// Subtract pointers with different bounds
97*2c2f96dcSApple OSS Distributions 	{
98*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin() + 2, array.begin() + 1, array.end() - 1);
99*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin() + 4, array.begin() + 3, array.end());
100*2c2f96dcSApple OSS Distributions 		_assert(a - b == -2);
101*2c2f96dcSApple OSS Distributions 		_assert(b - a == 2);
102*2c2f96dcSApple OSS Distributions 	}
103*2c2f96dcSApple OSS Distributions 
104*2c2f96dcSApple OSS Distributions 	// Subtract with raw pointers
105*2c2f96dcSApple OSS Distributions 	{
106*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Left> const a(array.begin() + 2, array.begin() + 1, array.end() - 1);
107*2c2f96dcSApple OSS Distributions 		Right* b = array.begin() + 4;
108*2c2f96dcSApple OSS Distributions 		_assert(a - b == -2);
109*2c2f96dcSApple OSS Distributions 	}
110*2c2f96dcSApple OSS Distributions 	{
111*2c2f96dcSApple OSS Distributions 		Left* a = array.begin() + 4;
112*2c2f96dcSApple OSS Distributions 		test_bounded_ptr<Right> const b(array.begin() + 2, array.begin() + 1, array.end() - 1);
113*2c2f96dcSApple OSS Distributions 		_assert(a - b == 2);
114*2c2f96dcSApple OSS Distributions 	}
115*2c2f96dcSApple OSS Distributions }
116*2c2f96dcSApple OSS Distributions 
117*2c2f96dcSApple OSS Distributions T_DECL(arith_difference, "bounded_ptr.arith.difference") {
118*2c2f96dcSApple OSS Distributions 	tests<T, T, T>();
119*2c2f96dcSApple OSS Distributions 	tests<T, T, T const>();
120*2c2f96dcSApple OSS Distributions 	tests<T, T const, T>();
121*2c2f96dcSApple OSS Distributions 	tests<T, T const, T const>();
122*2c2f96dcSApple OSS Distributions }
123