xref: /xnu-11417.140.69/tests/safe_allocation_src/assign.move.cpp (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions //
2*43a90889SApple OSS Distributions // Tests for
3*43a90889SApple OSS Distributions //      safe_allocation& operator=(safe_allocation&& other);
4*43a90889SApple OSS Distributions //
5*43a90889SApple OSS Distributions 
6*43a90889SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7*43a90889SApple OSS Distributions #include <darwintest.h>
8*43a90889SApple OSS Distributions #include "test_utils.h"
9*43a90889SApple OSS Distributions #include <utility>
10*43a90889SApple OSS Distributions 
11*43a90889SApple OSS Distributions struct T {
12*43a90889SApple OSS Distributions 	int i;
13*43a90889SApple OSS Distributions };
14*43a90889SApple OSS Distributions 
15*43a90889SApple OSS Distributions template <typename T>
16*43a90889SApple OSS Distributions static void
tests()17*43a90889SApple OSS Distributions tests()
18*43a90889SApple OSS Distributions {
19*43a90889SApple OSS Distributions 	// Move-assign non-null to non-null
20*43a90889SApple OSS Distributions 	{
21*43a90889SApple OSS Distributions 		{
22*43a90889SApple OSS Distributions 			tracked_safe_allocation<T> from(10, libkern::allocate_memory);
23*43a90889SApple OSS Distributions 			T* memory = from.data();
24*43a90889SApple OSS Distributions 			{
25*43a90889SApple OSS Distributions 				tracked_safe_allocation<T> to(20, libkern::allocate_memory);
26*43a90889SApple OSS Distributions 				tracking_allocator::reset();
27*43a90889SApple OSS Distributions 
28*43a90889SApple OSS Distributions 				tracked_safe_allocation<T>& ref = (to = std::move(from));
29*43a90889SApple OSS Distributions 				CHECK(&ref == &to);
30*43a90889SApple OSS Distributions 				CHECK(to.data() == memory);
31*43a90889SApple OSS Distributions 				CHECK(to.size() == 10);
32*43a90889SApple OSS Distributions 				CHECK(from.data() == nullptr);
33*43a90889SApple OSS Distributions 				CHECK(from.size() == 0);
34*43a90889SApple OSS Distributions 
35*43a90889SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
36*43a90889SApple OSS Distributions 				CHECK(tracking_allocator::deallocated_size == 20 * sizeof(T));
37*43a90889SApple OSS Distributions 				tracking_allocator::reset();
38*43a90889SApple OSS Distributions 			}
39*43a90889SApple OSS Distributions 			CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
40*43a90889SApple OSS Distributions 			tracking_allocator::reset();
41*43a90889SApple OSS Distributions 		}
42*43a90889SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
43*43a90889SApple OSS Distributions 	}
44*43a90889SApple OSS Distributions 
45*43a90889SApple OSS Distributions 	// Move-assign null to non-null
46*43a90889SApple OSS Distributions 	{
47*43a90889SApple OSS Distributions 		{
48*43a90889SApple OSS Distributions 			tracked_safe_allocation<T> from = nullptr;
49*43a90889SApple OSS Distributions 			{
50*43a90889SApple OSS Distributions 				tracked_safe_allocation<T> to(20, libkern::allocate_memory);
51*43a90889SApple OSS Distributions 				tracking_allocator::reset();
52*43a90889SApple OSS Distributions 
53*43a90889SApple OSS Distributions 				tracked_safe_allocation<T>& ref = (to = std::move(from));
54*43a90889SApple OSS Distributions 				CHECK(&ref == &to);
55*43a90889SApple OSS Distributions 				CHECK(to.data() == nullptr);
56*43a90889SApple OSS Distributions 				CHECK(to.size() == 0);
57*43a90889SApple OSS Distributions 				CHECK(from.data() == nullptr);
58*43a90889SApple OSS Distributions 				CHECK(from.size() == 0);
59*43a90889SApple OSS Distributions 
60*43a90889SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
61*43a90889SApple OSS Distributions 				CHECK(tracking_allocator::deallocated_size == 20 * sizeof(T));
62*43a90889SApple OSS Distributions 				tracking_allocator::reset();
63*43a90889SApple OSS Distributions 			}
64*43a90889SApple OSS Distributions 			CHECK(!tracking_allocator::did_deallocate);
65*43a90889SApple OSS Distributions 			tracking_allocator::reset();
66*43a90889SApple OSS Distributions 		}
67*43a90889SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
68*43a90889SApple OSS Distributions 	}
69*43a90889SApple OSS Distributions 
70*43a90889SApple OSS Distributions 	// Move-assign non-null to null
71*43a90889SApple OSS Distributions 	{
72*43a90889SApple OSS Distributions 		{
73*43a90889SApple OSS Distributions 			tracked_safe_allocation<T> from(10, libkern::allocate_memory);
74*43a90889SApple OSS Distributions 			T* memory = from.data();
75*43a90889SApple OSS Distributions 			{
76*43a90889SApple OSS Distributions 				tracked_safe_allocation<T> to = nullptr;
77*43a90889SApple OSS Distributions 				tracking_allocator::reset();
78*43a90889SApple OSS Distributions 
79*43a90889SApple OSS Distributions 				tracked_safe_allocation<T>& ref = (to = std::move(from));
80*43a90889SApple OSS Distributions 				CHECK(&ref == &to);
81*43a90889SApple OSS Distributions 				CHECK(to.data() == memory);
82*43a90889SApple OSS Distributions 				CHECK(to.size() == 10);
83*43a90889SApple OSS Distributions 				CHECK(from.data() == nullptr);
84*43a90889SApple OSS Distributions 				CHECK(from.size() == 0);
85*43a90889SApple OSS Distributions 
86*43a90889SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
87*43a90889SApple OSS Distributions 				CHECK(!tracking_allocator::did_deallocate);
88*43a90889SApple OSS Distributions 				tracking_allocator::reset();
89*43a90889SApple OSS Distributions 			}
90*43a90889SApple OSS Distributions 			CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
91*43a90889SApple OSS Distributions 			tracking_allocator::reset();
92*43a90889SApple OSS Distributions 		}
93*43a90889SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
94*43a90889SApple OSS Distributions 	}
95*43a90889SApple OSS Distributions 
96*43a90889SApple OSS Distributions 	// Move-assign null to null
97*43a90889SApple OSS Distributions 	{
98*43a90889SApple OSS Distributions 		{
99*43a90889SApple OSS Distributions 			tracked_safe_allocation<T> from = nullptr;
100*43a90889SApple OSS Distributions 			{
101*43a90889SApple OSS Distributions 				tracked_safe_allocation<T> to = nullptr;
102*43a90889SApple OSS Distributions 				tracking_allocator::reset();
103*43a90889SApple OSS Distributions 
104*43a90889SApple OSS Distributions 				tracked_safe_allocation<T>& ref = (to = std::move(from));
105*43a90889SApple OSS Distributions 				CHECK(&ref == &to);
106*43a90889SApple OSS Distributions 				CHECK(to.data() == nullptr);
107*43a90889SApple OSS Distributions 				CHECK(to.size() == 0);
108*43a90889SApple OSS Distributions 				CHECK(from.data() == nullptr);
109*43a90889SApple OSS Distributions 				CHECK(from.size() == 0);
110*43a90889SApple OSS Distributions 
111*43a90889SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
112*43a90889SApple OSS Distributions 				CHECK(!tracking_allocator::did_deallocate);
113*43a90889SApple OSS Distributions 				tracking_allocator::reset();
114*43a90889SApple OSS Distributions 			}
115*43a90889SApple OSS Distributions 			CHECK(!tracking_allocator::did_deallocate);
116*43a90889SApple OSS Distributions 			tracking_allocator::reset();
117*43a90889SApple OSS Distributions 		}
118*43a90889SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
119*43a90889SApple OSS Distributions 	}
120*43a90889SApple OSS Distributions 
121*43a90889SApple OSS Distributions 	// Move-assign to self
122*43a90889SApple OSS Distributions 	{
123*43a90889SApple OSS Distributions 		{
124*43a90889SApple OSS Distributions 			tracked_safe_allocation<T> obj(10, libkern::allocate_memory);
125*43a90889SApple OSS Distributions 			T* memory = obj.data();
126*43a90889SApple OSS Distributions 
127*43a90889SApple OSS Distributions 			tracking_allocator::reset();
128*43a90889SApple OSS Distributions 			tracked_safe_allocation<T>& ref = (obj = std::move(obj));
129*43a90889SApple OSS Distributions 			CHECK(&ref == &obj);
130*43a90889SApple OSS Distributions 			CHECK(obj.data() == memory);
131*43a90889SApple OSS Distributions 			CHECK(obj.size() == 10);
132*43a90889SApple OSS Distributions 			CHECK(!tracking_allocator::did_allocate);
133*43a90889SApple OSS Distributions 			CHECK(!tracking_allocator::did_deallocate);
134*43a90889SApple OSS Distributions 			tracking_allocator::reset();
135*43a90889SApple OSS Distributions 		}
136*43a90889SApple OSS Distributions 		CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
137*43a90889SApple OSS Distributions 	}
138*43a90889SApple OSS Distributions }
139*43a90889SApple OSS Distributions 
140*43a90889SApple OSS Distributions T_DECL(assign_move, "safe_allocation.assign.move", T_META_TAG_VM_PREFERRED) {
141*43a90889SApple OSS Distributions 	tests<T>();
142*43a90889SApple OSS Distributions 	tests<T const>();
143*43a90889SApple OSS Distributions }
144