1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions // template <typename U>
4bb611c8fSApple OSS Distributions // intrusive_shared_ptr(intrusive_shared_ptr<U, RefcountPolicy> const& other);
5bb611c8fSApple OSS Distributions //
6bb611c8fSApple OSS Distributions // intrusive_shared_ptr(intrusive_shared_ptr const& other);
7bb611c8fSApple OSS Distributions //
8bb611c8fSApple OSS Distributions
9bb611c8fSApple OSS Distributions #include <libkern/c++/intrusive_shared_ptr.h>
10bb611c8fSApple OSS Distributions #include <darwintest.h>
11bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
12bb611c8fSApple OSS Distributions #include <type_traits>
13bb611c8fSApple OSS Distributions #include "test_policy.h"
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 template <typename Stored, typename From, typename To>
29bb611c8fSApple OSS Distributions static void
tests()30bb611c8fSApple OSS Distributions tests()
31bb611c8fSApple OSS Distributions {
32bb611c8fSApple OSS Distributions Stored obj{3};
33bb611c8fSApple OSS Distributions
34bb611c8fSApple OSS Distributions // Test with non-null pointers
35bb611c8fSApple OSS Distributions {
36bb611c8fSApple OSS Distributions test_policy::retain_count = 0;
37bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<From, test_policy> const from(&obj, libkern::retain);
38bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<To, test_policy> to(from); // explicit
39bb611c8fSApple OSS Distributions CHECK(test_policy::retain_count == 2);
40bb611c8fSApple OSS Distributions CHECK(to.get() == &obj);
41bb611c8fSApple OSS Distributions }
42bb611c8fSApple OSS Distributions {
43bb611c8fSApple OSS Distributions test_policy::retain_count = 0;
44bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<From, test_policy> const from(&obj, libkern::retain);
45bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<To, test_policy> to{from}; // explicit
46bb611c8fSApple OSS Distributions CHECK(test_policy::retain_count == 2);
47bb611c8fSApple OSS Distributions CHECK(to.get() == &obj);
48bb611c8fSApple OSS Distributions }
49bb611c8fSApple OSS Distributions {
50bb611c8fSApple OSS Distributions test_policy::retain_count = 0;
51bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<From, test_policy> const from(&obj, libkern::retain);
52bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<To, test_policy> to = from; // implicit
53bb611c8fSApple OSS Distributions CHECK(test_policy::retain_count == 2);
54bb611c8fSApple OSS Distributions CHECK(to.get() == &obj);
55bb611c8fSApple OSS Distributions }
56bb611c8fSApple OSS Distributions
57bb611c8fSApple OSS Distributions // Test with a null pointer
58bb611c8fSApple OSS Distributions {
59bb611c8fSApple OSS Distributions test_policy::retain_count = 0;
60bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<From, test_policy> const from = nullptr;
61bb611c8fSApple OSS Distributions libkern::intrusive_shared_ptr<To, test_policy> to = from;
62bb611c8fSApple OSS Distributions CHECK(test_policy::retain_count == 0);
63bb611c8fSApple OSS Distributions CHECK(to.get() == nullptr);
64bb611c8fSApple OSS Distributions }
65bb611c8fSApple OSS Distributions }
66bb611c8fSApple OSS Distributions
67*8d741a5dSApple OSS Distributions T_DECL(ctor_copy, "intrusive_shared_ptr.ctor.copy", T_META_TAG_VM_PREFERRED) {
68bb611c8fSApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived>();
69bb611c8fSApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived const>();
70bb611c8fSApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Derived const>();
71bb611c8fSApple OSS Distributions
72bb611c8fSApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>();
73bb611c8fSApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base const>();
74bb611c8fSApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>();
75bb611c8fSApple OSS Distributions
76bb611c8fSApple OSS Distributions tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base1>();
77bb611c8fSApple OSS Distributions tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base1 const>();
78bb611c8fSApple OSS Distributions
79bb611c8fSApple OSS Distributions tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base2>();
80bb611c8fSApple OSS Distributions tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base2 const>();
81bb611c8fSApple OSS Distributions
82bb611c8fSApple OSS Distributions // Make sure basic trait querying works
83bb611c8fSApple OSS Distributions static_assert(std::is_copy_constructible_v<test_shared_ptr<Derived> >);
84bb611c8fSApple OSS Distributions
85bb611c8fSApple OSS Distributions // Make sure downcasts are disabled
86bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<Derived>, /*from*/ test_shared_ptr<Base> const&>);
87bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<DerivedMultiple>, /*from*/ test_shared_ptr<Base1> const&>);
88bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<DerivedMultiple>, /*from*/ test_shared_ptr<Base2> const&>);
89bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<Base2>, /*from*/ test_shared_ptr<Base1> const&>);
90bb611c8fSApple OSS Distributions
91bb611c8fSApple OSS Distributions // Make sure const-casting away doesn't work
92bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<Derived>, /*from*/ test_shared_ptr<Derived const> const&>);
93bb611c8fSApple OSS Distributions
94bb611c8fSApple OSS Distributions // Make sure casting to unrelated types doesn't work
95bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<char>, /*from*/ test_shared_ptr<Derived> const&>);
96bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<Unrelated>, /*from*/ test_shared_ptr<Derived> const&>);
97bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ test_shared_ptr<Base2>, /*from*/ test_shared_ptr<Base1> const&>);
98bb611c8fSApple OSS Distributions
99bb611c8fSApple OSS Distributions // Make sure constructing with different policies doesn't work
100bb611c8fSApple OSS Distributions static_assert(!std::is_constructible_v</*to*/ libkern::intrusive_shared_ptr<Derived, dummy_policy<2> >, /*from*/ libkern::intrusive_shared_ptr<Derived, dummy_policy<1> > const &>);
101bb611c8fSApple OSS Distributions }
102