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