| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <sstream> |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "macros.h" |
| 9 #include "nonstd_unique_ptr.h" |
| 10 |
| 11 using nonstd::unique_ptr; |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Used to test depth subtyping. |
| 16 class CtorDtorLoggerParent { |
| 17 public: |
| 18 virtual ~CtorDtorLoggerParent() {} |
| 19 |
| 20 virtual void SetPtr(int* ptr) = 0; |
| 21 |
| 22 virtual int SomeMeth(int x) const = 0; |
| 23 }; |
| 24 |
| 25 class CtorDtorLogger : public CtorDtorLoggerParent { |
| 26 public: |
| 27 CtorDtorLogger() : ptr_(nullptr) {} |
| 28 explicit CtorDtorLogger(int* ptr) { SetPtr(ptr); } |
| 29 ~CtorDtorLogger() override { --*ptr_; } |
| 30 |
| 31 void SetPtr(int* ptr) override { |
| 32 ptr_ = ptr; |
| 33 ++*ptr_; |
| 34 } |
| 35 |
| 36 int SomeMeth(int x) const override { return x; } |
| 37 |
| 38 private: |
| 39 int* ptr_; |
| 40 |
| 41 // Disallow evil constructors. |
| 42 CtorDtorLogger(const CtorDtorLogger&) = delete; |
| 43 void operator=(const CtorDtorLogger&) = delete; |
| 44 }; |
| 45 |
| 46 struct CountingDeleter { |
| 47 explicit CountingDeleter(int* count) : count_(count) {} |
| 48 inline void operator()(double* ptr) const { (*count_)++; } |
| 49 int* count_; |
| 50 }; |
| 51 |
| 52 // Do not delete this function! It's existence is to test that you can |
| 53 // return a temporarily constructed version of the scoper. |
| 54 unique_ptr<CtorDtorLogger> TestReturnOfType(int* constructed) { |
| 55 return unique_ptr<CtorDtorLogger>(new CtorDtorLogger(constructed)); |
| 56 } |
| 57 |
| 58 } // namespace |
| 59 |
| 60 TEST(UniquePtrTest, MoveTest) { |
| 61 int constructed = 0; |
| 62 int constructed4 = 0; |
| 63 { |
| 64 unique_ptr<CtorDtorLogger> ptr1(new CtorDtorLogger(&constructed)); |
| 65 EXPECT_EQ(1, constructed); |
| 66 EXPECT_TRUE(ptr1); |
| 67 |
| 68 unique_ptr<CtorDtorLogger> ptr2(nonstd::move(ptr1)); |
| 69 EXPECT_EQ(1, constructed); |
| 70 EXPECT_FALSE(ptr1); |
| 71 EXPECT_TRUE(ptr2); |
| 72 |
| 73 unique_ptr<CtorDtorLogger> ptr3; |
| 74 ptr3 = nonstd::move(ptr2); |
| 75 EXPECT_EQ(1, constructed); |
| 76 EXPECT_FALSE(ptr2); |
| 77 EXPECT_TRUE(ptr3); |
| 78 |
| 79 unique_ptr<CtorDtorLogger> ptr4(new CtorDtorLogger(&constructed4)); |
| 80 EXPECT_EQ(1, constructed4); |
| 81 ptr4 = nonstd::move(ptr3); |
| 82 EXPECT_EQ(0, constructed4); |
| 83 EXPECT_FALSE(ptr3); |
| 84 EXPECT_TRUE(ptr4); |
| 85 } |
| 86 EXPECT_EQ(0, constructed); |
| 87 } |
| OLD | NEW |