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