]> git.saurik.com Git - apple/xnu.git/blob - tests/safe_allocation_src/assign.nullptr.cpp
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / safe_allocation_src / assign.nullptr.cpp
1 //
2 // Tests for
3 // safe_allocation& operator=(std::nullptr_t);
4 //
5
6 #include <libkern/c++/safe_allocation.h>
7 #include <darwintest.h>
8 #include "test_utils.h"
9
10 struct T {
11 int i;
12 };
13
14 template <typename T>
15 static void
16 tests()
17 {
18 // Assign to a non-null allocation
19 {
20 tracked_safe_allocation<T> array(10, libkern::allocate_memory);
21 tracking_allocator::reset();
22
23 tracked_safe_allocation<T>& ref = (array = nullptr);
24 CHECK(&ref == &array);
25 CHECK(array.data() == nullptr);
26 CHECK(array.size() == 0);
27 CHECK(tracking_allocator::did_deallocate);
28 }
29
30 // Assign to a null allocation
31 {
32 tracked_safe_allocation<T> array = nullptr;
33 tracking_allocator::reset();
34
35 tracked_safe_allocation<T>& ref = (array = nullptr);
36 CHECK(&ref == &array);
37 CHECK(array.data() == nullptr);
38 CHECK(array.size() == 0);
39 CHECK(!tracking_allocator::did_deallocate);
40 }
41 }
42
43 T_DECL(assign_nullptr, "safe_allocation.assign.nullptr") {
44 tests<T>();
45 tests<T const>();
46 }