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