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