• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/port/file_input_stream.h"
21 #include "sfntly/data/font_input_stream.h"
22 #include "test/test_data.h"
23 
24 namespace sfntly {
25 
TestFileInputStream()26 bool TestFileInputStream() {
27   FILE* file_handle = NULL;
28 #if defined (WIN32)
29   fopen_s(&file_handle, SAMPLE_TTF_FILE, "rb");
30 #else
31   file_handle = fopen(SAMPLE_TTF_FILE, "rb");
32 #endif
33   if (file_handle == NULL) {
34     return false;
35   }
36   fseek(file_handle, 0, SEEK_END);
37   size_t length = ftell(file_handle);
38   fseek(file_handle, 0, SEEK_SET);
39   ByteVector b1;
40   b1.resize(length);
41   size_t bytes_read = fread(&(b1[0]), 1, length, file_handle);
42   EXPECT_EQ(bytes_read, length);
43   fclose(file_handle);
44 
45   // Full file reading test
46   FileInputStream is;
47   is.Open(SAMPLE_TTF_FILE);
48   EXPECT_EQ(length, (size_t)is.Available());
49   ByteVector b2;
50   is.Read(&b2, 0, length);
51   is.Close();
52   EXPECT_EQ(memcmp(&(b1[0]), &(b2[0]), length), 0);
53   b2.clear();
54 
55   // Partial reading test
56   is.Open(SAMPLE_TTF_FILE);
57   is.Skip(89);
58   is.Read(&b2, 0, 100);
59   EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 100), 0);
60   b2.clear();
61 
62   // Skip test
63   is.Skip(-89);
64   is.Read(&b2, 0, 100);
65   EXPECT_EQ(memcmp(&(b1[100]), &(b2[0]), 100), 0);
66   b2.clear();
67   is.Skip(100);
68   is.Read(&b2, 0, 100);
69   EXPECT_EQ(memcmp(&(b1[300]), &(b2[0]), 100), 0);
70   is.Skip(-400);
71   b2.clear();
72 
73   // Offset test
74   is.Read(&b2, 0, 100);
75   is.Read(&b2, 100, 100);
76   EXPECT_EQ(memcmp(&(b1[0]), &(b2[0]), 200), 0);
77 
78   // Unread test
79   ByteVector b3;
80   b3.resize(200);
81   is.Unread(&b3);
82   EXPECT_EQ(memcmp(&(b3[0]), &(b2[0]), 200), 0);
83 
84   return true;
85 }
86 
TestFontInputStreamBasic()87 bool TestFontInputStreamBasic() {
88   FILE* file_handle = NULL;
89 #if defined (WIN32)
90   fopen_s(&file_handle, SAMPLE_TTF_FILE, "rb");
91 #else
92   file_handle = fopen(SAMPLE_TTF_FILE, "rb");
93 #endif
94   if (file_handle == NULL) {
95     return false;
96   }
97   fseek(file_handle, 0, SEEK_END);
98   size_t length = ftell(file_handle);
99   fseek(file_handle, 0, SEEK_SET);
100   ByteVector b1;
101   b1.resize(length);
102   size_t bytes_read = fread(&(b1[0]), 1, length, file_handle);
103   EXPECT_EQ(bytes_read, length);
104   fclose(file_handle);
105 
106   FileInputStream is;
107   is.Open(SAMPLE_TTF_FILE);
108   FontInputStream font_is1(&is);
109   EXPECT_EQ((size_t)font_is1.Available(), length);
110 
111   ByteVector b2;
112   font_is1.Read(&b2, 0, length);
113   font_is1.Close();
114   EXPECT_EQ(memcmp(&(b1[0]), &(b2[0]), length), 0);
115   b2.clear();
116 
117   is.Open(SAMPLE_TTF_FILE);
118   is.Skip(89);
119   FontInputStream font_is2(&is, 200);
120   font_is2.Read(&b2, 0, 100);
121   EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 100), 0);
122   font_is2.Read(&b2, 100, 100);
123   EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 200), 0);
124   b2.clear();
125   font_is2.Skip(-200);
126   font_is2.Read(&b2, 0, 100);
127   EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 100), 0);
128 
129   return true;
130 }
131 
TestFontInputStreamTableLoading()132 bool TestFontInputStreamTableLoading() {
133   FileInputStream is;
134   is.Open(SAMPLE_TTF_FILE);
135   FontInputStream font_is(&is);
136 
137   font_is.Skip(TTF_OFFSET[SAMPLE_TTF_FEAT]);
138   FontInputStream gdef_is(&font_is, TTF_LENGTH[SAMPLE_TTF_FEAT]);
139   ByteVector feat_data;
140   gdef_is.Read(&feat_data, 0, TTF_LENGTH[SAMPLE_TTF_FEAT]);
141   EXPECT_EQ(memcmp(&(feat_data[0]), TTF_FEAT_DATA,
142                    TTF_LENGTH[SAMPLE_TTF_FEAT]), 0);
143 
144   return true;
145 }
146 
147 }  // namespace sfntly
148 
TEST(FileIO,All)149 TEST(FileIO, All) {
150   ASSERT_TRUE(sfntly::TestFileInputStream());
151   ASSERT_TRUE(sfntly::TestFontInputStreamBasic());
152   ASSERT_TRUE(sfntly::TestFontInputStreamTableLoading());
153 }
154