summaryrefslogtreecommitdiff
path: root/core/fxcrt/weak_ptr_unittest.cpp
blob: 22a723043b2d646a6081758372c35cc6b80c1fb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2016 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/fxcrt/weak_ptr.h"

#include <memory>
#include <utility>

#include "core/fxcrt/fx_memory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace fxcrt {
namespace {

class PseudoDeletable;
using WeakTestPtr = WeakPtr<PseudoDeletable, ReleaseDeleter<PseudoDeletable>>;
using UniqueTestPtr =
    std::unique_ptr<PseudoDeletable, ReleaseDeleter<PseudoDeletable>>;

class PseudoDeletable {
 public:
  PseudoDeletable() : delete_count_(0) {}
  void Release() {
    ++delete_count_;
    next_.Reset();
  }
  void SetNext(const WeakTestPtr& next) { next_ = next; }
  int delete_count() const { return delete_count_; }

 private:
  int delete_count_;
  WeakTestPtr next_;
};

}  // namespace

TEST(WeakPtr, Null) {
  WeakTestPtr ptr1;
  EXPECT_FALSE(ptr1);

  WeakTestPtr ptr2;
  EXPECT_TRUE(ptr1 == ptr2);
  EXPECT_FALSE(ptr1 != ptr2);

  WeakTestPtr ptr3(ptr1);
  EXPECT_TRUE(ptr1 == ptr3);
  EXPECT_FALSE(ptr1 != ptr3);

  WeakTestPtr ptr4 = ptr1;
  EXPECT_TRUE(ptr1 == ptr4);
  EXPECT_FALSE(ptr1 != ptr4);
}

TEST(WeakPtr, NonNull) {
  PseudoDeletable thing;
  EXPECT_EQ(0, thing.delete_count());
  {
    UniqueTestPtr unique(&thing);
    WeakTestPtr ptr1(std::move(unique));
    EXPECT_TRUE(ptr1);
    EXPECT_EQ(&thing, ptr1.Get());

    WeakTestPtr ptr2;
    EXPECT_FALSE(ptr1 == ptr2);
    EXPECT_TRUE(ptr1 != ptr2);
    {
      WeakTestPtr ptr3(ptr1);
      EXPECT_TRUE(ptr1 == ptr3);
      EXPECT_FALSE(ptr1 != ptr3);
      EXPECT_EQ(&thing, ptr3.Get());
      {
        WeakTestPtr ptr4 = ptr1;
        EXPECT_TRUE(ptr1 == ptr4);
        EXPECT_FALSE(ptr1 != ptr4);
        EXPECT_EQ(&thing, ptr4.Get());
      }
    }
    EXPECT_EQ(0, thing.delete_count());
  }
  EXPECT_EQ(1, thing.delete_count());
}

TEST(WeakPtr, ResetNull) {
  PseudoDeletable thing;
  {
    UniqueTestPtr unique(&thing);
    WeakTestPtr ptr1(std::move(unique));
    WeakTestPtr ptr2 = ptr1;
    ptr1.Reset();
    EXPECT_FALSE(ptr1);
    EXPECT_EQ(nullptr, ptr1.Get());
    EXPECT_TRUE(ptr2);
    EXPECT_EQ(&thing, ptr2.Get());
    EXPECT_FALSE(ptr1 == ptr2);
    EXPECT_TRUE(ptr1 != ptr2);
    EXPECT_EQ(0, thing.delete_count());
  }
  EXPECT_EQ(1, thing.delete_count());
}

TEST(WeakPtr, ResetNonNull) {
  PseudoDeletable thing1;
  PseudoDeletable thing2;
  {
    UniqueTestPtr unique1(&thing1);
    WeakTestPtr ptr1(std::move(unique1));
    WeakTestPtr ptr2 = ptr1;
    UniqueTestPtr unique2(&thing2);
    ptr2.Reset(std::move(unique2));
    EXPECT_TRUE(ptr1);
    EXPECT_EQ(&thing1, ptr1.Get());
    EXPECT_TRUE(ptr2);
    EXPECT_EQ(&thing2, ptr2.Get());
    EXPECT_FALSE(ptr1 == ptr2);
    EXPECT_TRUE(ptr1 != ptr2);
    EXPECT_EQ(0, thing1.delete_count());
    EXPECT_EQ(0, thing2.delete_count());
  }
  EXPECT_EQ(1, thing1.delete_count());
  EXPECT_EQ(1, thing2.delete_count());
}

TEST(WeakPtr, DeleteObject) {
  PseudoDeletable thing;
  {
    UniqueTestPtr unique(&thing);
    WeakTestPtr ptr1(std::move(unique));
    WeakTestPtr ptr2 = ptr1;
    ptr1.DeleteObject();
    EXPECT_FALSE(ptr1);
    EXPECT_EQ(nullptr, ptr1.Get());
    EXPECT_FALSE(ptr2);
    EXPECT_EQ(nullptr, ptr2.Get());
    EXPECT_FALSE(ptr1 == ptr2);
    EXPECT_TRUE(ptr1 != ptr2);
    EXPECT_EQ(1, thing.delete_count());
  }
  EXPECT_EQ(1, thing.delete_count());
}

TEST(WeakPtr, Cyclic) {
  PseudoDeletable thing1;
  PseudoDeletable thing2;
  {
    UniqueTestPtr unique1(&thing1);
    UniqueTestPtr unique2(&thing2);
    WeakTestPtr ptr1(std::move(unique1));
    WeakTestPtr ptr2(std::move(unique2));
    ptr1->SetNext(ptr2);
    ptr2->SetNext(ptr1);
  }
  // Leaks without explicit clear.
  EXPECT_EQ(0, thing1.delete_count());
  EXPECT_EQ(0, thing2.delete_count());
}

TEST(WeakPtr, CyclicDeleteObject) {
  PseudoDeletable thing1;
  PseudoDeletable thing2;
  {
    UniqueTestPtr unique1(&thing1);
    UniqueTestPtr unique2(&thing2);
    WeakTestPtr ptr1(std::move(unique1));
    WeakTestPtr ptr2(std::move(unique2));
    ptr1->SetNext(ptr2);
    ptr2->SetNext(ptr1);
    ptr1.DeleteObject();
    EXPECT_EQ(1, thing1.delete_count());
    EXPECT_EQ(0, thing2.delete_count());
  }
  EXPECT_EQ(1, thing1.delete_count());
  EXPECT_EQ(1, thing2.delete_count());
}

}  // namespace fxcrt