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