Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      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.
     17  *
     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.
     29  */
     30 
     31 #include "config.h"
     32 
     33 #include <algorithm>
     34 #include <cstdlib>
     35 
     36 #include "platform/SharedBuffer.h"
     37 #include "platform/TestingPlatformSupport.h"
     38 #include "public/platform/WebDiscardableMemory.h"
     39 
     40 #include "wtf/ArrayBuffer.h"
     41 #include "wtf/RefPtr.h"
     42 #include "wtf/Vector.h"
     43 #include <gtest/gtest.h>
     44 
     45 using namespace blink;
     46 
     47 namespace {
     48 
     49 TEST(SharedBufferTest, getAsArrayBuffer)
     50 {
     51     char testData0[] = "Hello";
     52     char testData1[] = "World";
     53     char testData2[] = "Goodbye";
     54 
     55     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData0, strlen(testData0));
     56     sharedBuffer->append(testData1, strlen(testData1));
     57     sharedBuffer->append(testData2, strlen(testData2));
     58 
     59     RefPtr<ArrayBuffer> arrayBuffer = sharedBuffer->getAsArrayBuffer();
     60 
     61     char expectedConcatenation[] = "HelloWorldGoodbye";
     62     ASSERT_EQ(strlen(expectedConcatenation), arrayBuffer->byteLength());
     63     EXPECT_EQ(0, memcmp(expectedConcatenation, arrayBuffer->data(), strlen(expectedConcatenation)));
     64 }
     65 
     66 TEST(SharedBufferTest, getAsArrayBufferLargeSegments)
     67 {
     68     Vector<char> vector0(0x4000);
     69     for (size_t i = 0; i < vector0.size(); ++i)
     70         vector0[i] = 'a';
     71     Vector<char> vector1(0x4000);
     72     for (size_t i = 0; i < vector1.size(); ++i)
     73         vector1[i] = 'b';
     74     Vector<char> vector2(0x4000);
     75     for (size_t i = 0; i < vector2.size(); ++i)
     76         vector2[i] = 'c';
     77 
     78     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::adoptVector(vector0);
     79     sharedBuffer->append(vector1);
     80     sharedBuffer->append(vector2);
     81 
     82     RefPtr<ArrayBuffer> arrayBuffer = sharedBuffer->getAsArrayBuffer();
     83 
     84     ASSERT_EQ(0x4000U + 0x4000U + 0x4000U, arrayBuffer->byteLength());
     85     int position = 0;
     86     for (int i = 0; i < 0x4000; ++i) {
     87         EXPECT_EQ('a', static_cast<char*>(arrayBuffer->data())[position]);
     88         ++position;
     89     }
     90     for (int i = 0; i < 0x4000; ++i) {
     91         EXPECT_EQ('b', static_cast<char*>(arrayBuffer->data())[position]);
     92         ++position;
     93     }
     94     for (int i = 0; i < 0x4000; ++i) {
     95         EXPECT_EQ('c', static_cast<char*>(arrayBuffer->data())[position]);
     96         ++position;
     97     }
     98 }
     99 
    100 TEST(SharedBufferTest, copy)
    101 {
    102     Vector<char> testData(10000);
    103     std::generate(testData.begin(), testData.end(), &std::rand);
    104 
    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());
    112 
    113     RefPtr<SharedBuffer> clone = sharedBuffer->copy();
    114     ASSERT_EQ(length * 4, clone->size());
    115     ASSERT_EQ(0, memcmp(clone->data(), sharedBuffer->data(), clone->size()));
    116 
    117     clone->append(testData.data(), length);
    118     ASSERT_EQ(length * 5, clone->size());
    119 }
    120 
    121 TEST(SharedBufferTest, constructorWithSizeOnly)
    122 {
    123     unsigned length = 10000;
    124     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(length);
    125     ASSERT_EQ(length, sharedBuffer->size());
    126 
    127     // The internal flat buffer should have been resized to |length| therefore getSomeData() should
    128     // directly return the full size.
    129     const char* data;
    130     ASSERT_EQ(length, sharedBuffer->getSomeData(data, 0));
    131 }
    132 
    133 TEST(SharedBufferTest, createPurgeable)
    134 {
    135     Vector<char> testData(30000);
    136     std::generate(testData.begin(), testData.end(), &std::rand);
    137 
    138     TestingPlatformSupport::Config config;
    139     config.hasDiscardableMemorySupport = true;
    140     TestingPlatformSupport platformWithDiscardableMemorySupport(config);
    141 
    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));
    148 
    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));
    155 }
    156 
    157 } // namespace
    158