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