xref: /xnu-11417.121.6/tests/safe_allocation_src/ctor.move.cpp (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650) !
1*a1e26a70SApple OSS Distributions //
2*a1e26a70SApple OSS Distributions // Tests for
3*a1e26a70SApple OSS Distributions //  safe_allocation(safe_allocation&& other);
4*a1e26a70SApple OSS Distributions //
5*a1e26a70SApple OSS Distributions 
6*a1e26a70SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7*a1e26a70SApple OSS Distributions #include <darwintest.h>
8*a1e26a70SApple OSS Distributions #include "test_utils.h"
9*a1e26a70SApple OSS Distributions #include <utility>
10*a1e26a70SApple OSS Distributions 
11*a1e26a70SApple OSS Distributions struct T {
12*a1e26a70SApple OSS Distributions 	int i;
13*a1e26a70SApple OSS Distributions };
14*a1e26a70SApple OSS Distributions 
15*a1e26a70SApple OSS Distributions template <typename T>
16*a1e26a70SApple OSS Distributions static void
tests()17*a1e26a70SApple OSS Distributions tests()
18*a1e26a70SApple OSS Distributions {
19*a1e26a70SApple OSS Distributions 	// Move-construct from a non-null allocation (with different syntaxes)
20*a1e26a70SApple OSS Distributions 	{
21*a1e26a70SApple OSS Distributions 		{
22*a1e26a70SApple OSS Distributions 			tracked_safe_allocation<T> from(10, libkern::allocate_memory);
23*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
24*a1e26a70SApple OSS Distributions 
25*a1e26a70SApple OSS Distributions 			T* memory = from.data();
26*a1e26a70SApple OSS Distributions 
27*a1e26a70SApple OSS Distributions 			{
28*a1e26a70SApple OSS Distributions 				tracked_safe_allocation<T> to(std::move(from));
29*a1e26a70SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
30*a1e26a70SApple OSS Distributions 				CHECK(to.data() == memory);
31*a1e26a70SApple OSS Distributions 				CHECK(to.size() == 10);
32*a1e26a70SApple OSS Distributions 				CHECK(from.data() == nullptr);
33*a1e26a70SApple OSS Distributions 				CHECK(from.size() == 0);
34*a1e26a70SApple OSS Distributions 			}
35*a1e26a70SApple OSS Distributions 			CHECK(tracking_allocator::did_deallocate);
36*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
37*a1e26a70SApple OSS Distributions 		}
38*a1e26a70SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
39*a1e26a70SApple OSS Distributions 	}
40*a1e26a70SApple OSS Distributions 	{
41*a1e26a70SApple OSS Distributions 		{
42*a1e26a70SApple OSS Distributions 			tracked_safe_allocation<T> from(10, libkern::allocate_memory);
43*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
44*a1e26a70SApple OSS Distributions 
45*a1e26a70SApple OSS Distributions 			T* memory = from.data();
46*a1e26a70SApple OSS Distributions 
47*a1e26a70SApple OSS Distributions 			{
48*a1e26a70SApple OSS Distributions 				tracked_safe_allocation<T> to{std::move(from)};
49*a1e26a70SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
50*a1e26a70SApple OSS Distributions 				CHECK(to.data() == memory);
51*a1e26a70SApple OSS Distributions 				CHECK(to.size() == 10);
52*a1e26a70SApple OSS Distributions 				CHECK(from.data() == nullptr);
53*a1e26a70SApple OSS Distributions 				CHECK(from.size() == 0);
54*a1e26a70SApple OSS Distributions 			}
55*a1e26a70SApple OSS Distributions 			CHECK(tracking_allocator::did_deallocate);
56*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
57*a1e26a70SApple OSS Distributions 		}
58*a1e26a70SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
59*a1e26a70SApple OSS Distributions 	}
60*a1e26a70SApple OSS Distributions 	{
61*a1e26a70SApple OSS Distributions 		{
62*a1e26a70SApple OSS Distributions 			tracked_safe_allocation<T> from(10, libkern::allocate_memory);
63*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
64*a1e26a70SApple OSS Distributions 
65*a1e26a70SApple OSS Distributions 			T* memory = from.data();
66*a1e26a70SApple OSS Distributions 
67*a1e26a70SApple OSS Distributions 			{
68*a1e26a70SApple OSS Distributions 				tracked_safe_allocation<T> to = std::move(from);
69*a1e26a70SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
70*a1e26a70SApple OSS Distributions 				CHECK(to.data() == memory);
71*a1e26a70SApple OSS Distributions 				CHECK(to.size() == 10);
72*a1e26a70SApple OSS Distributions 				CHECK(from.data() == nullptr);
73*a1e26a70SApple OSS Distributions 				CHECK(from.size() == 0);
74*a1e26a70SApple OSS Distributions 			}
75*a1e26a70SApple OSS Distributions 			CHECK(tracking_allocator::did_deallocate);
76*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
77*a1e26a70SApple OSS Distributions 		}
78*a1e26a70SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
79*a1e26a70SApple OSS Distributions 	}
80*a1e26a70SApple OSS Distributions 
81*a1e26a70SApple OSS Distributions 	// Move-construct from a null allocation
82*a1e26a70SApple OSS Distributions 	{
83*a1e26a70SApple OSS Distributions 		{
84*a1e26a70SApple OSS Distributions 			tracked_safe_allocation<T> from = nullptr;
85*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
86*a1e26a70SApple OSS Distributions 
87*a1e26a70SApple OSS Distributions 			{
88*a1e26a70SApple OSS Distributions 				tracked_safe_allocation<T> to(std::move(from));
89*a1e26a70SApple OSS Distributions 				CHECK(!tracking_allocator::did_allocate);
90*a1e26a70SApple OSS Distributions 				CHECK(to.data() == nullptr);
91*a1e26a70SApple OSS Distributions 				CHECK(to.size() == 0);
92*a1e26a70SApple OSS Distributions 				CHECK(from.data() == nullptr);
93*a1e26a70SApple OSS Distributions 				CHECK(from.size() == 0);
94*a1e26a70SApple OSS Distributions 			}
95*a1e26a70SApple OSS Distributions 			CHECK(!tracking_allocator::did_deallocate);
96*a1e26a70SApple OSS Distributions 			tracking_allocator::reset();
97*a1e26a70SApple OSS Distributions 		}
98*a1e26a70SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
99*a1e26a70SApple OSS Distributions 	}
100*a1e26a70SApple OSS Distributions }
101*a1e26a70SApple OSS Distributions 
102*a1e26a70SApple OSS Distributions T_DECL(ctor_move, "safe_allocation.ctor.move", T_META_TAG_VM_PREFERRED) {
103*a1e26a70SApple OSS Distributions 	tests<T>();
104*a1e26a70SApple OSS Distributions 	tests<T const>();
105*a1e26a70SApple OSS Distributions }
106