1 //
2 // Tests for
3 //  template <typename T, typename U, typename R>
4 //  bool operator==(intrusive_shared_ptr<T, R> const& x, intrusive_shared_ptr<U, R> const& y);
5 //
6 //  template <typename T, typename U, typename R>
7 //  bool operator!=(intrusive_shared_ptr<T, R> const& x, intrusive_shared_ptr<U, R> const& y);
8 //
9 
10 #include <libkern/c++/intrusive_shared_ptr.h>
11 #include <darwintest.h>
12 #include "test_policy.h"
13 
14 struct Base { int i; };
15 struct Derived : Base { };
16 
17 struct T { int i; };
18 
19 template <typename T, typename U>
20 static void
21 check_eq(T t, U u)
22 {
23 	CHECK(t == u);
24 	CHECK(u == t);
25 	CHECK(!(t != u));
26 	CHECK(!(u != t));
27 }
28 
29 template <typename T, typename U>
30 static void
31 check_ne(T t, U u)
32 {
33 	CHECK(!(t == u));
34 	CHECK(!(u == t));
35 	CHECK(t != u);
36 	CHECK(u != t);
37 }
38 
39 template <typename T, typename TQual>
40 static void
41 tests()
42 {
43 	T obj1{1};
44 	T obj2{2};
45 
46 	{
47 		test_shared_ptr<TQual> const a(&obj1, libkern::no_retain);
48 		test_shared_ptr<TQual> const b(&obj2, libkern::no_retain);
49 		check_ne(a, b);
50 	}
51 
52 	{
53 		test_shared_ptr<TQual> const a(&obj1, libkern::no_retain);
54 		test_shared_ptr<TQual> const b(&obj1, libkern::no_retain);
55 		check_eq(a, b);
56 	}
57 
58 	{
59 		test_shared_ptr<TQual> const a = nullptr;
60 		test_shared_ptr<TQual> const b(&obj2, libkern::no_retain);
61 		check_ne(a, b);
62 	}
63 
64 	{
65 		test_shared_ptr<TQual> const a = nullptr;
66 		test_shared_ptr<TQual> const b = nullptr;
67 		check_eq(a, b);
68 	}
69 }
70 
71 template <typename T, typename RelatedT>
72 static void
73 tests_convert()
74 {
75 	T obj{1};
76 	test_shared_ptr<T> const a(&obj, libkern::no_retain);
77 	test_shared_ptr<RelatedT> const b(&obj, libkern::no_retain);
78 	check_eq(a, b);
79 }
80 
81 T_DECL(compare_equal, "intrusive_shared_ptr.compare.equal") {
82 	tests<T, T>();
83 	tests<T, T const>();
84 	tests_convert<Derived, Base>();
85 	tests_convert<Derived, Base const>();
86 }
87