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