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