]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // Tests for | |
3 | // template <typename U> | |
4 | // intrusive_shared_ptr& operator=(intrusive_shared_ptr<U, RefcountPolicy>&& other); | |
5 | // | |
6 | // intrusive_shared_ptr& operator=(intrusive_shared_ptr&& other); | |
7 | // | |
8 | ||
9 | #include <libkern/c++/intrusive_shared_ptr.h> | |
10 | #include <darwintest.h> | |
11 | #include <type_traits> | |
12 | #include <utility> | |
13 | #include "test_policy.h" | |
14 | ||
15 | struct Base { int i; }; | |
16 | struct Derived : Base { }; | |
17 | ||
18 | struct Base1 { int i; }; | |
19 | struct Base2 { long l; }; | |
20 | struct DerivedMultiple : Base1, Base2 { | |
21 | DerivedMultiple(int i) : Base1{i}, Base2{i + 10} | |
22 | { | |
23 | } | |
24 | }; | |
25 | ||
26 | struct Unrelated { }; | |
27 | ||
28 | template <typename Stored, typename From, typename To> | |
29 | static void | |
30 | tests() | |
31 | { | |
32 | Stored obj1{1}; | |
33 | Stored obj2{2}; | |
34 | ||
35 | // Move-assign non-null to non-null | |
36 | { | |
37 | tracked_shared_ptr<From> from(&obj1, libkern::retain); | |
38 | tracked_shared_ptr<To> to(&obj2, libkern::retain); | |
39 | tracking_policy::reset(); | |
40 | tracked_shared_ptr<To>& ref = (to = std::move(from)); | |
41 | CHECK(tracking_policy::releases == 1); | |
42 | CHECK(tracking_policy::retains == 0); | |
43 | CHECK(&ref == &to); | |
44 | CHECK(from.get() == nullptr); | |
45 | CHECK(to.get() == &obj1); | |
46 | } | |
47 | ||
48 | // Move-assign non-null to null | |
49 | { | |
50 | tracked_shared_ptr<From> from(&obj1, libkern::retain); | |
51 | tracked_shared_ptr<To> to = nullptr; | |
52 | tracking_policy::reset(); | |
53 | tracked_shared_ptr<To>& ref = (to = std::move(from)); | |
54 | CHECK(tracking_policy::releases == 0); | |
55 | CHECK(tracking_policy::retains == 0); | |
56 | CHECK(&ref == &to); | |
57 | CHECK(from.get() == nullptr); | |
58 | CHECK(to.get() == &obj1); | |
59 | } | |
60 | ||
61 | // Move-assign null to non-null | |
62 | { | |
63 | tracked_shared_ptr<From> from = nullptr; | |
64 | tracked_shared_ptr<To> to(&obj2, libkern::retain); | |
65 | tracking_policy::reset(); | |
66 | tracked_shared_ptr<To>& ref = (to = std::move(from)); | |
67 | CHECK(tracking_policy::releases == 1); | |
68 | CHECK(tracking_policy::retains == 0); | |
69 | CHECK(&ref == &to); | |
70 | CHECK(from.get() == nullptr); | |
71 | CHECK(to.get() == nullptr); | |
72 | } | |
73 | ||
74 | // Move-assign null to null | |
75 | { | |
76 | tracked_shared_ptr<From> from = nullptr; | |
77 | tracked_shared_ptr<To> to = nullptr; | |
78 | tracking_policy::reset(); | |
79 | tracked_shared_ptr<To>& ref = (to = std::move(from)); | |
80 | CHECK(tracking_policy::releases == 0); | |
81 | CHECK(tracking_policy::retains == 0); | |
82 | CHECK(&ref == &to); | |
83 | CHECK(from.get() == nullptr); | |
84 | CHECK(to.get() == nullptr); | |
85 | } | |
86 | } | |
87 | ||
88 | T_DECL(assign_move, "intrusive_shared_ptr.assign.move") { | |
89 | tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived>(); | |
90 | tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived const>(); | |
91 | tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Derived const>(); | |
92 | ||
93 | tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>(); | |
94 | tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base const>(); | |
95 | tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>(); | |
96 | ||
97 | tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base1>(); | |
98 | tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base1 const>(); | |
99 | ||
100 | tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base2>(); | |
101 | tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base2 const>(); | |
102 | ||
103 | // Make sure basic trait querying works | |
104 | static_assert(std::is_move_assignable_v<test_shared_ptr<Derived> >); | |
105 | ||
106 | // Make sure downcasts are disabled | |
107 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Derived>, /*from*/ test_shared_ptr<Base>&&>); | |
108 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<DerivedMultiple>, /*from*/ test_shared_ptr<Base1>&&>); | |
109 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<DerivedMultiple>, /*from*/ test_shared_ptr<Base2>&&>); | |
110 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Base2>, /*from*/ test_shared_ptr<Base1>&&>); | |
111 | ||
112 | // Make sure const-casting away doesn't work | |
113 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Derived>, /*from*/ test_shared_ptr<Derived const>&&>); | |
114 | ||
115 | // Make sure casting to unrelated types doesn't work | |
116 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<char>, /*from*/ test_shared_ptr<Derived>&&>); | |
117 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Unrelated>, /*from*/ test_shared_ptr<Derived>&&>); | |
118 | static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Base2>, /*from*/ test_shared_ptr<Base1>&&>); | |
119 | ||
120 | // Make sure constructing with different policies doesn't work | |
121 | static_assert(!std::is_assignable_v</*to*/ libkern::intrusive_shared_ptr<Derived, dummy_policy<2> >, /*from*/ libkern::intrusive_shared_ptr<Derived, dummy_policy<1> >&&>); | |
122 | } |