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