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