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