xref: /xnu-8020.140.41/tests/bounded_ptr_src/assign.convert.cpp (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions //
2*27b03b36SApple OSS Distributions // Tests for
3*27b03b36SApple OSS Distributions //  template <typename U, typename Policy>
4*27b03b36SApple OSS Distributions //  bounded_ptr& operator=(bounded_ptr<U, Policy> const& other);
5*27b03b36SApple OSS Distributions //
6*27b03b36SApple OSS Distributions 
7*27b03b36SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
8*27b03b36SApple OSS Distributions #include <array>
9*27b03b36SApple OSS Distributions #include <darwintest.h>
10*27b03b36SApple OSS Distributions #include <darwintest_utils.h>
11*27b03b36SApple OSS Distributions #include "test_utils.h"
12*27b03b36SApple OSS Distributions 
13*27b03b36SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
14*27b03b36SApple OSS Distributions 
15*27b03b36SApple OSS Distributions struct Base { int i; };
16*27b03b36SApple OSS Distributions struct Derived : Base { };
17*27b03b36SApple OSS Distributions 
18*27b03b36SApple OSS Distributions struct Base1 { int i; };
19*27b03b36SApple OSS Distributions struct Base2 { long l; };
20*27b03b36SApple OSS Distributions struct DerivedMultiple : Base1, Base2 {
DerivedMultipleDerivedMultiple21*27b03b36SApple OSS Distributions 	DerivedMultiple(int i) : Base1{i}, Base2{i + 10}
22*27b03b36SApple OSS Distributions 	{
23*27b03b36SApple OSS Distributions 	}
24*27b03b36SApple OSS Distributions };
25*27b03b36SApple OSS Distributions 
26*27b03b36SApple OSS Distributions struct Unrelated { };
27*27b03b36SApple OSS Distributions 
28*27b03b36SApple OSS Distributions struct dummy_policy1 {
29*27b03b36SApple OSS Distributions 	static constexpr void
trapdummy_policy130*27b03b36SApple OSS Distributions 	trap(char const*)
31*27b03b36SApple OSS Distributions 	{
32*27b03b36SApple OSS Distributions 	}
33*27b03b36SApple OSS Distributions };
34*27b03b36SApple OSS Distributions struct dummy_policy2 {
35*27b03b36SApple OSS Distributions 	static constexpr void
trapdummy_policy236*27b03b36SApple OSS Distributions 	trap(char const*)
37*27b03b36SApple OSS Distributions 	{
38*27b03b36SApple OSS Distributions 	}
39*27b03b36SApple OSS Distributions };
40*27b03b36SApple OSS Distributions 
41*27b03b36SApple OSS Distributions template <typename Stored, typename From, typename To>
42*27b03b36SApple OSS Distributions static void
tests()43*27b03b36SApple OSS Distributions tests()
44*27b03b36SApple OSS Distributions {
45*27b03b36SApple OSS Distributions 	std::array<Stored, 5> array = {Stored{0}, Stored{1}, Stored{2}, Stored{3}, Stored{4}};
46*27b03b36SApple OSS Distributions 	Stored* const ptr1 = array.begin() + 2;
47*27b03b36SApple OSS Distributions 	Stored* const ptr2 = array.begin() + 3;
48*27b03b36SApple OSS Distributions 
49*27b03b36SApple OSS Distributions 	{
50*27b03b36SApple OSS Distributions 		test_bounded_ptr<From> const from(ptr1, array.begin(), array.end());
51*27b03b36SApple OSS Distributions 		test_bounded_ptr<To> to;
52*27b03b36SApple OSS Distributions 		test_bounded_ptr<To>& ref = (to = from);
53*27b03b36SApple OSS Distributions 		_assert(to.discard_bounds() == static_cast<To const*>(ptr1));
54*27b03b36SApple OSS Distributions 		_assert(&ref == &to); // make sure we return *this
55*27b03b36SApple OSS Distributions 	}
56*27b03b36SApple OSS Distributions 
57*27b03b36SApple OSS Distributions 	// Test assigning to a non-null pointer
58*27b03b36SApple OSS Distributions 	{
59*27b03b36SApple OSS Distributions 		test_bounded_ptr<From> const from(ptr1, array.begin(), array.end());
60*27b03b36SApple OSS Distributions 		test_bounded_ptr<To> to(ptr2, array.begin(), array.end());
61*27b03b36SApple OSS Distributions 		_assert(to.discard_bounds() == static_cast<To const*>(ptr2));
62*27b03b36SApple OSS Distributions 
63*27b03b36SApple OSS Distributions 		test_bounded_ptr<To>& ref = (to = from);
64*27b03b36SApple OSS Distributions 		_assert(to.discard_bounds() == static_cast<To const*>(ptr1));
65*27b03b36SApple OSS Distributions 		_assert(&ref == &to); // make sure we return *this
66*27b03b36SApple OSS Distributions 	}
67*27b03b36SApple OSS Distributions 
68*27b03b36SApple OSS Distributions 	// Test assigning from a null pointer
69*27b03b36SApple OSS Distributions 	{
70*27b03b36SApple OSS Distributions 		test_bounded_ptr<From> const from = nullptr;
71*27b03b36SApple OSS Distributions 		test_bounded_ptr<To> to;
72*27b03b36SApple OSS Distributions 		test_bounded_ptr<To>& ref = (to = from);
73*27b03b36SApple OSS Distributions 		_assert(to.unsafe_discard_bounds() == nullptr);
74*27b03b36SApple OSS Distributions 		_assert(&ref == &to); // make sure we return *this
75*27b03b36SApple OSS Distributions 	}
76*27b03b36SApple OSS Distributions 
77*27b03b36SApple OSS Distributions 	// Test with different policies
78*27b03b36SApple OSS Distributions 	{
79*27b03b36SApple OSS Distributions 		libkern::bounded_ptr<From, dummy_policy1> from(ptr1, array.begin(), array.end());
80*27b03b36SApple OSS Distributions 		libkern::bounded_ptr<To, dummy_policy2> to;
81*27b03b36SApple OSS Distributions 		libkern::bounded_ptr<To, dummy_policy2>& ref = (to = from);
82*27b03b36SApple OSS Distributions 		_assert(to.discard_bounds() == static_cast<To const*>(ptr1));
83*27b03b36SApple OSS Distributions 		_assert(&ref == &to); // make sure we return *this
84*27b03b36SApple OSS Distributions 	}
85*27b03b36SApple OSS Distributions 
86*27b03b36SApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
87*27b03b36SApple OSS Distributions 	//   ^        ^                                       ^
88*27b03b36SApple OSS Distributions 	//   |        |                                       |
89*27b03b36SApple OSS Distributions 	// from     begin                                    end
90*27b03b36SApple OSS Distributions 	{
91*27b03b36SApple OSS Distributions 		test_bounded_ptr<From> const from(array.begin(), array.begin() + 1, array.end());
92*27b03b36SApple OSS Distributions 		test_bounded_ptr<To> to;
93*27b03b36SApple OSS Distributions 		to = from;
94*27b03b36SApple OSS Distributions 		_assert(to.unsafe_discard_bounds() == static_cast<To const*>(array.begin()));
95*27b03b36SApple OSS Distributions 	}
96*27b03b36SApple OSS Distributions }
97*27b03b36SApple OSS Distributions 
98*27b03b36SApple OSS Distributions T_DECL(assign_convert, "bounded_ptr.assign.convert") {
99*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived>();
100*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Derived const>();
101*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ Derived volatile>();
102*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ Derived const volatile>();
103*27b03b36SApple OSS Distributions 
104*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>();
105*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>();
106*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ Base volatile>();
107*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ Base const volatile>();
108*27b03b36SApple OSS Distributions 
109*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base1>();
110*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base1 const>();
111*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple volatile, /*to*/ Base1 volatile>();
112*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const volatile, /*to*/ Base1 const volatile>();
113*27b03b36SApple OSS Distributions 
114*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base2>();
115*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base2 const>();
116*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple volatile, /*to*/ Base2 volatile>();
117*27b03b36SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const volatile, /*to*/ Base2 const volatile>();
118*27b03b36SApple OSS Distributions 
119*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ void>();
120*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ void const>();
121*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ void volatile>();
122*27b03b36SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ void const volatile>();
123*27b03b36SApple OSS Distributions 
124*27b03b36SApple OSS Distributions 	// Make sure downcasts are disabled
125*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<Derived>, /*from*/ test_bounded_ptr<Base> >);
126*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<DerivedMultiple>, /*from*/ test_bounded_ptr<Base1> >);
127*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<DerivedMultiple>, /*from*/ test_bounded_ptr<Base2> >);
128*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<Base2>, /*from*/ test_bounded_ptr<Base1> >);
129*27b03b36SApple OSS Distributions 
130*27b03b36SApple OSS Distributions 	// Make sure const-casting away doesn't work
131*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<Derived>, /*from*/ test_bounded_ptr<Derived const> >);
132*27b03b36SApple OSS Distributions 
133*27b03b36SApple OSS Distributions 	// Make sure casting to unrelated types doesn't work implicitly
134*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<char>, /*from*/ test_bounded_ptr<Derived> >);
135*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<Unrelated>, /*from*/ test_bounded_ptr<Derived> >);
136*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<Base2>, /*from*/ test_bounded_ptr<Base1> >);
137*27b03b36SApple OSS Distributions 
138*27b03b36SApple OSS Distributions 	// Make sure we can't assign from raw pointers
139*27b03b36SApple OSS Distributions 	static_assert(!std::is_assignable_v</*to*/ test_bounded_ptr<Derived>, /*from*/ Derived*>);
140*27b03b36SApple OSS Distributions }
141