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