12aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com/* 22aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * Copyright 2011 Google Inc. All Rights Reserved. 32aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * 42aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * Licensed under the Apache License, Version 2.0 (the "License"); 52aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * you may not use this file except in compliance with the License. 62aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * You may obtain a copy of the License at 72aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * 82aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * http://www.apache.org/licenses/LICENSE-2.0 92aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * 102aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * Unless required by applicable law or agreed to in writing, software 112aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * distributed under the License is distributed on an "AS IS" BASIS, 122aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * See the License for the specific language governing permissions and 142aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com * limitations under the License. 152aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com */ 162aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 172aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include <stdio.h> 182aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 192aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include "gtest/gtest.h" 202aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include "sfntly/port/file_input_stream.h" 212aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#include "sfntly/data/font_input_stream.h" 22ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com#include "test/test_data.h" 232aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 242aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.comnamespace sfntly { 252aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 26246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.combool TestFileInputStream() { 27ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com FILE* file_handle = NULL; 282aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#if defined (WIN32) 29ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com fopen_s(&file_handle, SAMPLE_TTF_FILE, "rb"); 302aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#else 31ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com file_handle = fopen(SAMPLE_TTF_FILE, "rb"); 322aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#endif 332aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com if (file_handle == NULL) { 342aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com return false; 352aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com } 362aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com fseek(file_handle, 0, SEEK_END); 372aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com size_t length = ftell(file_handle); 382aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com fseek(file_handle, 0, SEEK_SET); 392aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com ByteVector b1; 402aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b1.resize(length); 4135a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com size_t bytes_read = fread(&(b1[0]), 1, length, file_handle); 4235a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com EXPECT_EQ(bytes_read, length); 432aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com fclose(file_handle); 442aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 452aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com // Full file reading test 462aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com FileInputStream is; 47246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Open(SAMPLE_TTF_FILE); 48246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com EXPECT_EQ(length, (size_t)is.Available()); 492aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com ByteVector b2; 50246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Read(&b2, 0, length); 51246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Close(); 522aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[0]), &(b2[0]), length), 0); 532aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b2.clear(); 542aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 552aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com // Partial reading test 56246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Open(SAMPLE_TTF_FILE); 57246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Skip(89); 58246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Read(&b2, 0, 100); 592aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 100), 0); 602aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b2.clear(); 612aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 622aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com // Skip test 63246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Skip(-89); 64246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Read(&b2, 0, 100); 652aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[100]), &(b2[0]), 100), 0); 662aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b2.clear(); 67246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Skip(100); 68246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Read(&b2, 0, 100); 692aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[300]), &(b2[0]), 100), 0); 70246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Skip(-400); 712aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b2.clear(); 722aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 732aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com // Offset test 74246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Read(&b2, 0, 100); 75246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Read(&b2, 100, 100); 762aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[0]), &(b2[0]), 200), 0); 772aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 782aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com // Unread test 792aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com ByteVector b3; 802aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b3.resize(200); 81246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Unread(&b3); 822aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b3[0]), &(b2[0]), 200), 0); 832aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 842aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com return true; 852aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com} 862aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 87246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.combool TestFontInputStreamBasic() { 88ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com FILE* file_handle = NULL; 892aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#if defined (WIN32) 90ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com fopen_s(&file_handle, SAMPLE_TTF_FILE, "rb"); 912aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#else 92ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com file_handle = fopen(SAMPLE_TTF_FILE, "rb"); 932aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com#endif 942aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com if (file_handle == NULL) { 952aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com return false; 962aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com } 972aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com fseek(file_handle, 0, SEEK_END); 982aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com size_t length = ftell(file_handle); 992aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com fseek(file_handle, 0, SEEK_SET); 1002aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com ByteVector b1; 1012aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b1.resize(length); 10235a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com size_t bytes_read = fread(&(b1[0]), 1, length, file_handle); 10335a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com EXPECT_EQ(bytes_read, length); 1042aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com fclose(file_handle); 1052aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 1062aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com FileInputStream is; 107246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Open(SAMPLE_TTF_FILE); 1082aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com FontInputStream font_is1(&is); 109246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com EXPECT_EQ((size_t)font_is1.Available(), length); 1102aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 1112aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com ByteVector b2; 112246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com font_is1.Read(&b2, 0, length); 113246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com font_is1.Close(); 1142aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[0]), &(b2[0]), length), 0); 1152aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b2.clear(); 1162aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 117246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Open(SAMPLE_TTF_FILE); 118246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Skip(89); 1192aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com FontInputStream font_is2(&is, 200); 120246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com font_is2.Read(&b2, 0, 100); 1212aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 100), 0); 122246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com font_is2.Read(&b2, 100, 100); 1232aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 200), 0); 1242aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com b2.clear(); 125246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com font_is2.Skip(-200); 126246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com font_is2.Read(&b2, 0, 100); 1272aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com EXPECT_EQ(memcmp(&(b1[89]), &(b2[0]), 100), 0); 1282aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 1292aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com return true; 1302aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com} 1312aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com 132246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.combool TestFontInputStreamTableLoading() { 133ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com FileInputStream is; 134246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com is.Open(SAMPLE_TTF_FILE); 135ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com FontInputStream font_is(&is); 136ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com 1376b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com font_is.Skip(TTF_OFFSET[SAMPLE_TTF_FEAT]); 1386b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com FontInputStream gdef_is(&font_is, TTF_LENGTH[SAMPLE_TTF_FEAT]); 1396b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com ByteVector feat_data; 1406b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com gdef_is.Read(&feat_data, 0, TTF_LENGTH[SAMPLE_TTF_FEAT]); 1416b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com EXPECT_EQ(memcmp(&(feat_data[0]), TTF_FEAT_DATA, 1426b8e073e978eed96605da6f92d6db740a39864baarthurhsu@google.com TTF_LENGTH[SAMPLE_TTF_FEAT]), 0); 143ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com 144ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com return true; 145ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com} 146ed8406cf320973d04bbe348c681090b1feba3d68arthurhsu@google.com 1472aa6d0f9873471e25e471f778153eec1001e42e0arthurhsu@google.com} // namespace sfntly 1486a22b959c06e66a039c630e6ac514234114b46cbarthurhsu@google.com 1496a22b959c06e66a039c630e6ac514234114b46cbarthurhsu@google.comTEST(FileIO, All) { 1506a22b959c06e66a039c630e6ac514234114b46cbarthurhsu@google.com ASSERT_TRUE(sfntly::TestFileInputStream()); 1516a22b959c06e66a039c630e6ac514234114b46cbarthurhsu@google.com ASSERT_TRUE(sfntly::TestFontInputStreamBasic()); 1526a22b959c06e66a039c630e6ac514234114b46cbarthurhsu@google.com ASSERT_TRUE(sfntly::TestFontInputStreamTableLoading()); 1536a22b959c06e66a039c630e6ac514234114b46cbarthurhsu@google.com} 154