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