]>
git.saurik.com Git - apple/xnu.git/blob - tests/safe_allocation_src/assign.move.cpp
3 // safe_allocation& operator=(safe_allocation&& other);
6 #include <libkern/c++/safe_allocation.h>
7 #include <darwintest.h>
8 #include "test_utils.h"
19 // Move-assign non-null to non-null
22 tracked_safe_allocation
<T
> from(10, libkern::allocate_memory
);
23 T
* memory
= from
.data();
25 tracked_safe_allocation
<T
> to(20, libkern::allocate_memory
);
26 tracking_allocator::reset();
28 tracked_safe_allocation
<T
>& ref
= (to
= std::move(from
));
30 CHECK(to
.data() == memory
);
31 CHECK(to
.size() == 10);
32 CHECK(from
.data() == nullptr);
33 CHECK(from
.size() == 0);
35 CHECK(!tracking_allocator::did_allocate
);
36 CHECK(tracking_allocator::deallocated_size
== 20 * sizeof(T
));
37 tracking_allocator::reset();
39 CHECK(tracking_allocator::deallocated_size
== 10 * sizeof(T
));
40 tracking_allocator::reset();
42 CHECK(!tracking_allocator::did_deallocate
);
45 // Move-assign null to non-null
48 tracked_safe_allocation
<T
> from
= nullptr;
50 tracked_safe_allocation
<T
> to(20, libkern::allocate_memory
);
51 tracking_allocator::reset();
53 tracked_safe_allocation
<T
>& ref
= (to
= std::move(from
));
55 CHECK(to
.data() == nullptr);
56 CHECK(to
.size() == 0);
57 CHECK(from
.data() == nullptr);
58 CHECK(from
.size() == 0);
60 CHECK(!tracking_allocator::did_allocate
);
61 CHECK(tracking_allocator::deallocated_size
== 20 * sizeof(T
));
62 tracking_allocator::reset();
64 CHECK(!tracking_allocator::did_deallocate
);
65 tracking_allocator::reset();
67 CHECK(!tracking_allocator::did_deallocate
);
70 // Move-assign non-null to null
73 tracked_safe_allocation
<T
> from(10, libkern::allocate_memory
);
74 T
* memory
= from
.data();
76 tracked_safe_allocation
<T
> to
= nullptr;
77 tracking_allocator::reset();
79 tracked_safe_allocation
<T
>& ref
= (to
= std::move(from
));
81 CHECK(to
.data() == memory
);
82 CHECK(to
.size() == 10);
83 CHECK(from
.data() == nullptr);
84 CHECK(from
.size() == 0);
86 CHECK(!tracking_allocator::did_allocate
);
87 CHECK(!tracking_allocator::did_deallocate
);
88 tracking_allocator::reset();
90 CHECK(tracking_allocator::deallocated_size
== 10 * sizeof(T
));
91 tracking_allocator::reset();
93 CHECK(!tracking_allocator::did_deallocate
);
96 // Move-assign null to null
99 tracked_safe_allocation
<T
> from
= nullptr;
101 tracked_safe_allocation
<T
> to
= nullptr;
102 tracking_allocator::reset();
104 tracked_safe_allocation
<T
>& ref
= (to
= std::move(from
));
106 CHECK(to
.data() == nullptr);
107 CHECK(to
.size() == 0);
108 CHECK(from
.data() == nullptr);
109 CHECK(from
.size() == 0);
111 CHECK(!tracking_allocator::did_allocate
);
112 CHECK(!tracking_allocator::did_deallocate
);
113 tracking_allocator::reset();
115 CHECK(!tracking_allocator::did_deallocate
);
116 tracking_allocator::reset();
118 CHECK(!tracking_allocator::did_deallocate
);
121 // Move-assign to self
124 tracked_safe_allocation
<T
> obj(10, libkern::allocate_memory
);
125 T
* memory
= obj
.data();
127 tracking_allocator::reset();
128 tracked_safe_allocation
<T
>& ref
= (obj
= std::move(obj
));
130 CHECK(obj
.data() == memory
);
131 CHECK(obj
.size() == 10);
132 CHECK(!tracking_allocator::did_allocate
);
133 CHECK(!tracking_allocator::did_deallocate
);
134 tracking_allocator::reset();
136 CHECK(tracking_allocator::deallocated_size
== 10 * sizeof(T
));
140 T_DECL(assign_move
, "safe_allocation.assign.move") {