2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
36 #include "platform/SharedBuffer.h"
37 #include "platform/TestingPlatformSupport.h"
38 #include "public/platform/WebDiscardableMemory.h"
40 #include "wtf/ArrayBuffer.h"
41 #include "wtf/RefPtr.h"
42 #include "wtf/Vector.h"
43 #include <gtest/gtest.h>
45 using namespace blink;
49 TEST(SharedBufferTest, getAsArrayBuffer)
51 char testData0[] = "Hello";
52 char testData1[] = "World";
53 char testData2[] = "Goodbye";
55 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData0, strlen(testData0));
56 sharedBuffer->append(testData1, strlen(testData1));
57 sharedBuffer->append(testData2, strlen(testData2));
59 RefPtr<ArrayBuffer> arrayBuffer = sharedBuffer->getAsArrayBuffer();
61 char expectedConcatenation[] = "HelloWorldGoodbye";
62 ASSERT_EQ(strlen(expectedConcatenation), arrayBuffer->byteLength());
63 EXPECT_EQ(0, memcmp(expectedConcatenation, arrayBuffer->data(), strlen(expectedConcatenation)));
66 TEST(SharedBufferTest, getAsArrayBufferLargeSegments)
68 Vector<char> vector0(0x4000);
69 for (size_t i = 0; i < vector0.size(); ++i)
71 Vector<char> vector1(0x4000);
72 for (size_t i = 0; i < vector1.size(); ++i)
74 Vector<char> vector2(0x4000);
75 for (size_t i = 0; i < vector2.size(); ++i)
78 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::adoptVector(vector0);
79 sharedBuffer->append(vector1);
80 sharedBuffer->append(vector2);
82 RefPtr<ArrayBuffer> arrayBuffer = sharedBuffer->getAsArrayBuffer();
84 ASSERT_EQ(0x4000U + 0x4000U + 0x4000U, arrayBuffer->byteLength());
86 for (int i = 0; i < 0x4000; ++i) {
87 EXPECT_EQ('a', static_cast<char*>(arrayBuffer->data())[position]);
90 for (int i = 0; i < 0x4000; ++i) {
91 EXPECT_EQ('b', static_cast<char*>(arrayBuffer->data())[position]);
94 for (int i = 0; i < 0x4000; ++i) {
95 EXPECT_EQ('c', static_cast<char*>(arrayBuffer->data())[position]);
100 TEST(SharedBufferTest, copy)
102 Vector<char> testData(10000);
103 std::generate(testData.begin(), testData.end(), &std::rand);
105 unsigned length = testData.size();
106 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData.data(), length);
107 sharedBuffer->append(testData.data(), length);
108 sharedBuffer->append(testData.data(), length);
109 sharedBuffer->append(testData.data(), length);
110 // sharedBuffer must contain data more than segmentSize (= 0x1000) to check copy().
111 ASSERT_EQ(length * 4, sharedBuffer->size());
113 RefPtr<SharedBuffer> clone = sharedBuffer->copy();
114 ASSERT_EQ(length * 4, clone->size());
115 ASSERT_EQ(0, memcmp(clone->data(), sharedBuffer->data(), clone->size()));
117 clone->append(testData.data(), length);
118 ASSERT_EQ(length * 5, clone->size());
121 TEST(SharedBufferTest, constructorWithSizeOnly)
123 unsigned length = 10000;
124 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(length);
125 ASSERT_EQ(length, sharedBuffer->size());
127 // The internal flat buffer should have been resized to |length| therefore getSomeData() should
128 // directly return the full size.
130 ASSERT_EQ(length, sharedBuffer->getSomeData(data, 0));
133 TEST(SharedBufferTest, createPurgeable)
135 Vector<char> testData(30000);
136 std::generate(testData.begin(), testData.end(), &std::rand);
138 TestingPlatformSupport::Config config;
139 config.hasDiscardableMemorySupport = true;
140 TestingPlatformSupport platformWithDiscardableMemorySupport(config);
142 unsigned length = testData.size();
143 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::createPurgeable(testData.data(), length);
144 ASSERT_EQ(length, sharedBuffer->size());
145 // Merge the segments into a single vector.
146 const char* data = sharedBuffer->data();
147 ASSERT_EQ(0, memcmp(data, testData.data(), length));
149 // Do another append + merge the segments again.
150 size_t previousTestDataSize = testData.size();
151 testData.resize(2 * previousTestDataSize);
152 std::generate(testData.begin() + previousTestDataSize, testData.end(), &std::rand);
153 sharedBuffer->append(testData.data() + previousTestDataSize, previousTestDataSize);
154 ASSERT_EQ(0, memcmp(data, testData.data(), length));