xref: /xnu-12377.81.4/tests/bounded_ptr_src/reinterpret_cast.cpp (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions //
2*043036a2SApple OSS Distributions // Tests for
3*043036a2SApple OSS Distributions //  template <typename T, typename U, typename Policy>
4*043036a2SApple OSS Distributions //  bounded_ptr<T, Policy> reinterpret_pointer_cast(bounded_ptr<U, Policy> const& p) noexcept
5*043036a2SApple OSS Distributions //
6*043036a2SApple OSS Distributions 
7*043036a2SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
8*043036a2SApple OSS Distributions #include <array>
9*043036a2SApple OSS Distributions #include <darwintest.h>
10*043036a2SApple OSS Distributions #include <darwintest_utils.h>
11*043036a2SApple OSS Distributions #include "test_utils.h"
12*043036a2SApple OSS Distributions 
13*043036a2SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
14*043036a2SApple OSS Distributions 
15*043036a2SApple OSS Distributions struct Base { int i; };
16*043036a2SApple OSS Distributions struct Derived : Base { };
17*043036a2SApple OSS Distributions 
18*043036a2SApple OSS Distributions struct Base1 { int i; };
19*043036a2SApple OSS Distributions struct Base2 { long l; };
20*043036a2SApple OSS Distributions struct DerivedMultiple : Base1, Base2 {
DerivedMultipleDerivedMultiple21*043036a2SApple OSS Distributions 	DerivedMultiple(int i) : Base1{i}, Base2{i + 10}
22*043036a2SApple OSS Distributions 	{
23*043036a2SApple OSS Distributions 	}
24*043036a2SApple OSS Distributions };
25*043036a2SApple OSS Distributions 
26*043036a2SApple OSS Distributions struct non_default_policy {
27*043036a2SApple OSS Distributions 	static constexpr void
trapnon_default_policy28*043036a2SApple OSS Distributions 	trap(char const*)
29*043036a2SApple OSS Distributions 	{
30*043036a2SApple OSS Distributions 	}
31*043036a2SApple OSS Distributions };
32*043036a2SApple OSS Distributions 
33*043036a2SApple OSS Distributions template <typename Stored, typename From, typename To>
34*043036a2SApple OSS Distributions static void
tests()35*043036a2SApple OSS Distributions tests()
36*043036a2SApple OSS Distributions {
37*043036a2SApple OSS Distributions 	std::array<Stored, 5> array = {Stored{0}, Stored{1}, Stored{2}, Stored{3}, Stored{4}};
38*043036a2SApple OSS Distributions 
39*043036a2SApple OSS Distributions 	{
40*043036a2SApple OSS Distributions 		test_bounded_ptr<From> from(array.begin() + 2, array.begin(), array.end());
41*043036a2SApple OSS Distributions 		test_bounded_ptr<To> to = libkern::reinterpret_pointer_cast<To>(from);
42*043036a2SApple OSS Distributions 		_assert(to.discard_bounds() == reinterpret_cast<To const*>(from.discard_bounds()));
43*043036a2SApple OSS Distributions 	}
44*043036a2SApple OSS Distributions 
45*043036a2SApple OSS Distributions 	{
46*043036a2SApple OSS Distributions 		test_bounded_ptr<From> from(array.begin() + 2, array.begin(), array.end());
47*043036a2SApple OSS Distributions 		test_bounded_ptr<To> to = libkern::reinterpret_pointer_cast<To>(from);
48*043036a2SApple OSS Distributions 		_assert(to.discard_bounds() == reinterpret_cast<To const volatile*>(from.discard_bounds()));
49*043036a2SApple OSS Distributions 	}
50*043036a2SApple OSS Distributions 
51*043036a2SApple OSS Distributions 	// Test `reinterpret_pointer_cast`ing a null pointer
52*043036a2SApple OSS Distributions 	{
53*043036a2SApple OSS Distributions 		test_bounded_ptr<From> from(nullptr, nullptr, nullptr);
54*043036a2SApple OSS Distributions 		test_bounded_ptr<To> to = libkern::reinterpret_pointer_cast<To>(from);
55*043036a2SApple OSS Distributions 		_assert(to.unsafe_discard_bounds() == nullptr);
56*043036a2SApple OSS Distributions 	}
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions 	// Test with a non-default policy
59*043036a2SApple OSS Distributions 	{
60*043036a2SApple OSS Distributions 		libkern::bounded_ptr<From, non_default_policy> from(array.begin(), array.begin(), array.end());
61*043036a2SApple OSS Distributions 		libkern::bounded_ptr<To, non_default_policy> to = libkern::reinterpret_pointer_cast<To>(from);
62*043036a2SApple OSS Distributions 		_assert(to.discard_bounds() == reinterpret_cast<To const*>(from.discard_bounds()));
63*043036a2SApple OSS Distributions 	}
64*043036a2SApple OSS Distributions }
65*043036a2SApple OSS Distributions 
66*043036a2SApple OSS Distributions T_DECL(reinterpret_cast_, "bounded_ptr.reinterpret_cast", T_META_TAG_VM_PREFERRED) {
67*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>();
68*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>();
69*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ Base volatile>();
70*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ Base const volatile>();
71*043036a2SApple OSS Distributions 
72*043036a2SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base1>();
73*043036a2SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base1 const>();
74*043036a2SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple volatile, /*to*/ Base1 volatile>();
75*043036a2SApple OSS Distributions 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const volatile, /*to*/ Base1 const volatile>();
76*043036a2SApple OSS Distributions 
77*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ void>();
78*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ void const>();
79*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ void volatile>();
80*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ void const volatile>();
81*043036a2SApple OSS Distributions 
82*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ char>();
83*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ char const>();
84*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ char volatile>();
85*043036a2SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ char const volatile>();
86*043036a2SApple OSS Distributions }
87