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