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