1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  ~safe_allocation();
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 TriviallyDestructible {
11bb611c8fSApple OSS Distributions 	int i;
12bb611c8fSApple OSS Distributions };
13bb611c8fSApple OSS Distributions 
14bb611c8fSApple OSS Distributions struct NonTriviallyDestructible {
15bb611c8fSApple OSS Distributions 	int i;
~NonTriviallyDestructibleNonTriviallyDestructible16bb611c8fSApple OSS Distributions 	~NonTriviallyDestructible()
17bb611c8fSApple OSS Distributions 	{
18bb611c8fSApple OSS Distributions 	}
19bb611c8fSApple OSS Distributions };
20bb611c8fSApple OSS Distributions 
21bb611c8fSApple OSS Distributions template <typename T>
22bb611c8fSApple OSS Distributions static void
tests()23bb611c8fSApple OSS Distributions tests()
24bb611c8fSApple OSS Distributions {
25bb611c8fSApple OSS Distributions 	// Destroy a non-null allocation
26bb611c8fSApple OSS Distributions 	{
27bb611c8fSApple OSS Distributions 		{
28bb611c8fSApple OSS Distributions 			tracked_safe_allocation<T> array(10, libkern::allocate_memory);
29bb611c8fSApple OSS Distributions 			tracking_allocator::reset();
30bb611c8fSApple OSS Distributions 		}
31bb611c8fSApple OSS Distributions 		CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
32bb611c8fSApple OSS Distributions 	}
33bb611c8fSApple OSS Distributions 
34bb611c8fSApple OSS Distributions 	// Destroy a null allocation
35bb611c8fSApple OSS Distributions 	{
36bb611c8fSApple OSS Distributions 		{
37bb611c8fSApple OSS Distributions 			tracked_safe_allocation<T> array = nullptr;
38bb611c8fSApple OSS Distributions 			tracking_allocator::reset();
39bb611c8fSApple OSS Distributions 		}
40bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
41bb611c8fSApple OSS Distributions 	}
42bb611c8fSApple OSS Distributions }
43bb611c8fSApple OSS Distributions 
44*8d741a5dSApple OSS Distributions T_DECL(dtor, "safe_allocation.dtor", T_META_TAG_VM_PREFERRED) {
45bb611c8fSApple OSS Distributions 	tests<TriviallyDestructible>();
46bb611c8fSApple OSS Distributions 	tests<TriviallyDestructible const>();
47bb611c8fSApple OSS Distributions 
48bb611c8fSApple OSS Distributions 	tests<NonTriviallyDestructible>();
49bb611c8fSApple OSS Distributions 	tests<NonTriviallyDestructible const>();
50bb611c8fSApple OSS Distributions }
51