1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  pointer detach() noexcept;
4bb611c8fSApple OSS Distributions //
5bb611c8fSApple OSS Distributions 
6bb611c8fSApple OSS Distributions #include <libkern/c++/intrusive_shared_ptr.h>
7bb611c8fSApple OSS Distributions #include <darwintest.h>
8bb611c8fSApple OSS Distributions #include "test_policy.h"
9bb611c8fSApple OSS Distributions 
10bb611c8fSApple OSS Distributions struct T {
11bb611c8fSApple OSS Distributions 	int i;
12bb611c8fSApple OSS Distributions };
13bb611c8fSApple OSS Distributions 
14bb611c8fSApple OSS Distributions template <typename T>
15bb611c8fSApple OSS Distributions static void
tests()16bb611c8fSApple OSS Distributions tests()
17bb611c8fSApple OSS Distributions {
18bb611c8fSApple OSS Distributions 	T obj{3};
19bb611c8fSApple OSS Distributions 
20bb611c8fSApple OSS Distributions 	tracking_policy::reset();
21bb611c8fSApple OSS Distributions 	tracked_shared_ptr<T> ptr(&obj, libkern::retain);
22bb611c8fSApple OSS Distributions 	T* raw = ptr.detach();
23bb611c8fSApple OSS Distributions 	CHECK(raw == &obj);
24bb611c8fSApple OSS Distributions 	CHECK(ptr.get() == nullptr); // ptr was set to null
25bb611c8fSApple OSS Distributions 	CHECK(tracking_policy::retains == 1);
26bb611c8fSApple OSS Distributions 	CHECK(tracking_policy::releases == 0);
27bb611c8fSApple OSS Distributions }
28bb611c8fSApple OSS Distributions 
29*8d741a5dSApple OSS Distributions T_DECL(detach, "intrusive_shared_ptr.detach", T_META_TAG_VM_PREFERRED) {
30bb611c8fSApple OSS Distributions 	tests<T>();
31bb611c8fSApple OSS Distributions 	tests<T const>();
32bb611c8fSApple OSS Distributions }
33