]> git.saurik.com Git - apple/xnu.git/blobdiff - tests/intrusive_shared_ptr_src/compare.equal.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / tests / intrusive_shared_ptr_src / compare.equal.cpp
diff --git a/tests/intrusive_shared_ptr_src/compare.equal.cpp b/tests/intrusive_shared_ptr_src/compare.equal.cpp
new file mode 100644 (file)
index 0000000..fbf6e7b
--- /dev/null
@@ -0,0 +1,86 @@
+//
+// Tests for
+//  template <typename T, typename U, typename R>
+//  bool operator==(intrusive_shared_ptr<T, R> const& x, intrusive_shared_ptr<U, R> const& y);
+//
+//  template <typename T, typename U, typename R>
+//  bool operator!=(intrusive_shared_ptr<T, R> const& x, intrusive_shared_ptr<U, R> const& y);
+//
+
+#include <libkern/c++/intrusive_shared_ptr.h>
+#include <darwintest.h>
+#include "test_policy.h"
+
+struct Base { int i; };
+struct Derived : Base { };
+
+struct T { int i; };
+
+template <typename T, typename U>
+static void
+check_eq(T t, U u)
+{
+       CHECK(t == u);
+       CHECK(u == t);
+       CHECK(!(t != u));
+       CHECK(!(u != t));
+}
+
+template <typename T, typename U>
+static void
+check_ne(T t, U u)
+{
+       CHECK(!(t == u));
+       CHECK(!(u == t));
+       CHECK(t != u);
+       CHECK(u != t);
+}
+
+template <typename T, typename TQual>
+static void
+tests()
+{
+       T obj1{1};
+       T obj2{2};
+
+       {
+               test_shared_ptr<TQual> const a(&obj1, libkern::no_retain);
+               test_shared_ptr<TQual> const b(&obj2, libkern::no_retain);
+               check_ne(a, b);
+       }
+
+       {
+               test_shared_ptr<TQual> const a(&obj1, libkern::no_retain);
+               test_shared_ptr<TQual> const b(&obj1, libkern::no_retain);
+               check_eq(a, b);
+       }
+
+       {
+               test_shared_ptr<TQual> const a = nullptr;
+               test_shared_ptr<TQual> const b(&obj2, libkern::no_retain);
+               check_ne(a, b);
+       }
+
+       {
+               test_shared_ptr<TQual> const a = nullptr;
+               test_shared_ptr<TQual> const b = nullptr;
+               check_eq(a, b);
+       }
+}
+
+template <typename T, typename RelatedT>
+static void
+tests_convert()
+{
+       T obj{1};
+       test_shared_ptr<T> const a(&obj, libkern::no_retain);
+       test_shared_ptr<RelatedT> const b(&obj, libkern::no_retain);
+       check_eq(a, b);
+}
+
+T_DECL(compare_equal, "intrusive_shared_ptr.compare.equal") {
+       tests<T, T>();
+       tests<T, T const>();
+       tests_convert<Derived, Base>();
+       tests_convert<Derived, Base const>();
+}