]>
git.saurik.com Git - apple/xnu.git/blob - tests/safe_allocation_src/swap.cpp
3 // void swap(safe_allocation& a, safe_allocation& b);
6 #include <libkern/c++/safe_allocation.h>
7 #include <darwintest.h>
8 #include "test_utils.h"
18 // Swap non-null with non-null
20 tracked_safe_allocation
<T
> a(10, libkern::allocate_memory
);
21 tracked_safe_allocation
<T
> b(20, libkern::allocate_memory
);
24 tracking_allocator::reset();
26 swap(a
, b
); // ADL call
28 CHECK(!tracking_allocator::did_allocate
);
29 CHECK(!tracking_allocator::did_deallocate
);
30 CHECK(a
.data() == b_mem
);
31 CHECK(b
.data() == a_mem
);
32 CHECK(a
.size() == 20);
33 CHECK(b
.size() == 10);
36 // Swap non-null with null
38 tracked_safe_allocation
<T
> a(10, libkern::allocate_memory
);
39 tracked_safe_allocation
<T
> b
= nullptr;
41 tracking_allocator::reset();
43 swap(a
, b
); // ADL call
45 CHECK(!tracking_allocator::did_allocate
);
46 CHECK(!tracking_allocator::did_deallocate
);
47 CHECK(a
.data() == nullptr);
48 CHECK(b
.data() == a_mem
);
50 CHECK(b
.size() == 10);
53 // Swap null with non-null
55 tracked_safe_allocation
<T
> a
= nullptr;
56 tracked_safe_allocation
<T
> b(20, libkern::allocate_memory
);
58 tracking_allocator::reset();
60 swap(a
, b
); // ADL call
62 CHECK(!tracking_allocator::did_allocate
);
63 CHECK(!tracking_allocator::did_deallocate
);
64 CHECK(a
.data() == b_mem
);
65 CHECK(b
.data() == nullptr);
66 CHECK(a
.size() == 20);
70 // Swap null with null
72 tracked_safe_allocation
<T
> a
= nullptr;
73 tracked_safe_allocation
<T
> b
= nullptr;
74 tracking_allocator::reset();
76 swap(a
, b
); // ADL call
78 CHECK(!tracking_allocator::did_allocate
);
79 CHECK(!tracking_allocator::did_deallocate
);
80 CHECK(a
.data() == nullptr);
81 CHECK(b
.data() == nullptr);
88 tracked_safe_allocation
<T
> a(10, libkern::allocate_memory
);
90 tracking_allocator::reset();
92 swap(a
, a
); // ADL call
94 CHECK(!tracking_allocator::did_allocate
);
95 CHECK(!tracking_allocator::did_deallocate
);
96 CHECK(a
.data() == a_mem
);
97 CHECK(a
.size() == 10);
101 T_DECL(swap
, "safe_allocation.swap") {