xref: /xnu-8796.141.3/tests/intrusive_shared_ptr_src/get.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions //
2*1b191cb5SApple OSS Distributions // Tests for
3*1b191cb5SApple OSS Distributions //  pointer get() const noexcept;
4*1b191cb5SApple OSS Distributions //
5*1b191cb5SApple OSS Distributions 
6*1b191cb5SApple OSS Distributions #include <libkern/c++/intrusive_shared_ptr.h>
7*1b191cb5SApple OSS Distributions #include "test_policy.h"
8*1b191cb5SApple OSS Distributions #include <darwintest.h>
9*1b191cb5SApple OSS Distributions #include <utility>
10*1b191cb5SApple OSS Distributions 
11*1b191cb5SApple OSS Distributions struct T {
12*1b191cb5SApple OSS Distributions 	int i;
13*1b191cb5SApple OSS Distributions };
14*1b191cb5SApple OSS Distributions 
15*1b191cb5SApple OSS Distributions template <typename T>
16*1b191cb5SApple OSS Distributions static constexpr auto
can_call_get_on_temporary(int)17*1b191cb5SApple OSS Distributions can_call_get_on_temporary(int)->decltype(std::declval<test_shared_ptr<T> >().get(), bool ())
18*1b191cb5SApple OSS Distributions {
19*1b191cb5SApple OSS Distributions 	return true;
20*1b191cb5SApple OSS Distributions }
21*1b191cb5SApple OSS Distributions 
22*1b191cb5SApple OSS Distributions template <typename T>
23*1b191cb5SApple OSS Distributions static constexpr auto
can_call_get_on_temporary(...)24*1b191cb5SApple OSS Distributions can_call_get_on_temporary(...)->bool
25*1b191cb5SApple OSS Distributions {
26*1b191cb5SApple OSS Distributions 	return false;
27*1b191cb5SApple OSS Distributions }
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions template <typename T>
30*1b191cb5SApple OSS Distributions static void
tests()31*1b191cb5SApple OSS Distributions tests()
32*1b191cb5SApple OSS Distributions {
33*1b191cb5SApple OSS Distributions 	{
34*1b191cb5SApple OSS Distributions 		T obj{3};
35*1b191cb5SApple OSS Distributions 		tracking_policy::reset();
36*1b191cb5SApple OSS Distributions 		tracked_shared_ptr<T> const ptr(&obj, libkern::retain);
37*1b191cb5SApple OSS Distributions 		T* raw = ptr.get();
38*1b191cb5SApple OSS Distributions 		CHECK(raw == &obj);
39*1b191cb5SApple OSS Distributions 		CHECK(ptr.get() == raw); // ptr didn't change
40*1b191cb5SApple OSS Distributions 		CHECK(tracking_policy::retains == 1);
41*1b191cb5SApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
42*1b191cb5SApple OSS Distributions 	}
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions 	static_assert(!can_call_get_on_temporary<T>(int{}), "");
45*1b191cb5SApple OSS Distributions }
46*1b191cb5SApple OSS Distributions 
47*1b191cb5SApple OSS Distributions T_DECL(get, "intrusive_shared_ptr.get") {
48*1b191cb5SApple OSS Distributions 	tests<T>();
49*1b191cb5SApple OSS Distributions 	tests<T const>();
50*1b191cb5SApple OSS Distributions }
51