xref: /xnu-12377.41.6/tests/safe_allocation_src/dtor.cpp (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions //
2*bbb1b6f9SApple OSS Distributions // Tests for
3*bbb1b6f9SApple OSS Distributions //  ~safe_allocation();
4*bbb1b6f9SApple OSS Distributions //
5*bbb1b6f9SApple OSS Distributions 
6*bbb1b6f9SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
8*bbb1b6f9SApple OSS Distributions #include "test_utils.h"
9*bbb1b6f9SApple OSS Distributions 
10*bbb1b6f9SApple OSS Distributions struct TriviallyDestructible {
11*bbb1b6f9SApple OSS Distributions 	int i;
12*bbb1b6f9SApple OSS Distributions };
13*bbb1b6f9SApple OSS Distributions 
14*bbb1b6f9SApple OSS Distributions struct NonTriviallyDestructible {
15*bbb1b6f9SApple OSS Distributions 	int i;
~NonTriviallyDestructibleNonTriviallyDestructible16*bbb1b6f9SApple OSS Distributions 	~NonTriviallyDestructible()
17*bbb1b6f9SApple OSS Distributions 	{
18*bbb1b6f9SApple OSS Distributions 	}
19*bbb1b6f9SApple OSS Distributions };
20*bbb1b6f9SApple OSS Distributions 
21*bbb1b6f9SApple OSS Distributions template <typename T>
22*bbb1b6f9SApple OSS Distributions static void
tests()23*bbb1b6f9SApple OSS Distributions tests()
24*bbb1b6f9SApple OSS Distributions {
25*bbb1b6f9SApple OSS Distributions 	// Destroy a non-null allocation
26*bbb1b6f9SApple OSS Distributions 	{
27*bbb1b6f9SApple OSS Distributions 		{
28*bbb1b6f9SApple OSS Distributions 			tracked_safe_allocation<T> array(10, libkern::allocate_memory);
29*bbb1b6f9SApple OSS Distributions 			tracking_allocator::reset();
30*bbb1b6f9SApple OSS Distributions 		}
31*bbb1b6f9SApple OSS Distributions 		CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
32*bbb1b6f9SApple OSS Distributions 	}
33*bbb1b6f9SApple OSS Distributions 
34*bbb1b6f9SApple OSS Distributions 	// Destroy a null allocation
35*bbb1b6f9SApple OSS Distributions 	{
36*bbb1b6f9SApple OSS Distributions 		{
37*bbb1b6f9SApple OSS Distributions 			tracked_safe_allocation<T> array = nullptr;
38*bbb1b6f9SApple OSS Distributions 			tracking_allocator::reset();
39*bbb1b6f9SApple OSS Distributions 		}
40*bbb1b6f9SApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
41*bbb1b6f9SApple OSS Distributions 	}
42*bbb1b6f9SApple OSS Distributions }
43*bbb1b6f9SApple OSS Distributions 
44*bbb1b6f9SApple OSS Distributions T_DECL(dtor, "safe_allocation.dtor", T_META_TAG_VM_PREFERRED) {
45*bbb1b6f9SApple OSS Distributions 	tests<TriviallyDestructible>();
46*bbb1b6f9SApple OSS Distributions 	tests<TriviallyDestructible const>();
47*bbb1b6f9SApple OSS Distributions 
48*bbb1b6f9SApple OSS Distributions 	tests<NonTriviallyDestructible>();
49*bbb1b6f9SApple OSS Distributions 	tests<NonTriviallyDestructible const>();
50*bbb1b6f9SApple OSS Distributions }
51