Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / SharedBufferTest.cpp
blob80fdc9b9e32d8aefb9cfafb347422721a33b1f08
1 /*
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
6 * met:
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
13 * distribution.
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.
31 #include "config.h"
32 #include "platform/SharedBuffer.h"
34 #include "platform/TestingPlatformSupport.h"
35 #include "public/platform/WebDiscardableMemory.h"
36 #include "wtf/RefPtr.h"
37 #include "wtf/Vector.h"
38 #include <algorithm>
39 #include <cstdlib>
40 #include <gtest/gtest.h>
42 namespace blink {
44 TEST(SharedBufferTest, getAsBytes)
46 char testData0[] = "Hello";
47 char testData1[] = "World";
48 char testData2[] = "Goodbye";
50 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData0, strlen(testData0));
51 sharedBuffer->append(testData1, strlen(testData1));
52 sharedBuffer->append(testData2, strlen(testData2));
54 const unsigned size = sharedBuffer->size();
55 OwnPtr<char[]> data = adoptArrayPtr(new char[size]);
56 ASSERT_TRUE(sharedBuffer->getAsBytes(data.get(), size));
58 char expectedConcatenation[] = "HelloWorldGoodbye";
59 ASSERT_EQ(strlen(expectedConcatenation), size);
60 EXPECT_EQ(0, memcmp(expectedConcatenation, data.get(), strlen(expectedConcatenation)));
63 TEST(SharedBufferTest, getAsBytesLargeSegments)
65 Vector<char> vector0(0x4000);
66 for (size_t i = 0; i < vector0.size(); ++i)
67 vector0[i] = 'a';
68 Vector<char> vector1(0x4000);
69 for (size_t i = 0; i < vector1.size(); ++i)
70 vector1[i] = 'b';
71 Vector<char> vector2(0x4000);
72 for (size_t i = 0; i < vector2.size(); ++i)
73 vector2[i] = 'c';
75 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::adoptVector(vector0);
76 sharedBuffer->append(vector1);
77 sharedBuffer->append(vector2);
79 const unsigned size = sharedBuffer->size();
80 OwnPtr<char[]> data = adoptArrayPtr(new char[size]);
81 ASSERT_TRUE(sharedBuffer->getAsBytes(data.get(), size));
83 ASSERT_EQ(0x4000U + 0x4000U + 0x4000U, size);
84 int position = 0;
85 for (int i = 0; i < 0x4000; ++i) {
86 EXPECT_EQ('a', data[position]);
87 ++position;
89 for (int i = 0; i < 0x4000; ++i) {
90 EXPECT_EQ('b', data[position]);
91 ++position;
93 for (int i = 0; i < 0x4000; ++i) {
94 EXPECT_EQ('c', data[position]);
95 ++position;
99 TEST(SharedBufferTest, copy)
101 Vector<char> testData(10000);
102 std::generate(testData.begin(), testData.end(), &std::rand);
104 unsigned length = testData.size();
105 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData.data(), length);
106 sharedBuffer->append(testData.data(), length);
107 sharedBuffer->append(testData.data(), length);
108 sharedBuffer->append(testData.data(), length);
109 // sharedBuffer must contain data more than segmentSize (= 0x1000) to check copy().
110 ASSERT_EQ(length * 4, sharedBuffer->size());
112 RefPtr<SharedBuffer> clone = sharedBuffer->copy();
113 ASSERT_EQ(length * 4, clone->size());
114 ASSERT_EQ(0, memcmp(clone->data(), sharedBuffer->data(), clone->size()));
116 clone->append(testData.data(), length);
117 ASSERT_EQ(length * 5, clone->size());
120 TEST(SharedBufferTest, constructorWithSizeOnly)
122 unsigned length = 10000;
123 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(length);
124 ASSERT_EQ(length, sharedBuffer->size());
126 // The internal flat buffer should have been resized to |length| therefore getSomeData() should
127 // directly return the full size.
128 const char* data;
129 ASSERT_EQ(length, sharedBuffer->getSomeData(data, 0));
132 TEST(SharedBufferTest, createPurgeable)
134 Vector<char> testData(30000);
135 std::generate(testData.begin(), testData.end(), &std::rand);
137 TestingPlatformSupport::Config config;
138 config.hasDiscardableMemorySupport = true;
139 TestingPlatformSupport platformWithDiscardableMemorySupport(config);
141 unsigned length = testData.size();
142 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::createPurgeable(testData.data(), length);
143 ASSERT_EQ(length, sharedBuffer->size());
144 // Merge the segments into a single vector.
145 const char* data = sharedBuffer->data();
146 ASSERT_EQ(0, memcmp(data, testData.data(), length));
148 // Do another append + merge the segments again.
149 size_t previousTestDataSize = testData.size();
150 testData.resize(2 * previousTestDataSize);
151 std::generate(testData.begin() + previousTestDataSize, testData.end(), &std::rand);
152 sharedBuffer->append(testData.data() + previousTestDataSize, previousTestDataSize);
153 ASSERT_EQ(0, memcmp(data, testData.data(), length));
156 } // namespace blink