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