// // Tests for // template // bounded_ptr reinterpret_pointer_cast(bounded_ptr const& p) noexcept // #include #include #include #include #include "test_utils.h" #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__) struct Base { int i; }; struct Derived : Base { }; struct Base1 { int i; }; struct Base2 { long l; }; struct DerivedMultiple : Base1, Base2 { DerivedMultiple(int i) : Base1{i}, Base2{i + 10} { } }; struct non_default_policy { static constexpr void trap(char const*) { } }; template static void tests() { std::array array = {Stored{0}, Stored{1}, Stored{2}, Stored{3}, Stored{4}}; { test_bounded_ptr from(array.begin() + 2, array.begin(), array.end()); test_bounded_ptr to = libkern::reinterpret_pointer_cast(from); _assert(to.discard_bounds() == reinterpret_cast(from.discard_bounds())); } { test_bounded_ptr from(array.begin() + 2, array.begin(), array.end()); test_bounded_ptr to = libkern::reinterpret_pointer_cast(from); _assert(to.discard_bounds() == reinterpret_cast(from.discard_bounds())); } // Test `reinterpret_pointer_cast`ing a null pointer { test_bounded_ptr from(nullptr, nullptr, nullptr); test_bounded_ptr to = libkern::reinterpret_pointer_cast(from); _assert(to.unsafe_discard_bounds() == nullptr); } // Test with a non-default policy { libkern::bounded_ptr from(array.begin(), array.begin(), array.end()); libkern::bounded_ptr to = libkern::reinterpret_pointer_cast(from); _assert(to.discard_bounds() == reinterpret_cast(from.discard_bounds())); } } T_DECL(reinterpret_cast_, "bounded_ptr.reinterpret_cast", T_META_TAG_VM_PREFERRED) { tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); tests(); }