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