1*bb611c8fSApple OSS Distributions //
2*bb611c8fSApple OSS Distributions // Tests for
3*bb611c8fSApple OSS Distributions //  intrusive_shared_ptr& operator=(std::nullptr_t);
4*bb611c8fSApple OSS Distributions //
5*bb611c8fSApple OSS Distributions 
6*bb611c8fSApple OSS Distributions #include <libkern/c++/intrusive_shared_ptr.h>
7*bb611c8fSApple OSS Distributions #include <darwintest.h>
8*bb611c8fSApple OSS Distributions #include "test_policy.h"
9*bb611c8fSApple OSS Distributions 
10*bb611c8fSApple OSS Distributions struct T {
11*bb611c8fSApple OSS Distributions 	int i;
12*bb611c8fSApple OSS Distributions };
13*bb611c8fSApple OSS Distributions 
14*bb611c8fSApple OSS Distributions template <typename T>
15*bb611c8fSApple OSS Distributions static void
16*bb611c8fSApple OSS Distributions tests()
17*bb611c8fSApple OSS Distributions {
18*bb611c8fSApple OSS Distributions 	T obj{3};
19*bb611c8fSApple OSS Distributions 
20*bb611c8fSApple OSS Distributions 	// Assign nullptr to non-null
21*bb611c8fSApple OSS Distributions 	{
22*bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> ptr(&obj, libkern::retain);
23*bb611c8fSApple OSS Distributions 		tracking_policy::reset();
24*bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T>& ref = (ptr = nullptr);
25*bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::releases == 1);
26*bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
27*bb611c8fSApple OSS Distributions 		CHECK(&ref == &ptr);
28*bb611c8fSApple OSS Distributions 		CHECK(ptr.get() == nullptr);
29*bb611c8fSApple OSS Distributions 	}
30*bb611c8fSApple OSS Distributions 
31*bb611c8fSApple OSS Distributions 	// Assign nullptr to null
32*bb611c8fSApple OSS Distributions 	{
33*bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> ptr = nullptr;
34*bb611c8fSApple OSS Distributions 		tracking_policy::reset();
35*bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T>& ref = (ptr = nullptr);
36*bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
37*bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
38*bb611c8fSApple OSS Distributions 		CHECK(&ref == &ptr);
39*bb611c8fSApple OSS Distributions 		CHECK(ptr.get() == nullptr);
40*bb611c8fSApple OSS Distributions 	}
41*bb611c8fSApple OSS Distributions }
42*bb611c8fSApple OSS Distributions 
43*bb611c8fSApple OSS Distributions T_DECL(assign_nullptr, "intrusive_shared_ptr.assign.nullptr") {
44*bb611c8fSApple OSS Distributions 	tests<T>();
45*bb611c8fSApple OSS Distributions 	tests<T const>();
46*bb611c8fSApple OSS Distributions }
47