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