1 //
2 // Tests for
3 //  void reset() noexcept;
4 //
5 
6 #include <libkern/c++/intrusive_shared_ptr.h>
7 #include <darwintest.h>
8 #include "test_policy.h"
9 
10 struct T {
11 	int i;
12 };
13 
14 template <typename T>
15 static void
tests()16 tests()
17 {
18 	T obj{3};
19 
20 	// reset() on a non-null shared pointer
21 	{
22 		tracked_shared_ptr<T> ptr(&obj, libkern::retain);
23 		tracking_policy::reset();
24 		ptr.reset();
25 		CHECK(tracking_policy::releases == 1);
26 		CHECK(tracking_policy::retains == 0);
27 		CHECK(ptr.get() == nullptr);
28 	}
29 
30 	// reset() on a null shared pointer
31 	{
32 		tracked_shared_ptr<T> ptr = nullptr;
33 		tracking_policy::reset();
34 		ptr.reset();
35 		CHECK(tracking_policy::releases == 0);
36 		CHECK(tracking_policy::retains == 0);
37 		CHECK(ptr.get() == nullptr);
38 	}
39 
40 	// reset() as a self-reference
41 	{
42 		tracked_shared_ptr<T> ptr(&obj, libkern::retain);
43 		tracked_shared_ptr<T> ptr2(&obj, libkern::retain);
44 		CHECK(!ptr.reset());
45 
46 		CHECK(&ptr.reset() == &ptr);
47 
48 		// check short-circuiting
49 		bool ok =  (ptr.reset() && !ptr2.reset());
50 		CHECK(ptr2.get() != nullptr);
51 	}
52 }
53 
54 T_DECL(reset, "intrusive_shared_ptr.reset", T_META_TAG_VM_PREFERRED) {
55 	tests<T>();
56 	tests<T const>();
57 }
58