Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright 2011 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdio.h>
     18 
     19 #include "gtest/gtest.h"
     20 #include "sfntly/data/memory_byte_array.h"
     21 #include "sfntly/data/growable_memory_byte_array.h"
     22 
     23 namespace sfntly {
     24 namespace byte_array_test {
     25 
     26 const int32_t BYTE_ARRAY_SIZES[] =
     27     {1, 7, 127, 128, 129, 255, 256, 257, 666, 1023, 10000, 0xffff, 0x10000};
     28 
     29 void FillTestByteArray(ByteArray* ba, int32_t size) {
     30   for (int32_t i = 0; i < size; ++i) {
     31     ba->Put(i, (byte_t)(i % 256));
     32   }
     33 }
     34 
     35 void ReadByteArrayWithBuffer(ByteArray* ba, ByteVector* buffer, ByteVector* b) {
     36   b->resize(ba->Length());
     37   int32_t index = 0;
     38   while (index < ba->Length()) {
     39     int32_t bytes_read = ba->Get(index, buffer);
     40     std::copy(buffer->begin(), buffer->begin() + bytes_read,
     41               b->begin() + index);
     42     index += bytes_read;
     43   }
     44 }
     45 
     46 void ReadByteArrayWithSlidingWindow(ByteArray* ba, int window_size,
     47                                     ByteVector* b) {
     48   b->resize(ba->Length());
     49   int32_t index = 0;
     50   int32_t actual_window_size = window_size;
     51   while (index < ba->Length()) {
     52     actual_window_size =
     53         std::min<int32_t>(actual_window_size, b->size() - index);
     54     int32_t bytes_read = ba->Get(index, &((*b)[0]), index, actual_window_size);
     55     index += bytes_read;
     56   }
     57 }
     58 
     59 bool ReadComparison(ByteArray* ba1, ByteArray* ba2) {
     60   // single byte reads
     61   for (int i = 0; i < ba1->Length(); ++i) {
     62     EXPECT_EQ(ba1->Get(i), ba2->Get(i));
     63   }
     64 
     65   ByteVector b1, b2;
     66   // buffer reads
     67   int increments = std::max<int32_t>(ba1->Length() / 11, 1);
     68   for (int buffer_size = 1; buffer_size < ba1->Length();
     69        buffer_size += increments) {
     70     ByteVector buffer(buffer_size);
     71     ReadByteArrayWithBuffer(ba1, &buffer, &b1);
     72     ReadByteArrayWithBuffer(ba2, &buffer, &b2);
     73     EXPECT_GT(b1.size(), static_cast<size_t>(0));
     74     EXPECT_EQ(b1.size(), b2.size());
     75     EXPECT_TRUE(std::equal(b1.begin(), b1.end(), b2.begin()));
     76   }
     77 
     78   // sliding window reads
     79   b1.clear();
     80   b2.clear();
     81   for (int window_size = 1; window_size < ba1->Length();
     82        window_size += increments) {
     83     ReadByteArrayWithSlidingWindow(ba1, window_size, &b1);
     84     ReadByteArrayWithSlidingWindow(ba2, window_size, &b2);
     85     EXPECT_GT(b1.size(), static_cast<size_t>(0));
     86     EXPECT_EQ(b1.size(), b2.size());
     87     EXPECT_TRUE(std::equal(b1.begin(), b1.end(), b2.begin()));
     88   }
     89 
     90   return true;
     91 }
     92 
     93 bool CopyTest(ByteArray* ba) {
     94   ByteArrayPtr fixed_copy = new MemoryByteArray(ba->Length());
     95   ba->CopyTo(fixed_copy);
     96   EXPECT_EQ(ba->Length(), fixed_copy->Length());
     97   EXPECT_TRUE(ReadComparison(ba, fixed_copy));
     98 
     99   ByteArrayPtr growable_copy = new GrowableMemoryByteArray();
    100   ba->CopyTo(growable_copy);
    101   EXPECT_EQ(ba->Length(), growable_copy->Length());
    102   EXPECT_TRUE(ReadComparison(ba, growable_copy));
    103 
    104   return true;
    105 }
    106 
    107 bool ByteArrayTester(ByteArray* ba) {
    108   return CopyTest(ba);
    109 }
    110 
    111 }  // namespace byte_array_test
    112 
    113 bool TestMemoryByteArray() {
    114   fprintf(stderr, "fixed mem: size ");
    115   for (size_t i = 0;
    116        i < sizeof(byte_array_test::BYTE_ARRAY_SIZES) / sizeof(int32_t); ++i) {
    117     int32_t size = byte_array_test::BYTE_ARRAY_SIZES[i];
    118     fprintf(stderr, "%d ", size);
    119     ByteArrayPtr ba = new MemoryByteArray(size);
    120     byte_array_test::FillTestByteArray(ba, size);
    121     EXPECT_TRUE(byte_array_test::ByteArrayTester(ba));
    122   }
    123   fprintf(stderr, "\n");
    124   return true;
    125 }
    126 
    127 bool TestGrowableMemoryByteArray() {
    128   fprintf(stderr, "growable mem: size ");
    129   for (size_t i = 0;
    130        i < sizeof(byte_array_test::BYTE_ARRAY_SIZES) / sizeof(int32_t); ++i) {
    131     int32_t size = byte_array_test::BYTE_ARRAY_SIZES[i];
    132     fprintf(stderr, "%d ", size);
    133     ByteArrayPtr ba = new GrowableMemoryByteArray();
    134     byte_array_test::FillTestByteArray(ba, size);
    135     EXPECT_TRUE(byte_array_test::ByteArrayTester(ba));
    136   }
    137   fprintf(stderr, "\n");
    138   return true;
    139 }
    140 
    141 }  // namespace sfntly
    142 
    143 TEST(ByteArray, All) {
    144   ASSERT_TRUE(sfntly::TestMemoryByteArray());
    145   ASSERT_TRUE(sfntly::TestGrowableMemoryByteArray());
    146 }
    147