]> git.saurik.com Git - apple/xnu.git/blobdiff - tests/safe_allocation_src/size.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / tests / safe_allocation_src / size.cpp
diff --git a/tests/safe_allocation_src/size.cpp b/tests/safe_allocation_src/size.cpp
new file mode 100644 (file)
index 0000000..24d731d
--- /dev/null
@@ -0,0 +1,48 @@
+//
+// Tests for
+//      size_t size() const;
+//
+
+#include <libkern/c++/safe_allocation.h>
+#include <darwintest.h>
+#include "test_utils.h"
+#include <cstddef>
+#include <type_traits>
+#include <utility>
+
+struct T {
+       int i;
+};
+
+template <typename T>
+static void
+tests()
+{
+       {
+               test_safe_allocation<T> const array(10, libkern::allocate_memory);
+               CHECK(array.size() == 10);
+       }
+       {
+               T* memory = reinterpret_cast<T*>(malloc_allocator::allocate(10 * sizeof(T)));
+               test_safe_allocation<T> const array(memory, 10, libkern::adopt_memory);
+               CHECK(array.size() == 10);
+       }
+       {
+               test_safe_allocation<T> const array(nullptr, 0, libkern::adopt_memory);
+               CHECK(array.size() == 0);
+       }
+       {
+               test_safe_allocation<T> const array;
+               CHECK(array.size() == 0);
+       }
+
+       {
+               using Size = decltype(std::declval<test_safe_allocation<T> const&>().size());
+               static_assert(std::is_same_v<Size, std::size_t>);
+       }
+}
+
+T_DECL(size, "safe_allocation.size") {
+       tests<T>();
+       tests<T const>();
+}