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