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 #include "net/quic/quic_arena_scoped_ptr.h" |
| 5 |
| 6 #include "net/quic/quic_one_block_arena.h" |
| 7 #include "testing/gmock/include/gmock/gmock.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 // #include "testing/base/public/gunit.h" |
| 10 |
| 11 namespace net { |
| 12 namespace { |
| 13 |
| 14 enum class TestParam { kFromHeap, kFromArena }; |
| 15 |
| 16 struct TestObject { |
| 17 explicit TestObject(uintptr_t value) : value(value) { buffer.resize(1024); } |
| 18 uintptr_t value; |
| 19 |
| 20 // Ensure that we have a non-trivial destructor that will leak memory if it's |
| 21 // not called. |
| 22 std::vector<char> buffer; |
| 23 }; |
| 24 |
| 25 class QuicArenaScopedPtrParamTest : public ::testing::TestWithParam<TestParam> { |
| 26 protected: |
| 27 QuicArenaScopedPtrParamTest() { FLAGS_quic_enable_arena_allocation = true; } |
| 28 |
| 29 QuicArenaScopedPtr<TestObject> CreateObject(uintptr_t value) { |
| 30 QuicArenaScopedPtr<TestObject> ptr; |
| 31 switch (GetParam()) { |
| 32 case TestParam::kFromHeap: |
| 33 ptr = QuicArenaScopedPtr<TestObject>(new TestObject(value)); |
| 34 CHECK(!ptr.is_from_arena()); |
| 35 break; |
| 36 case TestParam::kFromArena: |
| 37 ptr = arena_.New<TestObject>(value); |
| 38 CHECK(ptr.is_from_arena()); |
| 39 break; |
| 40 } |
| 41 return ptr; |
| 42 } |
| 43 |
| 44 private: |
| 45 QuicOneBlockArena<1024> arena_; |
| 46 }; |
| 47 |
| 48 INSTANTIATE_TEST_CASE_P(QuicArenaScopedPtrParamTest, |
| 49 QuicArenaScopedPtrParamTest, |
| 50 testing::Values(TestParam::kFromHeap, |
| 51 TestParam::kFromArena)); |
| 52 |
| 53 TEST(QuicArenaScopedPtrTest, NullObjects) { |
| 54 FLAGS_quic_enable_arena_allocation = true; |
| 55 QuicArenaScopedPtr<TestObject> def; |
| 56 QuicArenaScopedPtr<TestObject> null(nullptr); |
| 57 EXPECT_EQ(def, null); |
| 58 EXPECT_EQ(def, nullptr); |
| 59 EXPECT_EQ(null, nullptr); |
| 60 } |
| 61 |
| 62 TEST(QuicArenaScopedPtrTest, FromArena) { |
| 63 FLAGS_quic_enable_arena_allocation = true; |
| 64 QuicOneBlockArena<1024> arena_; |
| 65 EXPECT_TRUE(arena_.New<TestObject>(0).is_from_arena()); |
| 66 EXPECT_FALSE( |
| 67 QuicArenaScopedPtr<TestObject>(new TestObject(0)).is_from_arena()); |
| 68 } |
| 69 |
| 70 TEST_P(QuicArenaScopedPtrParamTest, Assign) { |
| 71 QuicArenaScopedPtr<TestObject> ptr = CreateObject(12345); |
| 72 ptr = CreateObject(54321); |
| 73 EXPECT_EQ(54321u, ptr->value); |
| 74 } |
| 75 |
| 76 TEST_P(QuicArenaScopedPtrParamTest, MoveConstruct) { |
| 77 QuicArenaScopedPtr<TestObject> ptr1 = CreateObject(12345); |
| 78 QuicArenaScopedPtr<TestObject> ptr2(std::move(ptr1)); |
| 79 EXPECT_EQ(nullptr, ptr1); |
| 80 EXPECT_EQ(12345u, ptr2->value); |
| 81 } |
| 82 |
| 83 TEST_P(QuicArenaScopedPtrParamTest, Accessors) { |
| 84 QuicArenaScopedPtr<TestObject> ptr = CreateObject(12345); |
| 85 EXPECT_EQ(12345u, (*ptr).value); |
| 86 EXPECT_EQ(12345u, ptr->value); |
| 87 // We explicitly want to test that get() returns a valid pointer to the data, |
| 88 // but the call looks redundant. |
| 89 EXPECT_EQ(12345u, ptr.get()->value); // NOLINT |
| 90 } |
| 91 |
| 92 TEST_P(QuicArenaScopedPtrParamTest, Reset) { |
| 93 QuicArenaScopedPtr<TestObject> ptr = CreateObject(12345); |
| 94 ptr.reset(new TestObject(54321)); |
| 95 EXPECT_EQ(54321u, ptr->value); |
| 96 } |
| 97 |
| 98 TEST_P(QuicArenaScopedPtrParamTest, Swap) { |
| 99 QuicArenaScopedPtr<TestObject> ptr1 = CreateObject(12345); |
| 100 QuicArenaScopedPtr<TestObject> ptr2 = CreateObject(54321); |
| 101 ptr1.swap(ptr2); |
| 102 EXPECT_EQ(12345u, ptr2->value); |
| 103 EXPECT_EQ(54321u, ptr1->value); |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 } // namespace net |
OLD | NEW |