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 "gtest/gtest.h"
     18 #include "sfntly/data/writable_font_data.h"
     19 #include "sfntly/data/memory_byte_array.h"
     20 
     21 namespace sfntly {
     22 
     23 const byte_t TEST_OTF_DATA[] =
     24     {0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
     25 
     26 bool TestOTFRead() {
     27   ByteVector bytes;
     28   for (size_t i = 0; i < sizeof(TEST_OTF_DATA) / sizeof(byte_t); ++i) {
     29     bytes.push_back(TEST_OTF_DATA[i]);
     30   }
     31   ByteArrayPtr array = new MemoryByteArray(&(bytes[0]), bytes.size());
     32   ReadableFontDataPtr data = new ReadableFontData(array);
     33 
     34   EXPECT_EQ(-1, data->ReadByte(0));
     35   EXPECT_EQ(0xff, data->ReadUByte(0));
     36   EXPECT_EQ(0x01, data->ReadByte(1));
     37   EXPECT_EQ(65281, data->ReadUShort(0));
     38   EXPECT_EQ(-255, data->ReadShort(0));
     39   EXPECT_EQ(16711937, data->ReadUInt24(0));
     40   EXPECT_EQ(4278255873LL, data->ReadULong(0));
     41   EXPECT_EQ(-16711423, data->ReadLong(0));
     42   return true;
     43 }
     44 
     45 bool TestOTFCopy() {
     46   ByteVector source_bytes(1024);
     47   for (size_t i = 0; i < source_bytes.size(); ++i) {
     48     source_bytes[i] = (byte_t)(i & 0xff);
     49   }
     50   ByteArrayPtr source_array = new MemoryByteArray(&(source_bytes[0]), 1024);
     51   ReadableFontDataPtr source = new ReadableFontData(source_array);
     52 
     53   ByteVector destination_bytes(1024);
     54   ByteArrayPtr destination_array =
     55       new MemoryByteArray(&(destination_bytes[0]), 1024);
     56   WritableFontDataPtr destination = new WritableFontData(destination_array);
     57 
     58   int32_t length = source->CopyTo(destination);
     59   EXPECT_EQ(1024, length);
     60   EXPECT_TRUE(std::equal(source_bytes.begin(), source_bytes.end(),
     61                          destination_bytes.begin()));
     62   return true;
     63 }
     64 
     65 }  // namespace sfntly
     66 
     67 TEST(OpenTypeData, All) {
     68   ASSERT_TRUE(sfntly::TestOTFRead());
     69   ASSERT_TRUE(sfntly::TestOTFCopy());
     70 }
     71