| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "platform/SharedBuffer.h" | 31 #include "platform/SharedBuffer.h" |
| 32 | 32 |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "wtf/OwnPtr.h" | 34 #include "wtf/PtrUtil.h" |
| 35 #include "wtf/PassOwnPtr.h" | |
| 36 #include "wtf/RefPtr.h" | 35 #include "wtf/RefPtr.h" |
| 37 #include "wtf/Vector.h" | 36 #include "wtf/Vector.h" |
| 38 #include <algorithm> | 37 #include <algorithm> |
| 39 #include <cstdlib> | 38 #include <cstdlib> |
| 39 #include <memory> |
| 40 | 40 |
| 41 namespace blink { | 41 namespace blink { |
| 42 | 42 |
| 43 TEST(SharedBufferTest, getAsBytes) | 43 TEST(SharedBufferTest, getAsBytes) |
| 44 { | 44 { |
| 45 char testData0[] = "Hello"; | 45 char testData0[] = "Hello"; |
| 46 char testData1[] = "World"; | 46 char testData1[] = "World"; |
| 47 char testData2[] = "Goodbye"; | 47 char testData2[] = "Goodbye"; |
| 48 | 48 |
| 49 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData0, strlen(t
estData0)); | 49 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData0, strlen(t
estData0)); |
| 50 sharedBuffer->append(testData1, strlen(testData1)); | 50 sharedBuffer->append(testData1, strlen(testData1)); |
| 51 sharedBuffer->append(testData2, strlen(testData2)); | 51 sharedBuffer->append(testData2, strlen(testData2)); |
| 52 | 52 |
| 53 const size_t size = sharedBuffer->size(); | 53 const size_t size = sharedBuffer->size(); |
| 54 OwnPtr<char[]> data = adoptArrayPtr(new char[size]); | 54 std::unique_ptr<char[]> data = wrapArrayUnique(new char[size]); |
| 55 ASSERT_TRUE(sharedBuffer->getAsBytes(data.get(), size)); | 55 ASSERT_TRUE(sharedBuffer->getAsBytes(data.get(), size)); |
| 56 | 56 |
| 57 char expectedConcatenation[] = "HelloWorldGoodbye"; | 57 char expectedConcatenation[] = "HelloWorldGoodbye"; |
| 58 ASSERT_EQ(strlen(expectedConcatenation), size); | 58 ASSERT_EQ(strlen(expectedConcatenation), size); |
| 59 EXPECT_EQ(0, memcmp(expectedConcatenation, data.get(), strlen(expectedConcat
enation))); | 59 EXPECT_EQ(0, memcmp(expectedConcatenation, data.get(), strlen(expectedConcat
enation))); |
| 60 } | 60 } |
| 61 | 61 |
| 62 TEST(SharedBufferTest, getAsBytesLargeSegments) | 62 TEST(SharedBufferTest, getAsBytesLargeSegments) |
| 63 { | 63 { |
| 64 Vector<char> vector0(0x4000); | 64 Vector<char> vector0(0x4000); |
| 65 for (size_t i = 0; i < vector0.size(); ++i) | 65 for (size_t i = 0; i < vector0.size(); ++i) |
| 66 vector0[i] = 'a'; | 66 vector0[i] = 'a'; |
| 67 Vector<char> vector1(0x4000); | 67 Vector<char> vector1(0x4000); |
| 68 for (size_t i = 0; i < vector1.size(); ++i) | 68 for (size_t i = 0; i < vector1.size(); ++i) |
| 69 vector1[i] = 'b'; | 69 vector1[i] = 'b'; |
| 70 Vector<char> vector2(0x4000); | 70 Vector<char> vector2(0x4000); |
| 71 for (size_t i = 0; i < vector2.size(); ++i) | 71 for (size_t i = 0; i < vector2.size(); ++i) |
| 72 vector2[i] = 'c'; | 72 vector2[i] = 'c'; |
| 73 | 73 |
| 74 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::adoptVector(vector0); | 74 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::adoptVector(vector0); |
| 75 sharedBuffer->append(vector1); | 75 sharedBuffer->append(vector1); |
| 76 sharedBuffer->append(vector2); | 76 sharedBuffer->append(vector2); |
| 77 | 77 |
| 78 const size_t size = sharedBuffer->size(); | 78 const size_t size = sharedBuffer->size(); |
| 79 OwnPtr<char[]> data = adoptArrayPtr(new char[size]); | 79 std::unique_ptr<char[]> data = wrapArrayUnique(new char[size]); |
| 80 ASSERT_TRUE(sharedBuffer->getAsBytes(data.get(), size)); | 80 ASSERT_TRUE(sharedBuffer->getAsBytes(data.get(), size)); |
| 81 | 81 |
| 82 ASSERT_EQ(0x4000U + 0x4000U + 0x4000U, size); | 82 ASSERT_EQ(0x4000U + 0x4000U + 0x4000U, size); |
| 83 int position = 0; | 83 int position = 0; |
| 84 for (int i = 0; i < 0x4000; ++i) { | 84 for (int i = 0; i < 0x4000; ++i) { |
| 85 EXPECT_EQ('a', data[position]); | 85 EXPECT_EQ('a', data[position]); |
| 86 ++position; | 86 ++position; |
| 87 } | 87 } |
| 88 for (int i = 0; i < 0x4000; ++i) { | 88 for (int i = 0; i < 0x4000; ++i) { |
| 89 EXPECT_EQ('b', data[position]); | 89 EXPECT_EQ('b', data[position]); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 142 |
| 143 // Do another append + merge the segments again. | 143 // Do another append + merge the segments again. |
| 144 size_t previousTestDataSize = testData.size(); | 144 size_t previousTestDataSize = testData.size(); |
| 145 testData.resize(2 * previousTestDataSize); | 145 testData.resize(2 * previousTestDataSize); |
| 146 std::generate(testData.begin() + previousTestDataSize, testData.end(), &std:
:rand); | 146 std::generate(testData.begin() + previousTestDataSize, testData.end(), &std:
:rand); |
| 147 sharedBuffer->append(testData.data() + previousTestDataSize, previousTestDat
aSize); | 147 sharedBuffer->append(testData.data() + previousTestDataSize, previousTestDat
aSize); |
| 148 ASSERT_EQ(0, memcmp(data, testData.data(), length)); | 148 ASSERT_EQ(0, memcmp(data, testData.data(), length)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 } // namespace blink | 151 } // namespace blink |
| OLD | NEW |