summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-08-30 22:40:07 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-08-30 22:40:07 +0000
commitd4e7ca034cbbe29911b08eb5c252aa0d05b6a429 (patch)
tree9620d3988a0d0aee8826c830adc1b8030bbf0b11
parent3b83e83fbfd24e521f7ecd7ce7f1019769bcaec7 (diff)
downloadpdfium-d4e7ca034cbbe29911b08eb5c252aa0d05b6a429.tar.xz
Add unit test for AutoRestorer<> on smart pointer types.
Test UnownedPtr<> and RetainPtr<>. We cannot operate against std::unique_ptr<> because we need a copyable type. Add test for self-reassignment on going out of scope. Move one test helper class to testing/ so it can be shared among unit tests. Allow it to recognize if it ever gets "destroyed", otherwise we can't be sure Retain/Release applied in the correct order. Change-Id: I13056094c70079f7283cbc7600948f81a64874b4 Reviewed-on: https://pdfium-review.googlesource.com/41690 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
-rw-r--r--BUILD.gn1
-rw-r--r--core/fxcrt/autorestorer_unittest.cpp143
-rw-r--r--core/fxcrt/retain_ptr_unittest.cpp17
-rw-r--r--testing/pseudo_retainable.h26
4 files changed, 171 insertions, 16 deletions
diff --git a/BUILD.gn b/BUILD.gn
index 27e4b2615a..1d4573a8f2 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -3047,6 +3047,7 @@ test("pdfium_embeddertests") {
"testing/embedder_test_timer_handling_delegate.h",
"testing/fake_file_access.cpp",
"testing/fake_file_access.h",
+ "testing/pseudo_retainable.h",
"testing/range_set.cpp",
"testing/range_set.h",
]
diff --git a/core/fxcrt/autorestorer_unittest.cpp b/core/fxcrt/autorestorer_unittest.cpp
index ac1d613e7c..6430fb67f6 100644
--- a/core/fxcrt/autorestorer_unittest.cpp
+++ b/core/fxcrt/autorestorer_unittest.cpp
@@ -3,7 +3,10 @@
// found in the LICENSE file.
#include "core/fxcrt/autorestorer.h"
+#include "core/fxcrt/retain_ptr.h"
+#include "core/fxcrt/unowned_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/pseudo_retainable.h"
TEST(fxcrt, AutoRestorer) {
int x = 5;
@@ -13,6 +16,19 @@ TEST(fxcrt, AutoRestorer) {
EXPECT_EQ(6, x);
}
EXPECT_EQ(5, x);
+}
+
+TEST(fxcrt, AutoRestorerNoOp) {
+ int x = 5;
+ {
+ AutoRestorer<int> restorer(&x);
+ EXPECT_EQ(5, x);
+ }
+ EXPECT_EQ(5, x);
+}
+
+TEST(fxcrt, AutoRestorerAbandon) {
+ int x = 5;
{
AutoRestorer<int> restorer(&x);
x = 6;
@@ -21,3 +37,130 @@ TEST(fxcrt, AutoRestorer) {
}
EXPECT_EQ(6, x);
}
+
+TEST(fxcrt, AutoRestorerUnownedPtr) {
+ int x = 5;
+ int y = 6;
+ UnownedPtr<int> ptr(&x);
+ {
+ AutoRestorer<UnownedPtr<int>> restorer(&ptr);
+ ptr = &y;
+ EXPECT_EQ(&y, ptr);
+ }
+ EXPECT_EQ(&x, ptr);
+}
+
+TEST(fxcrt, AutoRestorerUnownedNoOp) {
+ int x = 5;
+ UnownedPtr<int> ptr(&x);
+ {
+ AutoRestorer<UnownedPtr<int>> restorer(&ptr);
+ EXPECT_EQ(&x, ptr);
+ }
+ EXPECT_EQ(&x, ptr);
+}
+
+TEST(fxcrt, AutoRestorerUnownedPtrAbandon) {
+ int x = 5;
+ int y = 6;
+ UnownedPtr<int> ptr(&x);
+ {
+ AutoRestorer<UnownedPtr<int>> restorer(&ptr);
+ ptr = &y;
+ EXPECT_EQ(&y, ptr);
+ restorer.AbandonRestoration();
+ }
+ EXPECT_EQ(&y, ptr);
+}
+
+TEST(fxcrt, AutoRestorerRetainPtr) {
+ PseudoRetainable obj1;
+ PseudoRetainable obj2;
+ RetainPtr<PseudoRetainable> ptr(&obj1);
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ {
+ AutoRestorer<RetainPtr<PseudoRetainable>> restorer(&ptr);
+ // |obj1| is kept alive by restorer in case it need to be restored.
+ EXPECT_EQ(2, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ ptr.Reset(&obj2);
+ EXPECT_EQ(&obj2, ptr.Get());
+
+ // |obj1| released by |ptr| assignment.
+ EXPECT_TRUE(obj1.alive());
+ EXPECT_EQ(2, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+
+ // |obj2| now kept alive by |ptr|.
+ EXPECT_TRUE(obj1.alive());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(0, obj2.release_count());
+ }
+ EXPECT_EQ(&obj1, ptr.Get());
+
+ // |obj1| now kept alive again by |ptr|.
+ EXPECT_TRUE(obj1.alive());
+ EXPECT_EQ(3, obj1.retain_count());
+ EXPECT_EQ(2, obj1.release_count());
+
+ // |obj2| dead.
+ EXPECT_FALSE(obj2.alive());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(1, obj2.release_count());
+}
+
+TEST(fxcrt, AutoRestorerRetainPtrNoOp) {
+ PseudoRetainable obj1;
+ RetainPtr<PseudoRetainable> ptr(&obj1);
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ {
+ AutoRestorer<RetainPtr<PseudoRetainable>> restorer(&ptr);
+ EXPECT_EQ(2, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ }
+ EXPECT_EQ(&obj1, ptr.Get());
+
+ // Self-reassignement avoided ref churn.
+ EXPECT_TRUE(obj1.alive());
+ EXPECT_EQ(2, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+}
+
+TEST(fxcrt, AutoRestorerRetainPtrAbandon) {
+ PseudoRetainable obj1;
+ PseudoRetainable obj2;
+ RetainPtr<PseudoRetainable> ptr(&obj1);
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ {
+ AutoRestorer<RetainPtr<PseudoRetainable>> restorer(&ptr);
+ // |obj1| is kept alive by restorer in case it need to be restored.
+ EXPECT_EQ(2, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ ptr.Reset(&obj2);
+ EXPECT_EQ(&obj2, ptr.Get());
+
+ // |obj1| released by |ptr| assignment.
+ EXPECT_EQ(2, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+
+ // |obj2| now kept alive by |ptr|.
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(0, obj2.release_count());
+
+ restorer.AbandonRestoration();
+ }
+ EXPECT_EQ(&obj2, ptr.Get());
+
+ // |obj1| now dead as a result of abandonment.
+ EXPECT_FALSE(obj1.alive());
+ EXPECT_EQ(2, obj1.retain_count());
+ EXPECT_EQ(2, obj1.release_count());
+
+ // |obj2| kept alive by |ptr|.
+ EXPECT_TRUE(obj2.alive());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(0, obj2.release_count());
+}
diff --git a/core/fxcrt/retain_ptr_unittest.cpp b/core/fxcrt/retain_ptr_unittest.cpp
index f4b2994eae..5548e9bfd7 100644
--- a/core/fxcrt/retain_ptr_unittest.cpp
+++ b/core/fxcrt/retain_ptr_unittest.cpp
@@ -8,24 +8,9 @@
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/pseudo_retainable.h"
namespace fxcrt {
-namespace {
-
-class PseudoRetainable {
- public:
- PseudoRetainable() : retain_count_(0), release_count_(0) {}
- void Retain() { ++retain_count_; }
- void Release() { ++release_count_; }
- int retain_count() const { return retain_count_; }
- int release_count() const { return release_count_; }
-
- private:
- int retain_count_;
- int release_count_;
-};
-
-} // namespace
TEST(RetainPtr, Null) {
RetainPtr<PseudoRetainable> ptr;
diff --git a/testing/pseudo_retainable.h b/testing/pseudo_retainable.h
new file mode 100644
index 0000000000..c4390d697d
--- /dev/null
+++ b/testing/pseudo_retainable.h
@@ -0,0 +1,26 @@
+// Copyright 2018 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.
+
+#ifndef TESTING_PSEUDO_RETAINABLE_H_
+#define TESTING_PSEUDO_RETAINABLE_H_
+
+class PseudoRetainable {
+ public:
+ PseudoRetainable() = default;
+ void Retain() { ++retain_count_; }
+ void Release() {
+ if (++release_count_ == retain_count_)
+ alive_ = false;
+ }
+ bool alive() const { return alive_; }
+ int retain_count() const { return retain_count_; }
+ int release_count() const { return release_count_; }
+
+ private:
+ bool alive_ = true;
+ int retain_count_ = 0;
+ int release_count_ = 0;
+};
+
+#endif // TESTING_PSEUDO_RETAINABLE_H_