]> git.saurik.com Git - apple/xnu.git/blobdiff - tests/safe_allocation_src/assign.nullptr.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / tests / safe_allocation_src / assign.nullptr.cpp
diff --git a/tests/safe_allocation_src/assign.nullptr.cpp b/tests/safe_allocation_src/assign.nullptr.cpp
new file mode 100644 (file)
index 0000000..07a13a1
--- /dev/null
@@ -0,0 +1,46 @@
+//
+// Tests for
+//  safe_allocation& operator=(std::nullptr_t);
+//
+
+#include <libkern/c++/safe_allocation.h>
+#include <darwintest.h>
+#include "test_utils.h"
+
+struct T {
+       int i;
+};
+
+template <typename T>
+static void
+tests()
+{
+       // Assign to a non-null allocation
+       {
+               tracked_safe_allocation<T> array(10, libkern::allocate_memory);
+               tracking_allocator::reset();
+
+               tracked_safe_allocation<T>& ref = (array = nullptr);
+               CHECK(&ref == &array);
+               CHECK(array.data() == nullptr);
+               CHECK(array.size() == 0);
+               CHECK(tracking_allocator::did_deallocate);
+       }
+
+       // Assign to a null allocation
+       {
+               tracked_safe_allocation<T> array = nullptr;
+               tracking_allocator::reset();
+
+               tracked_safe_allocation<T>& ref = (array = nullptr);
+               CHECK(&ref == &array);
+               CHECK(array.data() == nullptr);
+               CHECK(array.size() == 0);
+               CHECK(!tracking_allocator::did_deallocate);
+       }
+}
+
+T_DECL(assign_nullptr, "safe_allocation.assign.nullptr") {
+       tests<T>();
+       tests<T const>();
+}