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