]> git.saurik.com Git - apple/xnu.git/blobdiff - tests/safe_allocation_src/dtor.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / tests / safe_allocation_src / dtor.cpp
diff --git a/tests/safe_allocation_src/dtor.cpp b/tests/safe_allocation_src/dtor.cpp
new file mode 100644 (file)
index 0000000..8bd575c
--- /dev/null
@@ -0,0 +1,50 @@
+//
+// Tests for
+//  ~safe_allocation();
+//
+
+#include <libkern/c++/safe_allocation.h>
+#include <darwintest.h>
+#include "test_utils.h"
+
+struct TriviallyDestructible {
+       int i;
+};
+
+struct NonTriviallyDestructible {
+       int i;
+       ~NonTriviallyDestructible()
+       {
+       }
+};
+
+template <typename T>
+static void
+tests()
+{
+       // Destroy a non-null allocation
+       {
+               {
+                       tracked_safe_allocation<T> array(10, libkern::allocate_memory);
+                       tracking_allocator::reset();
+               }
+               CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
+       }
+
+       // Destroy a null allocation
+       {
+               {
+                       tracked_safe_allocation<T> array = nullptr;
+                       tracking_allocator::reset();
+               }
+               CHECK(!tracking_allocator::did_deallocate);
+       }
+}
+
+T_DECL(dtor, "safe_allocation.dtor") {
+       tests<TriviallyDestructible>();
+       tests<TriviallyDestructible const>();
+
+       tests<NonTriviallyDestructible>();
+       tests<NonTriviallyDestructible const>();
+}