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