]> git.saurik.com Git - apple/xnu.git/blob - tests/intrusive_shared_ptr_src/reset.no_retain.cpp
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / intrusive_shared_ptr_src / reset.no_retain.cpp
1 //
2 // Tests for
3 // void reset(pointer p, no_retain_t) noexcept;
4 //
5
6 #include <libkern/c++/intrusive_shared_ptr.h>
7 #include <darwintest.h>
8 #include "test_policy.h"
9
10 struct T {
11 int i;
12 };
13
14 template <typename T>
15 static void
16 tests()
17 {
18 T obj1{1};
19 T obj2{2};
20
21 // reset() non-null shared pointer to non-null raw pointer
22 {
23 tracked_shared_ptr<T> ptr(&obj1, libkern::retain);
24 tracking_policy::reset();
25 ptr.reset(&obj2, libkern::no_retain);
26 CHECK(tracking_policy::releases == 1);
27 CHECK(tracking_policy::retains == 0);
28 CHECK(ptr.get() == &obj2);
29 }
30
31 // reset() null shared pointer to non-null raw pointer
32 {
33 tracked_shared_ptr<T> ptr = nullptr;
34 tracking_policy::reset();
35 ptr.reset(&obj2, libkern::no_retain);
36 CHECK(tracking_policy::releases == 0);
37 CHECK(tracking_policy::retains == 0);
38 CHECK(ptr.get() == &obj2);
39 }
40
41 // reset() non-null shared pointer to null raw pointer
42 {
43 tracked_shared_ptr<T> ptr(&obj1, libkern::retain);
44 tracking_policy::reset();
45 ptr.reset(nullptr, libkern::no_retain);
46 CHECK(tracking_policy::releases == 1);
47 CHECK(tracking_policy::retains == 0);
48 CHECK(ptr.get() == nullptr);
49 }
50
51 // reset() null shared pointer to null raw pointer
52 {
53 tracked_shared_ptr<T> ptr = nullptr;
54 tracking_policy::reset();
55 ptr.reset(nullptr, libkern::no_retain);
56 CHECK(tracking_policy::releases == 0);
57 CHECK(tracking_policy::retains == 0);
58 CHECK(ptr.get() == nullptr);
59 }
60
61 // reset() as a self-reference
62 {
63 tracked_shared_ptr<T> ptr;
64 tracked_shared_ptr<T> ptr2;
65 CHECK(ptr.reset(&obj2, libkern::no_retain));
66
67 // check short-circuiting
68 bool ok = (ptr.reset() && ptr2.reset(&obj1, libkern::no_retain));
69 CHECK(ptr2.get() == nullptr);
70 }
71 }
72
73 T_DECL(reset_no_retain, "intrusive_shared_ptr.reset.no_retain") {
74 tests<T>();
75 tests<T const>();
76 }