1// Copyright 2015 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fpdfapi/font/cpdf_cmapparser.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace {
10
11bool uint_ranges_equal(uint8_t* a, uint8_t* b, size_t count) {
12  for (size_t i = 0; i < count; ++i) {
13    if (a[i] != b[i])
14      return false;
15  }
16  return true;
17}
18
19}  // namespace
20
21TEST(cpdf_cmapparser, GetCode) {
22  CPDF_CMapParser parser(nullptr);
23
24  EXPECT_EQ(0u, parser.GetCode(""));
25  EXPECT_EQ(0u, parser.GetCode("<"));
26  EXPECT_EQ(194u, parser.GetCode("<c2"));
27  EXPECT_EQ(162u, parser.GetCode("<A2"));
28  EXPECT_EQ(2802u, parser.GetCode("<Af2"));
29  EXPECT_EQ(162u, parser.GetCode("<A2z"));
30
31  EXPECT_EQ(12u, parser.GetCode("12"));
32  EXPECT_EQ(12u, parser.GetCode("12d"));
33  EXPECT_EQ(128u, parser.GetCode("128"));
34
35  EXPECT_EQ(4294967295u, parser.GetCode("<FFFFFFFF"));
36
37  // Overflow a uint32_t.
38  EXPECT_EQ(0u, parser.GetCode("<100000000"));
39}
40
41TEST(cpdf_cmapparser, GetCodeRange) {
42  CPDF_CMapParser parser(nullptr);
43  CPDF_CMap::CodeRange range;
44
45  // Must start with a <
46  EXPECT_FALSE(parser.GetCodeRange(range, "", ""));
47  EXPECT_FALSE(parser.GetCodeRange(range, "A", ""));
48
49  // m_CharSize must be <= 4
50  EXPECT_FALSE(parser.GetCodeRange(range, "<aaaaaaaaaa>", ""));
51  EXPECT_EQ(5u, range.m_CharSize);
52
53  EXPECT_TRUE(parser.GetCodeRange(range, "<12345678>", "<87654321>"));
54  EXPECT_EQ(4u, range.m_CharSize);
55  {
56    uint8_t lower[4] = {18, 52, 86, 120};
57    uint8_t upper[4] = {135, 101, 67, 33};
58    EXPECT_TRUE(uint_ranges_equal(lower, range.m_Lower, range.m_CharSize));
59    EXPECT_TRUE(uint_ranges_equal(upper, range.m_Upper, range.m_CharSize));
60  }
61
62  // Hex characters
63  EXPECT_TRUE(parser.GetCodeRange(range, "<a1>", "<F3>"));
64  EXPECT_EQ(1u, range.m_CharSize);
65  EXPECT_EQ(161, range.m_Lower[0]);
66  EXPECT_EQ(243, range.m_Upper[0]);
67
68  // The second string should return 0's if it is shorter
69  EXPECT_TRUE(parser.GetCodeRange(range, "<a1>", ""));
70  EXPECT_EQ(1u, range.m_CharSize);
71  EXPECT_EQ(161, range.m_Lower[0]);
72  EXPECT_EQ(0, range.m_Upper[0]);
73}
74