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