]> git.saurik.com Git - apple/xnu.git/blobdiff - tests/intrusive_shared_ptr_src/get.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / tests / intrusive_shared_ptr_src / get.cpp
diff --git a/tests/intrusive_shared_ptr_src/get.cpp b/tests/intrusive_shared_ptr_src/get.cpp
new file mode 100644 (file)
index 0000000..8b37b8e
--- /dev/null
@@ -0,0 +1,50 @@
+//
+// Tests for
+//  pointer get() const noexcept;
+//
+
+#include <libkern/c++/intrusive_shared_ptr.h>
+#include "test_policy.h"
+#include <darwintest.h>
+#include <utility>
+
+struct T {
+       int i;
+};
+
+template <typename T>
+static constexpr auto
+can_call_get_on_temporary(int)->decltype(std::declval<test_shared_ptr<T> >().get(), bool ())
+{
+       return true;
+}
+
+template <typename T>
+static constexpr auto
+can_call_get_on_temporary(...)->bool
+{
+       return false;
+}
+
+template <typename T>
+static void
+tests()
+{
+       {
+               T obj{3};
+               tracking_policy::reset();
+               tracked_shared_ptr<T> const ptr(&obj, libkern::retain);
+               T* raw = ptr.get();
+               CHECK(raw == &obj);
+               CHECK(ptr.get() == raw); // ptr didn't change
+               CHECK(tracking_policy::retains == 1);
+               CHECK(tracking_policy::releases == 0);
+       }
+
+       static_assert(!can_call_get_on_temporary<T>(int{}), "");
+}
+
+T_DECL(get, "intrusive_shared_ptr.get") {
+       tests<T>();
+       tests<T const>();
+}