]> git.saurik.com Git - apple/xnu.git/blame - tests/intrusive_shared_ptr_src/reset.cpp
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / intrusive_shared_ptr_src / reset.cpp
CommitLineData
f427ee49
A
1//
2// Tests for
3// void reset() noexcept;
4//
5
6#include <libkern/c++/intrusive_shared_ptr.h>
7#include <darwintest.h>
8#include "test_policy.h"
9
10struct T {
11 int i;
12};
13
14template <typename T>
15static void
16tests()
17{
18 T obj{3};
19
20 // reset() on a non-null shared pointer
21 {
22 tracked_shared_ptr<T> ptr(&obj, libkern::retain);
23 tracking_policy::reset();
24 ptr.reset();
25 CHECK(tracking_policy::releases == 1);
26 CHECK(tracking_policy::retains == 0);
27 CHECK(ptr.get() == nullptr);
28 }
29
30 // reset() on a null shared pointer
31 {
32 tracked_shared_ptr<T> ptr = nullptr;
33 tracking_policy::reset();
34 ptr.reset();
35 CHECK(tracking_policy::releases == 0);
36 CHECK(tracking_policy::retains == 0);
37 CHECK(ptr.get() == nullptr);
38 }
39
40 // reset() as a self-reference
41 {
42 tracked_shared_ptr<T> ptr(&obj, libkern::retain);
43 tracked_shared_ptr<T> ptr2(&obj, libkern::retain);
44 CHECK(!ptr.reset());
45
46 CHECK(&ptr.reset() == &ptr);
47
48 // check short-circuiting
49 bool ok = (ptr.reset() && !ptr2.reset());
50 CHECK(ptr2.get() != nullptr);
51 }
52}
53
54T_DECL(reset, "intrusive_shared_ptr.reset") {
55 tests<T>();
56 tests<T const>();
57}