1*fdd8201dSApple OSS Distributions //
2*fdd8201dSApple OSS Distributions // Tests for
3*fdd8201dSApple OSS Distributions // void reset(pointer p, retain_t) noexcept;
4*fdd8201dSApple OSS Distributions //
5*fdd8201dSApple OSS Distributions
6*fdd8201dSApple OSS Distributions #include <libkern/c++/intrusive_shared_ptr.h>
7*fdd8201dSApple OSS Distributions #include <darwintest.h>
8*fdd8201dSApple OSS Distributions #include "test_policy.h"
9*fdd8201dSApple OSS Distributions
10*fdd8201dSApple OSS Distributions struct T {
11*fdd8201dSApple OSS Distributions int i;
12*fdd8201dSApple OSS Distributions };
13*fdd8201dSApple OSS Distributions
14*fdd8201dSApple OSS Distributions template <typename T>
15*fdd8201dSApple OSS Distributions static void
tests()16*fdd8201dSApple OSS Distributions tests()
17*fdd8201dSApple OSS Distributions {
18*fdd8201dSApple OSS Distributions T obj1{1};
19*fdd8201dSApple OSS Distributions T obj2{2};
20*fdd8201dSApple OSS Distributions
21*fdd8201dSApple OSS Distributions // reset() non-null shared pointer to non-null raw pointer
22*fdd8201dSApple OSS Distributions {
23*fdd8201dSApple OSS Distributions tracked_shared_ptr<T> ptr(&obj1, libkern::retain);
24*fdd8201dSApple OSS Distributions tracking_policy::reset();
25*fdd8201dSApple OSS Distributions ptr.reset(&obj2, libkern::retain);
26*fdd8201dSApple OSS Distributions CHECK(tracking_policy::releases == 1);
27*fdd8201dSApple OSS Distributions CHECK(tracking_policy::retains == 1);
28*fdd8201dSApple OSS Distributions CHECK(ptr.get() == &obj2);
29*fdd8201dSApple OSS Distributions }
30*fdd8201dSApple OSS Distributions
31*fdd8201dSApple OSS Distributions // reset() non-null shared pointer to null raw pointer
32*fdd8201dSApple OSS Distributions {
33*fdd8201dSApple OSS Distributions tracked_shared_ptr<T> ptr(&obj1, libkern::retain);
34*fdd8201dSApple OSS Distributions tracking_policy::reset();
35*fdd8201dSApple OSS Distributions ptr.reset(nullptr, libkern::retain);
36*fdd8201dSApple OSS Distributions CHECK(tracking_policy::releases == 1);
37*fdd8201dSApple OSS Distributions CHECK(tracking_policy::retains == 0);
38*fdd8201dSApple OSS Distributions CHECK(ptr.get() == nullptr);
39*fdd8201dSApple OSS Distributions }
40*fdd8201dSApple OSS Distributions
41*fdd8201dSApple OSS Distributions // reset() null shared pointer to non-null raw pointer
42*fdd8201dSApple OSS Distributions {
43*fdd8201dSApple OSS Distributions tracked_shared_ptr<T> ptr = nullptr;
44*fdd8201dSApple OSS Distributions tracking_policy::reset();
45*fdd8201dSApple OSS Distributions ptr.reset(&obj2, libkern::retain);
46*fdd8201dSApple OSS Distributions CHECK(tracking_policy::releases == 0);
47*fdd8201dSApple OSS Distributions CHECK(tracking_policy::retains == 1);
48*fdd8201dSApple OSS Distributions CHECK(ptr.get() == &obj2);
49*fdd8201dSApple OSS Distributions }
50*fdd8201dSApple OSS Distributions
51*fdd8201dSApple OSS Distributions // reset() null shared pointer to null raw pointer
52*fdd8201dSApple OSS Distributions {
53*fdd8201dSApple OSS Distributions tracked_shared_ptr<T> ptr = nullptr;
54*fdd8201dSApple OSS Distributions tracking_policy::reset();
55*fdd8201dSApple OSS Distributions ptr.reset(nullptr, libkern::retain);
56*fdd8201dSApple OSS Distributions CHECK(tracking_policy::releases == 0);
57*fdd8201dSApple OSS Distributions CHECK(tracking_policy::retains == 0);
58*fdd8201dSApple OSS Distributions CHECK(ptr.get() == nullptr);
59*fdd8201dSApple OSS Distributions }
60*fdd8201dSApple OSS Distributions
61*fdd8201dSApple OSS Distributions // self-reset() should not cause the refcount to hit 0, ever
62*fdd8201dSApple OSS Distributions {
63*fdd8201dSApple OSS Distributions tracking_policy::reset();
64*fdd8201dSApple OSS Distributions tracked_shared_ptr<T> ptr(&obj1, libkern::retain);
65*fdd8201dSApple OSS Distributions CHECK(tracking_policy::retains == 1);
66*fdd8201dSApple OSS Distributions ptr.reset(ptr.get(), libkern::retain);
67*fdd8201dSApple OSS Distributions CHECK(tracking_policy::retains == 2);
68*fdd8201dSApple OSS Distributions CHECK(tracking_policy::releases == 1);
69*fdd8201dSApple OSS Distributions CHECK(tracking_policy::refcount == 1);
70*fdd8201dSApple OSS Distributions CHECK(!tracking_policy::hit_zero);
71*fdd8201dSApple OSS Distributions CHECK(ptr.get() == &obj1);
72*fdd8201dSApple OSS Distributions }
73*fdd8201dSApple OSS Distributions
74*fdd8201dSApple OSS Distributions // reset() as a self-reference
75*fdd8201dSApple OSS Distributions {
76*fdd8201dSApple OSS Distributions tracked_shared_ptr<T> ptr;
77*fdd8201dSApple OSS Distributions tracked_shared_ptr<T> ptr2;
78*fdd8201dSApple OSS Distributions CHECK(ptr.reset(&obj2, libkern::retain));
79*fdd8201dSApple OSS Distributions
80*fdd8201dSApple OSS Distributions // check short-circuiting
81*fdd8201dSApple OSS Distributions bool ok = (ptr.reset() && ptr2.reset(&obj1, libkern::retain));
82*fdd8201dSApple OSS Distributions CHECK(ptr2.get() == nullptr);
83*fdd8201dSApple OSS Distributions }
84*fdd8201dSApple OSS Distributions }
85*fdd8201dSApple OSS Distributions
86*fdd8201dSApple OSS Distributions T_DECL(reset_retain, "intrusive_shared_ptr.reset.retain") {
87*fdd8201dSApple OSS Distributions tests<T>();
88*fdd8201dSApple OSS Distributions tests<T const>();
89*fdd8201dSApple OSS Distributions }
90