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