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