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