QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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/third_party/quiche/src/quic/platform/api/quic_reference_counted.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 8 | |
| 9 | namespace quic { |
| 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 | protected: |
| 20 | ~Base() override { *destroyed_ = true; } |
| 21 | |
| 22 | private: |
| 23 | bool* destroyed_; |
| 24 | }; |
| 25 | |
| 26 | class Derived : public Base { |
| 27 | public: |
| 28 | explicit Derived(bool* destroyed) : Base(destroyed) {} |
| 29 | |
| 30 | private: |
| 31 | ~Derived() override {} |
| 32 | }; |
| 33 | |
| 34 | class QuicReferenceCountedTest : public QuicTest {}; |
| 35 | |
| 36 | TEST_F(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_F(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_F(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_F(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_F(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_F(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_F(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_F(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_F(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_F(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_F(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 quic |