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