| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/fxcrt/cfx_count_ref.h" | 5 #include "core/fxcrt/cfx_shared_copy_on_write.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "testing/fx_string_testhelpers.h" | 10 #include "testing/fx_string_testhelpers.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 class Observer { | 15 class Observer { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 ~Object() { observer_->OnDestruct(name_); } | 40 ~Object() { observer_->OnDestruct(name_); } |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 std::string name_; | 43 std::string name_; |
| 44 Observer* observer_; | 44 Observer* observer_; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 } // namespace | 47 } // namespace |
| 48 | 48 |
| 49 TEST(fxcrt, CountRefNull) { | 49 TEST(fxcrt, SharedCopyOnWriteNull) { |
| 50 Observer observer; | 50 Observer observer; |
| 51 { | 51 { |
| 52 CFX_CountRef<Object> ptr; | 52 CFX_SharedCopyOnWrite<Object> ptr; |
| 53 EXPECT_EQ(nullptr, ptr.GetObject()); | 53 EXPECT_EQ(nullptr, ptr.GetObject()); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 TEST(fxcrt, CountRefCopy) { | 57 TEST(fxcrt, SharedCopyOnWriteCopy) { |
| 58 Observer observer; | 58 Observer observer; |
| 59 { | 59 { |
| 60 CFX_CountRef<Object> ptr1; | 60 CFX_SharedCopyOnWrite<Object> ptr1; |
| 61 ptr1.Emplace(&observer, std::string("one")); | 61 ptr1.Emplace(&observer, std::string("one")); |
| 62 { | 62 { |
| 63 CFX_CountRef<Object> ptr2 = ptr1; | 63 CFX_SharedCopyOnWrite<Object> ptr2 = ptr1; |
| 64 EXPECT_EQ(1, observer.GetConstructionCount("one")); | 64 EXPECT_EQ(1, observer.GetConstructionCount("one")); |
| 65 EXPECT_EQ(0, observer.GetDestructionCount("one")); | 65 EXPECT_EQ(0, observer.GetDestructionCount("one")); |
| 66 } | 66 } |
| 67 { | 67 { |
| 68 CFX_CountRef<Object> ptr3(ptr1); | 68 CFX_SharedCopyOnWrite<Object> ptr3(ptr1); |
| 69 EXPECT_EQ(1, observer.GetConstructionCount("one")); | 69 EXPECT_EQ(1, observer.GetConstructionCount("one")); |
| 70 EXPECT_EQ(0, observer.GetDestructionCount("one")); | 70 EXPECT_EQ(0, observer.GetDestructionCount("one")); |
| 71 } | 71 } |
| 72 EXPECT_EQ(1, observer.GetConstructionCount("one")); | 72 EXPECT_EQ(1, observer.GetConstructionCount("one")); |
| 73 EXPECT_EQ(0, observer.GetDestructionCount("one")); | 73 EXPECT_EQ(0, observer.GetDestructionCount("one")); |
| 74 } | 74 } |
| 75 EXPECT_EQ(1, observer.GetDestructionCount("one")); | 75 EXPECT_EQ(1, observer.GetDestructionCount("one")); |
| 76 } | 76 } |
| 77 | 77 |
| 78 TEST(fxcrt, CountRefAssignOverOld) { | 78 TEST(fxcrt, SharedCopyOnWriteAssignOverOld) { |
| 79 Observer observer; | 79 Observer observer; |
| 80 { | 80 { |
| 81 CFX_CountRef<Object> ptr1; | 81 CFX_SharedCopyOnWrite<Object> ptr1; |
| 82 ptr1.Emplace(&observer, std::string("one")); | 82 ptr1.Emplace(&observer, std::string("one")); |
| 83 ptr1.Emplace(&observer, std::string("two")); | 83 ptr1.Emplace(&observer, std::string("two")); |
| 84 EXPECT_EQ(1, observer.GetConstructionCount("one")); | 84 EXPECT_EQ(1, observer.GetConstructionCount("one")); |
| 85 EXPECT_EQ(1, observer.GetConstructionCount("two")); | 85 EXPECT_EQ(1, observer.GetConstructionCount("two")); |
| 86 EXPECT_EQ(1, observer.GetDestructionCount("one")); | 86 EXPECT_EQ(1, observer.GetDestructionCount("one")); |
| 87 EXPECT_EQ(0, observer.GetDestructionCount("two")); | 87 EXPECT_EQ(0, observer.GetDestructionCount("two")); |
| 88 } | 88 } |
| 89 EXPECT_EQ(1, observer.GetDestructionCount("two")); | 89 EXPECT_EQ(1, observer.GetDestructionCount("two")); |
| 90 } | 90 } |
| 91 | 91 |
| 92 TEST(fxcrt, CountRefAssignOverRetained) { | 92 TEST(fxcrt, SharedCopyOnWriteAssignOverRetained) { |
| 93 Observer observer; | 93 Observer observer; |
| 94 { | 94 { |
| 95 CFX_CountRef<Object> ptr1; | 95 CFX_SharedCopyOnWrite<Object> ptr1; |
| 96 ptr1.Emplace(&observer, std::string("one")); | 96 ptr1.Emplace(&observer, std::string("one")); |
| 97 CFX_CountRef<Object> ptr2(ptr1); | 97 CFX_SharedCopyOnWrite<Object> ptr2(ptr1); |
| 98 ptr1.Emplace(&observer, std::string("two")); | 98 ptr1.Emplace(&observer, std::string("two")); |
| 99 EXPECT_EQ(1, observer.GetConstructionCount("one")); | 99 EXPECT_EQ(1, observer.GetConstructionCount("one")); |
| 100 EXPECT_EQ(1, observer.GetConstructionCount("two")); | 100 EXPECT_EQ(1, observer.GetConstructionCount("two")); |
| 101 EXPECT_EQ(0, observer.GetDestructionCount("one")); | 101 EXPECT_EQ(0, observer.GetDestructionCount("one")); |
| 102 EXPECT_EQ(0, observer.GetDestructionCount("two")); | 102 EXPECT_EQ(0, observer.GetDestructionCount("two")); |
| 103 } | 103 } |
| 104 EXPECT_EQ(1, observer.GetDestructionCount("one")); | 104 EXPECT_EQ(1, observer.GetDestructionCount("one")); |
| 105 EXPECT_EQ(1, observer.GetDestructionCount("two")); | 105 EXPECT_EQ(1, observer.GetDestructionCount("two")); |
| 106 } | 106 } |
| 107 | 107 |
| 108 TEST(fxcrt, CountRefGetModify) { | 108 TEST(fxcrt, SharedCopyOnWriteGetModify) { |
| 109 Observer observer; | 109 Observer observer; |
| 110 { | 110 { |
| 111 CFX_CountRef<Object> ptr; | 111 CFX_SharedCopyOnWrite<Object> ptr; |
| 112 EXPECT_NE(nullptr, ptr.GetPrivateCopy(&observer, std::string("one"))); | 112 EXPECT_NE(nullptr, ptr.GetPrivateCopy(&observer, std::string("one"))); |
| 113 EXPECT_EQ(1, observer.GetConstructionCount("one")); | 113 EXPECT_EQ(1, observer.GetConstructionCount("one")); |
| 114 EXPECT_EQ(0, observer.GetDestructionCount("one")); | 114 EXPECT_EQ(0, observer.GetDestructionCount("one")); |
| 115 | 115 |
| 116 EXPECT_NE(nullptr, ptr.GetPrivateCopy(&observer, std::string("one"))); | 116 EXPECT_NE(nullptr, ptr.GetPrivateCopy(&observer, std::string("one"))); |
| 117 EXPECT_EQ(1, observer.GetConstructionCount("one")); | 117 EXPECT_EQ(1, observer.GetConstructionCount("one")); |
| 118 EXPECT_EQ(0, observer.GetDestructionCount("one")); | 118 EXPECT_EQ(0, observer.GetDestructionCount("one")); |
| 119 { | 119 { |
| 120 CFX_CountRef<Object> other(ptr); | 120 CFX_SharedCopyOnWrite<Object> other(ptr); |
| 121 EXPECT_NE(nullptr, ptr.GetPrivateCopy(&observer, std::string("one"))); | 121 EXPECT_NE(nullptr, ptr.GetPrivateCopy(&observer, std::string("one"))); |
| 122 EXPECT_EQ(2, observer.GetConstructionCount("one")); | 122 EXPECT_EQ(2, observer.GetConstructionCount("one")); |
| 123 EXPECT_EQ(0, observer.GetDestructionCount("one")); | 123 EXPECT_EQ(0, observer.GetDestructionCount("one")); |
| 124 } | 124 } |
| 125 EXPECT_EQ(2, observer.GetConstructionCount("one")); | 125 EXPECT_EQ(2, observer.GetConstructionCount("one")); |
| 126 EXPECT_EQ(1, observer.GetDestructionCount("one")); | 126 EXPECT_EQ(1, observer.GetDestructionCount("one")); |
| 127 } | 127 } |
| 128 EXPECT_EQ(2, observer.GetDestructionCount("one")); | 128 EXPECT_EQ(2, observer.GetDestructionCount("one")); |
| 129 } | 129 } |
| OLD | NEW |