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