1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  void swap(safe_allocation& a, safe_allocation& b);
4bb611c8fSApple OSS Distributions //
5bb611c8fSApple OSS Distributions 
6bb611c8fSApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7bb611c8fSApple OSS Distributions #include <darwintest.h>
8bb611c8fSApple OSS Distributions #include "test_utils.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 	// Swap non-null with non-null
19bb611c8fSApple OSS Distributions 	{
20bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> a(10, libkern::allocate_memory);
21bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> b(20, libkern::allocate_memory);
22bb611c8fSApple OSS Distributions 		T* a_mem = a.data();
23bb611c8fSApple OSS Distributions 		T* b_mem = b.data();
24bb611c8fSApple OSS Distributions 		tracking_allocator::reset();
25bb611c8fSApple OSS Distributions 
26bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
27bb611c8fSApple OSS Distributions 
28bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_allocate);
29bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
30bb611c8fSApple OSS Distributions 		CHECK(a.data() == b_mem);
31bb611c8fSApple OSS Distributions 		CHECK(b.data() == a_mem);
32bb611c8fSApple OSS Distributions 		CHECK(a.size() == 20);
33bb611c8fSApple OSS Distributions 		CHECK(b.size() == 10);
34bb611c8fSApple OSS Distributions 	}
35bb611c8fSApple OSS Distributions 
36bb611c8fSApple OSS Distributions 	// Swap non-null with null
37bb611c8fSApple OSS Distributions 	{
38bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> a(10, libkern::allocate_memory);
39bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> b = nullptr;
40bb611c8fSApple OSS Distributions 		T* a_mem = a.data();
41bb611c8fSApple OSS Distributions 		tracking_allocator::reset();
42bb611c8fSApple OSS Distributions 
43bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
44bb611c8fSApple OSS Distributions 
45bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_allocate);
46bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
47bb611c8fSApple OSS Distributions 		CHECK(a.data() == nullptr);
48bb611c8fSApple OSS Distributions 		CHECK(b.data() == a_mem);
49bb611c8fSApple OSS Distributions 		CHECK(a.size() == 0);
50bb611c8fSApple OSS Distributions 		CHECK(b.size() == 10);
51bb611c8fSApple OSS Distributions 	}
52bb611c8fSApple OSS Distributions 
53bb611c8fSApple OSS Distributions 	// Swap null with non-null
54bb611c8fSApple OSS Distributions 	{
55bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> a = nullptr;
56bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> b(20, libkern::allocate_memory);
57bb611c8fSApple OSS Distributions 		T* b_mem = b.data();
58bb611c8fSApple OSS Distributions 		tracking_allocator::reset();
59bb611c8fSApple OSS Distributions 
60bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
61bb611c8fSApple OSS Distributions 
62bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_allocate);
63bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
64bb611c8fSApple OSS Distributions 		CHECK(a.data() == b_mem);
65bb611c8fSApple OSS Distributions 		CHECK(b.data() == nullptr);
66bb611c8fSApple OSS Distributions 		CHECK(a.size() == 20);
67bb611c8fSApple OSS Distributions 		CHECK(b.size() == 0);
68bb611c8fSApple OSS Distributions 	}
69bb611c8fSApple OSS Distributions 
70bb611c8fSApple OSS Distributions 	// Swap null with null
71bb611c8fSApple OSS Distributions 	{
72bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> a = nullptr;
73bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> b = nullptr;
74bb611c8fSApple OSS Distributions 		tracking_allocator::reset();
75bb611c8fSApple OSS Distributions 
76bb611c8fSApple OSS Distributions 		swap(a, b); // ADL call
77bb611c8fSApple OSS Distributions 
78bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_allocate);
79bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
80bb611c8fSApple OSS Distributions 		CHECK(a.data() == nullptr);
81bb611c8fSApple OSS Distributions 		CHECK(b.data() == nullptr);
82bb611c8fSApple OSS Distributions 		CHECK(a.size() == 0);
83bb611c8fSApple OSS Distributions 		CHECK(b.size() == 0);
84bb611c8fSApple OSS Distributions 	}
85bb611c8fSApple OSS Distributions 
86bb611c8fSApple OSS Distributions 	// Swap with self
87bb611c8fSApple OSS Distributions 	{
88bb611c8fSApple OSS Distributions 		tracked_safe_allocation<T> a(10, libkern::allocate_memory);
89bb611c8fSApple OSS Distributions 		T* a_mem = a.data();
90bb611c8fSApple OSS Distributions 		tracking_allocator::reset();
91bb611c8fSApple OSS Distributions 
92bb611c8fSApple OSS Distributions 		swap(a, a); // ADL call
93bb611c8fSApple OSS Distributions 
94bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_allocate);
95bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
96bb611c8fSApple OSS Distributions 		CHECK(a.data() == a_mem);
97bb611c8fSApple OSS Distributions 		CHECK(a.size() == 10);
98bb611c8fSApple OSS Distributions 	}
99bb611c8fSApple OSS Distributions }
100bb611c8fSApple OSS Distributions 
101*8d741a5dSApple OSS Distributions T_DECL(swap, "safe_allocation.swap", T_META_TAG_VM_PREFERRED) {
102bb611c8fSApple OSS Distributions 	tests<T>();
103bb611c8fSApple OSS Distributions 	tests<T const>();
104bb611c8fSApple OSS Distributions }
105