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