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