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 #include "net/quic/quic_arena_scoped_ptr.h" | 4 #include "net/quic/quic_arena_scoped_ptr.h" |
5 | 5 |
6 #include "net/quic/quic_one_block_arena.h" | 6 #include "net/quic/quic_one_block_arena.h" |
7 #include "testing/gmock/include/gmock/gmock.h" | 7 #include "testing/gmock/include/gmock/gmock.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 // #include "testing/base/public/gunit.h" | 9 // #include "testing/base/public/gunit.h" |
10 | 10 |
11 namespace net { | 11 namespace net { |
12 namespace { | 12 namespace { |
13 | 13 |
14 enum class TestParam { kFromHeap, kFromArena }; | 14 enum class TestParam { kFromHeap, kFromArena }; |
15 | 15 |
16 struct TestObject { | 16 struct TestObject { |
17 explicit TestObject(uintptr_t value) : value(value) { buffer.resize(1024); } | 17 explicit TestObject(uintptr_t value) : value(value) { buffer.resize(1024); } |
18 uintptr_t value; | 18 uintptr_t value; |
19 | 19 |
20 // Ensure that we have a non-trivial destructor that will leak memory if it's | 20 // Ensure that we have a non-trivial destructor that will leak memory if it's |
21 // not called. | 21 // not called. |
22 std::vector<char> buffer; | 22 std::vector<char> buffer; |
23 }; | 23 }; |
24 | 24 |
25 class QuicArenaScopedPtrParamTest : public ::testing::TestWithParam<TestParam> { | 25 class QuicArenaScopedPtrParamTest : public ::testing::TestWithParam<TestParam> { |
26 protected: | 26 protected: |
27 QuicArenaScopedPtrParamTest() { FLAGS_quic_enable_arena_allocation = true; } | |
28 | |
29 QuicArenaScopedPtr<TestObject> CreateObject(uintptr_t value) { | 27 QuicArenaScopedPtr<TestObject> CreateObject(uintptr_t value) { |
30 QuicArenaScopedPtr<TestObject> ptr; | 28 QuicArenaScopedPtr<TestObject> ptr; |
31 switch (GetParam()) { | 29 switch (GetParam()) { |
32 case TestParam::kFromHeap: | 30 case TestParam::kFromHeap: |
33 ptr = QuicArenaScopedPtr<TestObject>(new TestObject(value)); | 31 ptr = QuicArenaScopedPtr<TestObject>(new TestObject(value)); |
34 CHECK(!ptr.is_from_arena()); | 32 CHECK(!ptr.is_from_arena()); |
35 break; | 33 break; |
36 case TestParam::kFromArena: | 34 case TestParam::kFromArena: |
37 ptr = arena_.New<TestObject>(value); | 35 ptr = arena_.New<TestObject>(value); |
38 CHECK(ptr.is_from_arena()); | 36 CHECK(ptr.is_from_arena()); |
39 break; | 37 break; |
40 } | 38 } |
41 return ptr; | 39 return ptr; |
42 } | 40 } |
43 | 41 |
44 private: | 42 private: |
45 QuicOneBlockArena<1024> arena_; | 43 QuicOneBlockArena<1024> arena_; |
46 }; | 44 }; |
47 | 45 |
48 INSTANTIATE_TEST_CASE_P(QuicArenaScopedPtrParamTest, | 46 INSTANTIATE_TEST_CASE_P(QuicArenaScopedPtrParamTest, |
49 QuicArenaScopedPtrParamTest, | 47 QuicArenaScopedPtrParamTest, |
50 testing::Values(TestParam::kFromHeap, | 48 testing::Values(TestParam::kFromHeap, |
51 TestParam::kFromArena)); | 49 TestParam::kFromArena)); |
52 | 50 |
53 TEST(QuicArenaScopedPtrTest, NullObjects) { | 51 TEST(QuicArenaScopedPtrTest, NullObjects) { |
54 FLAGS_quic_enable_arena_allocation = true; | |
55 QuicArenaScopedPtr<TestObject> def; | 52 QuicArenaScopedPtr<TestObject> def; |
56 QuicArenaScopedPtr<TestObject> null(nullptr); | 53 QuicArenaScopedPtr<TestObject> null(nullptr); |
57 EXPECT_EQ(def, null); | 54 EXPECT_EQ(def, null); |
58 EXPECT_EQ(def, nullptr); | 55 EXPECT_EQ(def, nullptr); |
59 EXPECT_EQ(null, nullptr); | 56 EXPECT_EQ(null, nullptr); |
60 } | 57 } |
61 | 58 |
62 TEST(QuicArenaScopedPtrTest, FromArena) { | 59 TEST(QuicArenaScopedPtrTest, FromArena) { |
63 FLAGS_quic_enable_arena_allocation = true; | |
64 QuicOneBlockArena<1024> arena_; | 60 QuicOneBlockArena<1024> arena_; |
65 EXPECT_TRUE(arena_.New<TestObject>(0).is_from_arena()); | 61 EXPECT_TRUE(arena_.New<TestObject>(0).is_from_arena()); |
66 EXPECT_FALSE( | 62 EXPECT_FALSE( |
67 QuicArenaScopedPtr<TestObject>(new TestObject(0)).is_from_arena()); | 63 QuicArenaScopedPtr<TestObject>(new TestObject(0)).is_from_arena()); |
68 } | 64 } |
69 | 65 |
70 TEST_P(QuicArenaScopedPtrParamTest, Assign) { | 66 TEST_P(QuicArenaScopedPtrParamTest, Assign) { |
71 QuicArenaScopedPtr<TestObject> ptr = CreateObject(12345); | 67 QuicArenaScopedPtr<TestObject> ptr = CreateObject(12345); |
72 ptr = CreateObject(54321); | 68 ptr = CreateObject(54321); |
73 EXPECT_EQ(54321u, ptr->value); | 69 EXPECT_EQ(54321u, ptr->value); |
(...skipping 24 matching lines...) Expand all Loading... |
98 TEST_P(QuicArenaScopedPtrParamTest, Swap) { | 94 TEST_P(QuicArenaScopedPtrParamTest, Swap) { |
99 QuicArenaScopedPtr<TestObject> ptr1 = CreateObject(12345); | 95 QuicArenaScopedPtr<TestObject> ptr1 = CreateObject(12345); |
100 QuicArenaScopedPtr<TestObject> ptr2 = CreateObject(54321); | 96 QuicArenaScopedPtr<TestObject> ptr2 = CreateObject(54321); |
101 ptr1.swap(ptr2); | 97 ptr1.swap(ptr2); |
102 EXPECT_EQ(12345u, ptr2->value); | 98 EXPECT_EQ(12345u, ptr2->value); |
103 EXPECT_EQ(54321u, ptr1->value); | 99 EXPECT_EQ(54321u, ptr1->value); |
104 } | 100 } |
105 | 101 |
106 } // namespace | 102 } // namespace |
107 } // namespace net | 103 } // namespace net |
OLD | NEW |