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