1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  void swap(intrusive_shared_ptr& a, intrusive_shared_ptr& b);
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 { int i; };
11bb611c8fSApple OSS Distributions 
12bb611c8fSApple OSS Distributions template <typename T>
13bb611c8fSApple OSS Distributions static void
tests()14bb611c8fSApple OSS Distributions tests()
15bb611c8fSApple OSS Distributions {
16bb611c8fSApple OSS Distributions 	T obj1{1};
17bb611c8fSApple OSS Distributions 	T obj2{2};
18bb611c8fSApple OSS Distributions 
19bb611c8fSApple OSS Distributions 	// Swap non-null with non-null
20bb611c8fSApple OSS Distributions 	{
21bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> a(&obj1, libkern::retain);
22bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> b(&obj2, libkern::retain);
23bb611c8fSApple OSS Distributions 		T* a_raw = a.get();
24bb611c8fSApple OSS Distributions 		T* b_raw = b.get();
25bb611c8fSApple OSS Distributions 		tracking_policy::reset();
26bb611c8fSApple OSS Distributions 
27bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
28bb611c8fSApple OSS Distributions 
29bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
30bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
31bb611c8fSApple OSS Distributions 		CHECK(a.get() == b_raw);
32bb611c8fSApple OSS Distributions 		CHECK(b.get() == a_raw);
33bb611c8fSApple OSS Distributions 	}
34bb611c8fSApple OSS Distributions 
35bb611c8fSApple OSS Distributions 	// Swap non-null with null
36bb611c8fSApple OSS Distributions 	{
37bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> a(&obj1, libkern::retain);
38bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> b = nullptr;
39bb611c8fSApple OSS Distributions 		T* a_raw = a.get();
40bb611c8fSApple OSS Distributions 		tracking_policy::reset();
41bb611c8fSApple OSS Distributions 
42bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
43bb611c8fSApple OSS Distributions 
44bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
45bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
46bb611c8fSApple OSS Distributions 		CHECK(a.get() == nullptr);
47bb611c8fSApple OSS Distributions 		CHECK(b.get() == a_raw);
48bb611c8fSApple OSS Distributions 	}
49bb611c8fSApple OSS Distributions 
50bb611c8fSApple OSS Distributions 	// Swap null with non-null
51bb611c8fSApple OSS Distributions 	{
52bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> a = nullptr;
53bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> b(&obj2, libkern::retain);
54bb611c8fSApple OSS Distributions 		T* b_raw = b.get();
55bb611c8fSApple OSS Distributions 		tracking_policy::reset();
56bb611c8fSApple OSS Distributions 
57bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
58bb611c8fSApple OSS Distributions 
59bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
60bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
61bb611c8fSApple OSS Distributions 		CHECK(a.get() == b_raw);
62bb611c8fSApple OSS Distributions 		CHECK(b.get() == nullptr);
63bb611c8fSApple OSS Distributions 	}
64bb611c8fSApple OSS Distributions 
65bb611c8fSApple OSS Distributions 	// Swap null with null
66bb611c8fSApple OSS Distributions 	{
67bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> a = nullptr;
68bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> b = nullptr;
69bb611c8fSApple OSS Distributions 		tracking_policy::reset();
70bb611c8fSApple OSS Distributions 
71bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
72bb611c8fSApple OSS Distributions 
73bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
74bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
75bb611c8fSApple OSS Distributions 		CHECK(a.get() == nullptr);
76bb611c8fSApple OSS Distributions 		CHECK(b.get() == nullptr);
77bb611c8fSApple OSS Distributions 	}
78bb611c8fSApple OSS Distributions 
79bb611c8fSApple OSS Distributions 	// Swap with self
80bb611c8fSApple OSS Distributions 	{
81bb611c8fSApple OSS Distributions 		tracked_shared_ptr<T> a(&obj1, libkern::retain);
82bb611c8fSApple OSS Distributions 		T* a_raw = a.get();
83bb611c8fSApple OSS Distributions 		tracking_policy::reset();
84bb611c8fSApple OSS Distributions 
85bb611c8fSApple OSS Distributions 		swap(a, a); // ADL call
86bb611c8fSApple OSS Distributions 
87bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
88bb611c8fSApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
89bb611c8fSApple OSS Distributions 		CHECK(a.get() == a_raw);
90bb611c8fSApple OSS Distributions 	}
91bb611c8fSApple OSS Distributions }
92bb611c8fSApple OSS Distributions 
93*8d741a5dSApple OSS Distributions T_DECL(swap, "intrusive_shared_ptr.swap", T_META_TAG_VM_PREFERRED) {
94bb611c8fSApple OSS Distributions 	tests<T>();
95bb611c8fSApple OSS Distributions 	tests<T const>();
96bb611c8fSApple OSS Distributions }
97