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