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
tests()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", T_META_TAG_VM_PREFERRED) {
44 	tests<T>();
45 	tests<T const>();
46 }
47