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/tag.h"
19 #include "sfntly/data/growable_memory_byte_array.h"
20 #include "sfntly/data/writable_font_data.h"
21 #include "sfntly/math/fixed1616.h"
22 #include "sfntly/port/memory_output_stream.h"
23 #include "sfntly/data/font_output_stream.h"
24 
25 namespace sfntly {
26 
TestEndian()27 bool TestEndian() {
28   byte_t test_data[] = {
29       0x68, 0x65, 0x61, 0x64,  // 0: head
30       0xca, 0xca, 0xca, 0xca,  // 4: ubyte, byte, char
31       0x00, 0x18, 0x80, 0x18,  // 8: ushort, short
32       0x00, 0x00, 0x18, 0x00,  // 12: uint24
33       0x00, 0x00, 0x00, 0x18,  // 16: ulong
34       0xff, 0xff, 0xff, 0x00,  // 20: long
35       0x00, 0x01, 0x00, 0x00   // 24: fixed
36   };
37 
38   ByteArrayPtr ba1 = new GrowableMemoryByteArray();
39   for (size_t i = 0; i < sizeof(test_data); ++i) {
40     ba1->Put(i, test_data[i]);
41   }
42   ReadableFontDataPtr rfd = new ReadableFontData(ba1);
43   EXPECT_EQ(rfd->ReadULongAsInt(0), Tag::head);
44   EXPECT_EQ(rfd->ReadUByte(4), 202);
45   EXPECT_EQ(rfd->ReadByte(5), -54);
46   EXPECT_EQ(rfd->ReadChar(6), 202);
47   EXPECT_EQ(rfd->ReadUShort(8), 24);
48   EXPECT_EQ(rfd->ReadShort(10), -32744);
49   EXPECT_EQ(rfd->ReadUInt24(12), 24);
50   EXPECT_EQ(rfd->ReadULong(16), 24);
51   EXPECT_EQ(rfd->ReadLong(20), -256);
52   EXPECT_EQ(rfd->ReadFixed(24), Fixed1616::Fixed(1, 0));
53 
54   MemoryOutputStream os;
55   FontOutputStream fos(&os);
56   fos.WriteULong(Tag::head);
57   fos.Write(202);
58   fos.Write(202);
59   fos.Write(202);
60   fos.Write(202);
61   fos.WriteUShort(24);
62   fos.WriteShort(-32744);
63   fos.WriteUInt24(24);
64   fos.WriteChar(0);
65   fos.WriteULong(24);
66   fos.WriteLong(-256);
67   fos.WriteFixed(Fixed1616::Fixed(1, 0));
68   EXPECT_EQ(memcmp(os.Get(), test_data, sizeof(test_data)), 0);
69 
70   return true;
71 }
72 
73 }  // namespace sfntly
74 
TEST(Endian,All)75 TEST(Endian, All) {
76   ASSERT_TRUE(sfntly::TestEndian());
77 }
78