xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / bounded_ptr_src / ctor.convert.cpp
CommitLineData
f427ee49
A
1//
2// Tests for
3// template <typename U, typename Policy>
4// bounded_ptr(bounded_ptr<U, Policy> const& other);
5//
6
7#include <libkern/c++/bounded_ptr.h>
8#include <array>
9#include <type_traits>
10#include <darwintest.h>
11#include <darwintest_utils.h>
12#include "test_utils.h"
13
14#define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
15
16struct Base { int i; };
17struct Derived : Base { };
18
19struct Base1 { int i; };
20struct Base2 { long l; };
21struct DerivedMultiple : Base1, Base2 {
22 DerivedMultiple(int i) : Base1{i}, Base2{i + 10}
23 {
24 }
25};
26
27struct Unrelated { };
28
29struct dummy_policy1 {
30 static constexpr void
31 trap(char const*)
32 {
33 }
34};
35struct dummy_policy2 {
36 static constexpr void
37 trap(char const*)
38 {
39 }
40};
41
42template <typename Stored, typename From, typename To>
43static void
44tests()
45{
46 std::array<Stored, 5> array = {Stored{0}, Stored{1}, Stored{2}, Stored{3}, Stored{4}};
47 Stored* const ptr = array.begin() + 2;
48
49 {
50 test_bounded_ptr<From> const from(ptr, array.begin(), array.end());
51 test_bounded_ptr<To> to = from; // conversion (implicit)
52 _assert(to.discard_bounds() == static_cast<To const*>(ptr));
53 }
54 {
55 test_bounded_ptr<From> const from(ptr, array.begin(), array.end());
56 test_bounded_ptr<To> to(from); // conversion (explicit)
57 _assert(to.discard_bounds() == static_cast<To const*>(ptr));
58 }
59 {
60 test_bounded_ptr<From> const from(ptr, array.begin(), array.end());
61 test_bounded_ptr<To> to{from}; // conversion (explicit)
62 _assert(to.discard_bounds() == static_cast<To const*>(ptr));
63 }
64 {
65 test_bounded_ptr<From> const from(ptr, array.begin(), array.end());
66 test_bounded_ptr<To> to = static_cast<test_bounded_ptr<To> >(from); // conversion (explicit)
67 _assert(to.discard_bounds() == static_cast<To const*>(ptr));
68 }
69
70 // Test converting from a null pointer
71 {
72 test_bounded_ptr<From> from = nullptr;
73 test_bounded_ptr<To> to = from; // conversion (implicit)
74 _assert(to.unsafe_discard_bounds() == nullptr);
75 }
76
77 // Test with different policies
78 {
79 libkern::bounded_ptr<From, dummy_policy1> from(ptr, array.begin(), array.end());
80 libkern::bounded_ptr<To, dummy_policy2> to = from; // conversion (implicit)
81 _assert(to.discard_bounds() == static_cast<To const*>(ptr));
82 }
83
84 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
85 // ^ ^ ^
86 // | | |
87 // from begin end
88 {
89 test_bounded_ptr<From> const from(array.begin(), array.begin() + 1, array.end());
90 test_bounded_ptr<To> to(from);
91 _assert(to.unsafe_discard_bounds() == static_cast<To const*>(array.begin()));
92 }
93}
94
95T_DECL(ctor_convert, "bounded_ptr.ctor.convert") {
96 tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived>();
97 tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Derived const>();
98 tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ Derived volatile>();
99 tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ Derived const volatile>();
100
101 tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>();
102 tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>();
103 tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ Base volatile>();
104 tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ Base const volatile>();
105
106 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base1>();
107 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base1 const>();
108 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple volatile, /*to*/ Base1 volatile>();
109 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const volatile, /*to*/ Base1 const volatile>();
110
111 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base2>();
112 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base2 const>();
113 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple volatile, /*to*/ Base2 volatile>();
114 tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const volatile, /*to*/ Base2 const volatile>();
115
116 tests</*stored*/ Derived, /*from*/ Derived, /*to*/ void>();
117 tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ void const>();
118 tests</*stored*/ Derived, /*from*/ Derived volatile, /*to*/ void volatile>();
119 tests</*stored*/ Derived, /*from*/ Derived const volatile, /*to*/ void const volatile>();
120
121 // Make sure downcasts are disabled
122 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Base>, /*to*/ test_bounded_ptr<Derived> >);
123 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Base1>, /*to*/ test_bounded_ptr<DerivedMultiple> >);
124 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Base2>, /*to*/ test_bounded_ptr<DerivedMultiple> >);
125 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Base1>, /*to*/ test_bounded_ptr<Base2> >);
126
127 // Make sure const-casting away doesn't work
128 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Derived const>, /*to*/ test_bounded_ptr<Derived> >);
129
130 // Make sure casting to unrelated types doesn't work implicitly
131 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Derived>, /*to*/ test_bounded_ptr<char> >);
132 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Derived>, /*to*/ test_bounded_ptr<Unrelated> >);
133 static_assert(!std::is_convertible_v</*from*/ test_bounded_ptr<Base1>, /*to*/ test_bounded_ptr<Base2> >);
134
135 // Make sure even explicit conversion to unrelated types doesn't work
136 static_assert(!std::is_constructible_v</*to*/ test_bounded_ptr<char>, /*from*/ test_bounded_ptr<Derived> >);
137 static_assert(!std::is_constructible_v</*to*/ test_bounded_ptr<Unrelated>, /*from*/ test_bounded_ptr<Derived> >);
138 static_assert(!std::is_constructible_v</*to*/ test_bounded_ptr<Base2>, /*from*/ test_bounded_ptr<Base1> >);
139
140 // Make sure construction from a raw pointer doesn't work
141 static_assert(!std::is_constructible_v</*to*/ test_bounded_ptr<Derived>, /*from*/ Derived*>);
142}