| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium 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 "net/quic/platform/api/quic_reference_counted.h" | 5 #include "net/quic/platform/api/quic_reference_counted.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "net/quic/platform/api/quic_test.h" |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 namespace test { | 10 namespace test { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 class Base : public QuicReferenceCounted { | 13 class Base : public QuicReferenceCounted { |
| 14 public: | 14 public: |
| 15 explicit Base(bool* destroyed) : destroyed_(destroyed) { | 15 explicit Base(bool* destroyed) : destroyed_(destroyed) { |
| 16 *destroyed_ = false; | 16 *destroyed_ = false; |
| 17 } | 17 } |
| 18 | 18 |
| 19 bool destroyed() const { return *destroyed_; } | 19 bool destroyed() const { return *destroyed_; } |
| 20 | 20 |
| 21 protected: | 21 protected: |
| 22 ~Base() override { *destroyed_ = true; } | 22 ~Base() override { *destroyed_ = true; } |
| 23 | 23 |
| 24 private: | 24 private: |
| 25 bool* destroyed_; | 25 bool* destroyed_; |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 class Derived : public Base { | 28 class Derived : public Base { |
| 29 public: | 29 public: |
| 30 explicit Derived(bool* destroyed) : Base(destroyed) {} | 30 explicit Derived(bool* destroyed) : Base(destroyed) {} |
| 31 | 31 |
| 32 private: | 32 private: |
| 33 ~Derived() override {} | 33 ~Derived() override {} |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 TEST(QuicReferenceCountedTest, DefaultConstructor) { | 36 class QuicReferenceCountedTest : public QuicTest {}; |
| 37 |
| 38 TEST_F(QuicReferenceCountedTest, DefaultConstructor) { |
| 37 QuicReferenceCountedPointer<Base> a; | 39 QuicReferenceCountedPointer<Base> a; |
| 38 EXPECT_EQ(nullptr, a); | 40 EXPECT_EQ(nullptr, a); |
| 39 EXPECT_EQ(nullptr, a.get()); | 41 EXPECT_EQ(nullptr, a.get()); |
| 40 EXPECT_FALSE(a); | 42 EXPECT_FALSE(a); |
| 41 } | 43 } |
| 42 | 44 |
| 43 TEST(QuicReferenceCountedTest, ConstructFromRawPointer) { | 45 TEST_F(QuicReferenceCountedTest, ConstructFromRawPointer) { |
| 44 bool destroyed = false; | 46 bool destroyed = false; |
| 45 { | 47 { |
| 46 QuicReferenceCountedPointer<Base> a(new Base(&destroyed)); | 48 QuicReferenceCountedPointer<Base> a(new Base(&destroyed)); |
| 47 EXPECT_FALSE(destroyed); | 49 EXPECT_FALSE(destroyed); |
| 48 } | 50 } |
| 49 EXPECT_TRUE(destroyed); | 51 EXPECT_TRUE(destroyed); |
| 50 } | 52 } |
| 51 | 53 |
| 52 TEST(QuicReferenceCountedTest, RawPointerAssignment) { | 54 TEST_F(QuicReferenceCountedTest, RawPointerAssignment) { |
| 53 bool destroyed = false; | 55 bool destroyed = false; |
| 54 { | 56 { |
| 55 QuicReferenceCountedPointer<Base> a; | 57 QuicReferenceCountedPointer<Base> a; |
| 56 Base* rct = new Base(&destroyed); | 58 Base* rct = new Base(&destroyed); |
| 57 a = rct; | 59 a = rct; |
| 58 EXPECT_FALSE(destroyed); | 60 EXPECT_FALSE(destroyed); |
| 59 } | 61 } |
| 60 EXPECT_TRUE(destroyed); | 62 EXPECT_TRUE(destroyed); |
| 61 } | 63 } |
| 62 | 64 |
| 63 TEST(QuicReferenceCountedTest, PointerCopy) { | 65 TEST_F(QuicReferenceCountedTest, PointerCopy) { |
| 64 bool destroyed = false; | 66 bool destroyed = false; |
| 65 { | 67 { |
| 66 QuicReferenceCountedPointer<Base> a(new Base(&destroyed)); | 68 QuicReferenceCountedPointer<Base> a(new Base(&destroyed)); |
| 67 { | 69 { |
| 68 QuicReferenceCountedPointer<Base> b(a); | 70 QuicReferenceCountedPointer<Base> b(a); |
| 69 EXPECT_EQ(a, b); | 71 EXPECT_EQ(a, b); |
| 70 EXPECT_FALSE(destroyed); | 72 EXPECT_FALSE(destroyed); |
| 71 } | 73 } |
| 72 EXPECT_FALSE(destroyed); | 74 EXPECT_FALSE(destroyed); |
| 73 } | 75 } |
| 74 EXPECT_TRUE(destroyed); | 76 EXPECT_TRUE(destroyed); |
| 75 } | 77 } |
| 76 | 78 |
| 77 TEST(QuicReferenceCountedTest, PointerCopyAssignment) { | 79 TEST_F(QuicReferenceCountedTest, PointerCopyAssignment) { |
| 78 bool destroyed = false; | 80 bool destroyed = false; |
| 79 { | 81 { |
| 80 QuicReferenceCountedPointer<Base> a(new Base(&destroyed)); | 82 QuicReferenceCountedPointer<Base> a(new Base(&destroyed)); |
| 81 { | 83 { |
| 82 QuicReferenceCountedPointer<Base> b = a; | 84 QuicReferenceCountedPointer<Base> b = a; |
| 83 EXPECT_EQ(a, b); | 85 EXPECT_EQ(a, b); |
| 84 EXPECT_FALSE(destroyed); | 86 EXPECT_FALSE(destroyed); |
| 85 } | 87 } |
| 86 EXPECT_FALSE(destroyed); | 88 EXPECT_FALSE(destroyed); |
| 87 } | 89 } |
| 88 EXPECT_TRUE(destroyed); | 90 EXPECT_TRUE(destroyed); |
| 89 } | 91 } |
| 90 | 92 |
| 91 TEST(QuicReferenceCountedTest, PointerCopyFromOtherType) { | 93 TEST_F(QuicReferenceCountedTest, PointerCopyFromOtherType) { |
| 92 bool destroyed = false; | 94 bool destroyed = false; |
| 93 { | 95 { |
| 94 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); | 96 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); |
| 95 { | 97 { |
| 96 QuicReferenceCountedPointer<Base> b(a); | 98 QuicReferenceCountedPointer<Base> b(a); |
| 97 EXPECT_EQ(a.get(), b.get()); | 99 EXPECT_EQ(a.get(), b.get()); |
| 98 EXPECT_FALSE(destroyed); | 100 EXPECT_FALSE(destroyed); |
| 99 } | 101 } |
| 100 EXPECT_FALSE(destroyed); | 102 EXPECT_FALSE(destroyed); |
| 101 } | 103 } |
| 102 EXPECT_TRUE(destroyed); | 104 EXPECT_TRUE(destroyed); |
| 103 } | 105 } |
| 104 | 106 |
| 105 TEST(QuicReferenceCountedTest, PointerCopyAssignmentFromOtherType) { | 107 TEST_F(QuicReferenceCountedTest, PointerCopyAssignmentFromOtherType) { |
| 106 bool destroyed = false; | 108 bool destroyed = false; |
| 107 { | 109 { |
| 108 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); | 110 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); |
| 109 { | 111 { |
| 110 QuicReferenceCountedPointer<Base> b = a; | 112 QuicReferenceCountedPointer<Base> b = a; |
| 111 EXPECT_EQ(a.get(), b.get()); | 113 EXPECT_EQ(a.get(), b.get()); |
| 112 EXPECT_FALSE(destroyed); | 114 EXPECT_FALSE(destroyed); |
| 113 } | 115 } |
| 114 EXPECT_FALSE(destroyed); | 116 EXPECT_FALSE(destroyed); |
| 115 } | 117 } |
| 116 EXPECT_TRUE(destroyed); | 118 EXPECT_TRUE(destroyed); |
| 117 } | 119 } |
| 118 | 120 |
| 119 TEST(QuicReferenceCountedTest, PointerMove) { | 121 TEST_F(QuicReferenceCountedTest, PointerMove) { |
| 120 bool destroyed = false; | 122 bool destroyed = false; |
| 121 QuicReferenceCountedPointer<Base> a(new Derived(&destroyed)); | 123 QuicReferenceCountedPointer<Base> a(new Derived(&destroyed)); |
| 122 EXPECT_FALSE(destroyed); | 124 EXPECT_FALSE(destroyed); |
| 123 QuicReferenceCountedPointer<Base> b(std::move(a)); | 125 QuicReferenceCountedPointer<Base> b(std::move(a)); |
| 124 EXPECT_FALSE(destroyed); | 126 EXPECT_FALSE(destroyed); |
| 125 EXPECT_NE(nullptr, b); | 127 EXPECT_NE(nullptr, b); |
| 126 EXPECT_EQ(nullptr, a); // NOLINT | 128 EXPECT_EQ(nullptr, a); // NOLINT |
| 127 | 129 |
| 128 b = nullptr; | 130 b = nullptr; |
| 129 EXPECT_TRUE(destroyed); | 131 EXPECT_TRUE(destroyed); |
| 130 } | 132 } |
| 131 | 133 |
| 132 TEST(QuicReferenceCountedTest, PointerMoveAssignment) { | 134 TEST_F(QuicReferenceCountedTest, PointerMoveAssignment) { |
| 133 bool destroyed = false; | 135 bool destroyed = false; |
| 134 QuicReferenceCountedPointer<Base> a(new Derived(&destroyed)); | 136 QuicReferenceCountedPointer<Base> a(new Derived(&destroyed)); |
| 135 EXPECT_FALSE(destroyed); | 137 EXPECT_FALSE(destroyed); |
| 136 QuicReferenceCountedPointer<Base> b = std::move(a); | 138 QuicReferenceCountedPointer<Base> b = std::move(a); |
| 137 EXPECT_FALSE(destroyed); | 139 EXPECT_FALSE(destroyed); |
| 138 EXPECT_NE(nullptr, b); | 140 EXPECT_NE(nullptr, b); |
| 139 EXPECT_EQ(nullptr, a); // NOLINT | 141 EXPECT_EQ(nullptr, a); // NOLINT |
| 140 | 142 |
| 141 b = nullptr; | 143 b = nullptr; |
| 142 EXPECT_TRUE(destroyed); | 144 EXPECT_TRUE(destroyed); |
| 143 } | 145 } |
| 144 | 146 |
| 145 TEST(QuicReferenceCountedTest, PointerMoveFromOtherType) { | 147 TEST_F(QuicReferenceCountedTest, PointerMoveFromOtherType) { |
| 146 bool destroyed = false; | 148 bool destroyed = false; |
| 147 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); | 149 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); |
| 148 EXPECT_FALSE(destroyed); | 150 EXPECT_FALSE(destroyed); |
| 149 QuicReferenceCountedPointer<Base> b(std::move(a)); | 151 QuicReferenceCountedPointer<Base> b(std::move(a)); |
| 150 EXPECT_FALSE(destroyed); | 152 EXPECT_FALSE(destroyed); |
| 151 EXPECT_NE(nullptr, b); | 153 EXPECT_NE(nullptr, b); |
| 152 EXPECT_EQ(nullptr, a); // NOLINT | 154 EXPECT_EQ(nullptr, a); // NOLINT |
| 153 | 155 |
| 154 b = nullptr; | 156 b = nullptr; |
| 155 EXPECT_TRUE(destroyed); | 157 EXPECT_TRUE(destroyed); |
| 156 } | 158 } |
| 157 | 159 |
| 158 TEST(QuicReferenceCountedTest, PointerMoveAssignmentFromOtherType) { | 160 TEST_F(QuicReferenceCountedTest, PointerMoveAssignmentFromOtherType) { |
| 159 bool destroyed = false; | 161 bool destroyed = false; |
| 160 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); | 162 QuicReferenceCountedPointer<Derived> a(new Derived(&destroyed)); |
| 161 EXPECT_FALSE(destroyed); | 163 EXPECT_FALSE(destroyed); |
| 162 QuicReferenceCountedPointer<Base> b = std::move(a); | 164 QuicReferenceCountedPointer<Base> b = std::move(a); |
| 163 EXPECT_FALSE(destroyed); | 165 EXPECT_FALSE(destroyed); |
| 164 EXPECT_NE(nullptr, b); | 166 EXPECT_NE(nullptr, b); |
| 165 EXPECT_EQ(nullptr, a); // NOLINT | 167 EXPECT_EQ(nullptr, a); // NOLINT |
| 166 | 168 |
| 167 b = nullptr; | 169 b = nullptr; |
| 168 EXPECT_TRUE(destroyed); | 170 EXPECT_TRUE(destroyed); |
| 169 } | 171 } |
| 170 | 172 |
| 171 } // namespace | 173 } // namespace |
| 172 } // namespace test | 174 } // namespace test |
| 173 } // namespace net | 175 } // namespace net |
| OLD | NEW |