summaryrefslogtreecommitdiffstats
path: root/skia/ext/refptr_unittest.cc
blob: d7a401c507170b897637c123a732694157bd792a (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2012 The Chromium 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 "skia/ext/refptr.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace skia {
namespace {

TEST(RefPtrTest, ReferenceCounting) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  {
    // Adopt the reference from the caller on creation.
    RefPtr<SkRefCnt> refptr1 = AdoptRef(ref);
    EXPECT_TRUE(ref->unique());
    EXPECT_TRUE(refptr1->unique());

    EXPECT_EQ(ref, &*refptr1);
    EXPECT_EQ(ref, refptr1.get());

    {
      // Take a second reference for the second instance.
      RefPtr<SkRefCnt> refptr2(refptr1);
      EXPECT_FALSE(ref->unique());

      RefPtr<SkRefCnt> refptr3;
      EXPECT_FALSE(refptr3);

      // Take a third reference for the third instance.
      refptr3 = refptr1;
      EXPECT_FALSE(ref->unique());

      // Same object, so should have the same refcount.
      refptr2 = refptr3;
      EXPECT_FALSE(ref->unique());

      // Drop the object from refptr2, so it should lose its reference.
      EXPECT_TRUE(refptr2);
      refptr2.clear();
      EXPECT_FALSE(ref->unique());

      EXPECT_FALSE(refptr2);
      EXPECT_EQ(NULL, refptr2.get());

      EXPECT_TRUE(refptr3);
      EXPECT_FALSE(refptr3->unique());
      EXPECT_EQ(ref, &*refptr3);
      EXPECT_EQ(ref, refptr3.get());
    }

    // Drop a reference when the third object is destroyed.
    EXPECT_TRUE(ref->unique());
  }
}

TEST(RefPtrTest, Construct) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  // Adopt the reference from the caller on creation.
  RefPtr<SkRefCnt> refptr1(AdoptRef(ref));
  EXPECT_TRUE(ref->unique());
  EXPECT_TRUE(refptr1->unique());

  EXPECT_EQ(ref, &*refptr1);
  EXPECT_EQ(ref, refptr1.get());

  RefPtr<SkRefCnt> refptr2(refptr1);
  EXPECT_FALSE(ref->unique());
}

TEST(RefPtrTest, DeclareAndAssign) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  // Adopt the reference from the caller on creation.
  RefPtr<SkRefCnt> refptr1 = AdoptRef(ref);
  EXPECT_TRUE(ref->unique());
  EXPECT_TRUE(refptr1->unique());

  EXPECT_EQ(ref, &*refptr1);
  EXPECT_EQ(ref, refptr1.get());

  RefPtr<SkRefCnt> refptr2 = refptr1;
  EXPECT_FALSE(ref->unique());
}

TEST(RefPtrTest, Assign) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  // Adopt the reference from the caller on creation.
  RefPtr<SkRefCnt> refptr1;
  refptr1 = AdoptRef(ref);
  EXPECT_TRUE(ref->unique());
  EXPECT_TRUE(refptr1->unique());

  EXPECT_EQ(ref, &*refptr1);
  EXPECT_EQ(ref, refptr1.get());

  RefPtr<SkRefCnt> refptr2;
  refptr2 = refptr1;
  EXPECT_FALSE(ref->unique());
}

class Subclass : public SkRefCnt {};

TEST(RefPtrTest, Upcast) {
  RefPtr<Subclass> child = AdoptRef(new Subclass());
  EXPECT_TRUE(child->unique());

  RefPtr<SkRefCnt> parent = child;
  EXPECT_TRUE(child);
  EXPECT_TRUE(parent);

  EXPECT_FALSE(child->unique());
  EXPECT_FALSE(parent->unique());
}

// Counts the number of ref/unref operations (which require atomic operations)
// that are done.
class RefCountCounter : public SkRefCnt {
 public:
  void ref() const {
    ref_count_changes_++;
    SkRefCnt::ref();
  }
  void unref() const {
    ref_count_changes_++;
    SkRefCnt::unref();
  }
  int ref_count_changes() const { return ref_count_changes_; }
  void ResetRefCountChanges() { ref_count_changes_ = 0; }

 private:
  mutable int ref_count_changes_ = 0;
};

TEST(RefPtrTest, ConstructionFromTemporary) {
  // No ref count changes to move temporary into a local.
  RefPtr<RefCountCounter> object = skia::AdoptRef(new RefCountCounter);
  EXPECT_EQ(0, object->ref_count_changes());

  // Only one change to share the pointer.
  object->ResetRefCountChanges();
  RefPtr<RefCountCounter> shared = skia::SharePtr(object.get());
  EXPECT_EQ(1, object->ref_count_changes());

  // Two ref count changes for the extra ref when passed as an argument, but no
  // more.
  object->ResetRefCountChanges();
  auto do_nothing = [](RefPtr<RefCountCounter>) {};
  do_nothing(object);
  EXPECT_EQ(2, object->ref_count_changes());

  // No ref count changes when passing a newly adopted ref as an argument.
  auto lambda = [](RefPtr<RefCountCounter> arg) {
    EXPECT_EQ(0, arg->ref_count_changes());
  };
  lambda(skia::AdoptRef(new RefCountCounter));
}

TEST(RefPtrTest, AssignmentFromTemporary) {
  // No ref count changes to move temporary into a local.
  RefPtr<RefCountCounter> object;
  object = skia::AdoptRef(new RefCountCounter);
  EXPECT_EQ(0, object->ref_count_changes());

  // Only one change to share the pointer.
  object->ResetRefCountChanges();
  RefPtr<RefCountCounter> shared;
  shared = skia::SharePtr(object.get());
  EXPECT_EQ(1, object->ref_count_changes());
}

TEST(RefPtrTest, PassIntoArguments) {
  // No ref count changes when passing an argument with Pass().
  RefPtr<RefCountCounter> object = skia::AdoptRef(new RefCountCounter);
  RefPtr<RefCountCounter> object2 = object.Pass();
  auto lambda = [](RefPtr<RefCountCounter> arg) {
    EXPECT_EQ(0, arg->ref_count_changes());
  };
  lambda(object2.Pass());
}

class DestructionNotifier : public SkRefCnt {
 public:
  DestructionNotifier(bool* flag) : flag_(flag) {}
  ~DestructionNotifier() override { *flag_ = true; }

 private:
  bool* flag_;
};

TEST(RefPtrTest, PassIntoSelf) {
  bool is_destroyed = false;
  RefPtr<DestructionNotifier> object =
      skia::AdoptRef(new DestructionNotifier(&is_destroyed));
  object = object.Pass();
  ASSERT_FALSE(is_destroyed);
  EXPECT_TRUE(object->unique());
}

}  // namespace
}  // namespace skia