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// Must include at the first line to avoid ICU / stdint conflict.
18#include "sfntly/port/type.h"
19
20#include <stdio.h>
21#include <unicode/ucnv.h>
22#include <unicode/uchar.h>
23
24#include "gtest/gtest.h"
25#include "test/test_utils.h"
26
27namespace sfntly {
28
29// Check if proper encoding is being performed
30// Conversion is done from UTF16 to UTF8, SJIS
31bool TestEncoding() {
32  UConverter* conv = TestUtils::GetEncoder("utf8");
33  EXPECT_TRUE(conv != NULL);
34  // Ūnĭcōde̽
35  UChar from[8] = {0x016A, 0x006E, 0x012D, 0x0063, 0x014D, 0x0064, 0x0065,
36                   0x033D};
37  int32_t want[12] = {0xc5, 0xaa, 0x6e, 0xc4, 0xad, 0x63, 0xc5, 0x8d, 0x64,
38                      0x65, 0xcc, 0xbd};
39  int32_t i, j = 0;
40  for (i = 0; i < 7; ++i) {
41    int32_t encoded = TestUtils::EncodeOneChar(conv, (int16_t)from[i]);
42    for (; encoded; encoded <<= 8) {
43      byte_t b = (encoded & 0xff000000) >> 24;
44      if (!b)
45        continue;
46      EXPECT_EQ(want[j], b);
47      if (want[j++] != b) {
48        ucnv_close(conv);
49        return false;
50      }
51    }
52  }
53  ucnv_close(conv);
54  return true;
55}
56
57// Check if the proper extension is obtained
58bool TestExtension() {
59  // usual file name
60  const char *result;
61  result = TestUtils::Extension("../data/ext/tuffy.ttf");
62  EXPECT_EQ(strcmp(result, ".ttf"), 0);
63
64  // more than one 'extension'
65  result = TestUtils::Extension("tuffy.ttf.fake");
66  EXPECT_EQ(strcmp(result, ".fake"), 0);
67
68  // no extension
69  result = TestUtils::Extension("tuffy");
70  EXPECT_STREQ(result, NULL);
71
72  // bogus extension
73  result = TestUtils::Extension("tuffy.");
74  EXPECT_EQ(strcmp(result, "."), 0);
75
76  return true;
77}
78
79}  // namespace sfntly
80
81TEST(TestUtils, All) {
82  ASSERT_TRUE(sfntly::TestExtension());
83  ASSERT_TRUE(sfntly::TestEncoding());
84}
85