From d4e7ca034cbbe29911b08eb5c252aa0d05b6a429 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 30 Aug 2018 22:40:07 +0000 Subject: 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 Commit-Queue: Tom Sepez --- BUILD.gn | 1 + core/fxcrt/autorestorer_unittest.cpp | 143 +++++++++++++++++++++++++++++++++++ core/fxcrt/retain_ptr_unittest.cpp | 17 +---- testing/pseudo_retainable.h | 26 +++++++ 4 files changed, 171 insertions(+), 16 deletions(-) create mode 100644 testing/pseudo_retainable.h 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 restorer(&x); + EXPECT_EQ(5, x); + } + EXPECT_EQ(5, x); +} + +TEST(fxcrt, AutoRestorerAbandon) { + int x = 5; { AutoRestorer 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 ptr(&x); + { + AutoRestorer> restorer(&ptr); + ptr = &y; + EXPECT_EQ(&y, ptr); + } + EXPECT_EQ(&x, ptr); +} + +TEST(fxcrt, AutoRestorerUnownedNoOp) { + int x = 5; + UnownedPtr ptr(&x); + { + AutoRestorer> restorer(&ptr); + EXPECT_EQ(&x, ptr); + } + EXPECT_EQ(&x, ptr); +} + +TEST(fxcrt, AutoRestorerUnownedPtrAbandon) { + int x = 5; + int y = 6; + UnownedPtr ptr(&x); + { + AutoRestorer> restorer(&ptr); + ptr = &y; + EXPECT_EQ(&y, ptr); + restorer.AbandonRestoration(); + } + EXPECT_EQ(&y, ptr); +} + +TEST(fxcrt, AutoRestorerRetainPtr) { + PseudoRetainable obj1; + PseudoRetainable obj2; + RetainPtr ptr(&obj1); + EXPECT_EQ(1, obj1.retain_count()); + EXPECT_EQ(0, obj1.release_count()); + { + AutoRestorer> 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 ptr(&obj1); + EXPECT_EQ(1, obj1.retain_count()); + EXPECT_EQ(0, obj1.release_count()); + { + AutoRestorer> 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 ptr(&obj1); + EXPECT_EQ(1, obj1.retain_count()); + EXPECT_EQ(0, obj1.release_count()); + { + AutoRestorer> 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 #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 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_ -- cgit v1.2.3