OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/iovector.h" | 5 #include "net/quic/iovector.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 using std::string; | 12 using std::string; |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 namespace test { | 15 namespace test { |
16 namespace { | 16 namespace { |
17 | 17 |
18 const char* const test_data[] = { | 18 const char* const test_data[] = { |
19 "test string 1, a medium size one.", | 19 "test string 1, a medium size one.", "test string2", |
20 "test string2", | 20 "test string 3, a looooooooooooong loooooooooooooooong string"}; |
21 "test string 3, a looooooooooooong loooooooooooooooong string" | |
22 }; | |
23 | 21 |
24 TEST(IOVectorTest, CopyConstructor) { | 22 TEST(IOVectorTest, CopyConstructor) { |
25 IOVector iov1; | 23 IOVector iov1; |
26 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 24 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
27 iov1.Append(const_cast<char*>(test_data[i]), strlen(test_data[i])); | 25 iov1.Append(const_cast<char*>(test_data[i]), strlen(test_data[i])); |
28 } | 26 } |
29 IOVector iov2 = iov1; | 27 IOVector iov2 = iov1; |
30 EXPECT_EQ(iov2.Size(), iov1.Size()); | 28 EXPECT_EQ(iov2.Size(), iov1.Size()); |
31 for (size_t i = 0; i < iov2.Size(); ++i) { | 29 for (size_t i = 0; i < iov2.Size(); ++i) { |
32 EXPECT_TRUE(iov2.iovec()[i].iov_base == iov1.iovec()[i].iov_base); | 30 EXPECT_TRUE(iov2.iovec()[i].iov_base == iov1.iovec()[i].iov_base); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 iov2 = iov.iovec(); | 77 iov2 = iov.iovec(); |
80 ASSERT_TRUE(iov2 != NULL); | 78 ASSERT_TRUE(iov2 != NULL); |
81 for (size_t i = 0; i < iov.Size(); ++i) { | 79 for (size_t i = 0; i < iov.Size(); ++i) { |
82 ASSERT_TRUE(test_data[i] == iov2[i].iov_base); | 80 ASSERT_TRUE(test_data[i] == iov2[i].iov_base); |
83 ASSERT_EQ(strlen(test_data[i]), iov2[i].iov_len); | 81 ASSERT_EQ(strlen(test_data[i]), iov2[i].iov_len); |
84 } | 82 } |
85 } | 83 } |
86 | 84 |
87 TEST(IOVectorTest, AppendIovec) { | 85 TEST(IOVectorTest, AppendIovec) { |
88 IOVector iov; | 86 IOVector iov; |
89 const struct iovec test_iov[] = { | 87 const struct iovec test_iov[] = {{const_cast<char*>("foo"), 3}, |
90 {const_cast<char*>("foo"), 3}, | 88 {const_cast<char*>("bar"), 3}, |
91 {const_cast<char*>("bar"), 3}, | 89 {const_cast<char*>("buzzzz"), 6}}; |
92 {const_cast<char*>("buzzzz"), 6} | |
93 }; | |
94 iov.AppendIovec(test_iov, ARRAYSIZE_UNSAFE(test_iov)); | 90 iov.AppendIovec(test_iov, ARRAYSIZE_UNSAFE(test_iov)); |
95 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_iov); ++i) { | 91 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_iov); ++i) { |
96 EXPECT_EQ(test_iov[i].iov_base, iov.iovec()[i].iov_base); | 92 EXPECT_EQ(test_iov[i].iov_base, iov.iovec()[i].iov_base); |
97 EXPECT_EQ(test_iov[i].iov_len, iov.iovec()[i].iov_len); | 93 EXPECT_EQ(test_iov[i].iov_len, iov.iovec()[i].iov_len); |
98 } | 94 } |
99 | 95 |
100 // Test AppendIovecAtMostBytes. | 96 // Test AppendIovecAtMostBytes. |
101 iov.Clear(); | 97 iov.Clear(); |
102 // Stop in the middle of a block. | 98 // Stop in the middle of a block. |
103 EXPECT_EQ(5u, iov.AppendIovecAtMostBytes(test_iov, ARRAYSIZE_UNSAFE(test_iov), | 99 EXPECT_EQ( |
104 5)); | 100 5u, iov.AppendIovecAtMostBytes(test_iov, ARRAYSIZE_UNSAFE(test_iov), 5)); |
105 EXPECT_EQ(5u, iov.TotalBufferSize()); | 101 EXPECT_EQ(5u, iov.TotalBufferSize()); |
106 iov.Append(static_cast<char*>(test_iov[1].iov_base) + 2, 1); | 102 iov.Append(static_cast<char*>(test_iov[1].iov_base) + 2, 1); |
107 // Make sure the boundary case, where max_bytes == size of block also works. | 103 // Make sure the boundary case, where max_bytes == size of block also works. |
108 EXPECT_EQ(6u, iov.AppendIovecAtMostBytes(&test_iov[2], 1, 6)); | 104 EXPECT_EQ(6u, iov.AppendIovecAtMostBytes(&test_iov[2], 1, 6)); |
109 ASSERT_LE(ARRAYSIZE_UNSAFE(test_iov), static_cast<size_t>(iov.Size())); | 105 ASSERT_LE(ARRAYSIZE_UNSAFE(test_iov), static_cast<size_t>(iov.Size())); |
110 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_iov); ++i) { | 106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_iov); ++i) { |
111 EXPECT_EQ(test_iov[i].iov_base, iov.iovec()[i].iov_base); | 107 EXPECT_EQ(test_iov[i].iov_base, iov.iovec()[i].iov_base); |
112 EXPECT_EQ(test_iov[i].iov_len, iov.iovec()[i].iov_len); | 108 EXPECT_EQ(test_iov[i].iov_len, iov.iovec()[i].iov_len); |
113 } | 109 } |
114 } | 110 } |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 ASSERT_EQ(2u, iov2.Size()); | 270 ASSERT_EQ(2u, iov2.Size()); |
275 EXPECT_EQ(&a[0], iov2.iovec()[0].iov_base); | 271 EXPECT_EQ(&a[0], iov2.iovec()[0].iov_base); |
276 EXPECT_EQ(1u, iov2.iovec()[0].iov_len); | 272 EXPECT_EQ(1u, iov2.iovec()[0].iov_len); |
277 EXPECT_EQ(&b[0], iov2.iovec()[1].iov_base); | 273 EXPECT_EQ(&b[0], iov2.iovec()[1].iov_base); |
278 EXPECT_EQ(1u, iov2.iovec()[1].iov_len); | 274 EXPECT_EQ(1u, iov2.iovec()[1].iov_len); |
279 } | 275 } |
280 | 276 |
281 } // namespace | 277 } // namespace |
282 } // namespace test | 278 } // namespace test |
283 } // namespace net | 279 } // namespace net |
OLD | NEW |