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