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