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