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/fxcrt/fx_extension.h"
6#include "testing/gtest/include/gtest/gtest.h"
7
8namespace {
9
10uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) {
11  int result = 0;
12  for (int i = 0; i < nbits; i++) {
13    if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8)))
14      result |= 1 << (nbits - i - 1);
15  }
16  return result;
17}
18
19}  // namespace
20
21TEST(fxcrt, FXSYS_HexCharToInt) {
22  EXPECT_EQ(10, FXSYS_HexCharToInt('a'));
23  EXPECT_EQ(10, FXSYS_HexCharToInt('A'));
24  EXPECT_EQ(7, FXSYS_HexCharToInt('7'));
25  EXPECT_EQ(0, FXSYS_HexCharToInt('i'));
26}
27
28TEST(fxcrt, FXSYS_DecimalCharToInt) {
29  EXPECT_EQ(7, FXSYS_DecimalCharToInt('7'));
30  EXPECT_EQ(0, FXSYS_DecimalCharToInt('a'));
31  EXPECT_EQ(7, FXSYS_DecimalCharToInt(L'7'));
32  EXPECT_EQ(0, FXSYS_DecimalCharToInt(L'a'));
33}
34
35TEST(fxcrt, FXSYS_isDecimalDigit) {
36  EXPECT_TRUE(FXSYS_isDecimalDigit('7'));
37  EXPECT_TRUE(FXSYS_isDecimalDigit(L'7'));
38  EXPECT_FALSE(FXSYS_isDecimalDigit('a'));
39  EXPECT_FALSE(FXSYS_isDecimalDigit(L'a'));
40}
41
42TEST(fxcrt, FX_HashCode_Ascii) {
43  EXPECT_EQ(0u, FX_HashCode_GetA("", false));
44  EXPECT_EQ(65u, FX_HashCode_GetA("A", false));
45  EXPECT_EQ(97u, FX_HashCode_GetA("A", true));
46  EXPECT_EQ(31 * 65u + 66u, FX_HashCode_GetA("AB", false));
47}
48
49TEST(fxcrt, FX_HashCode_Wide) {
50  EXPECT_EQ(0u, FX_HashCode_GetW(L"", false));
51  EXPECT_EQ(65u, FX_HashCode_GetW(L"A", false));
52  EXPECT_EQ(97u, FX_HashCode_GetW(L"A", true));
53  EXPECT_EQ(1313 * 65u + 66u, FX_HashCode_GetW(L"AB", false));
54}
55
56TEST(fxcrt, FXSYS_IntToTwoHexChars) {
57  char buf[3] = {0};
58  FXSYS_IntToTwoHexChars(0x0, buf);
59  EXPECT_STREQ("00", buf);
60  FXSYS_IntToTwoHexChars(0x9, buf);
61  EXPECT_STREQ("09", buf);
62  FXSYS_IntToTwoHexChars(0xA, buf);
63  EXPECT_STREQ("0A", buf);
64  FXSYS_IntToTwoHexChars(0x8C, buf);
65  EXPECT_STREQ("8C", buf);
66  FXSYS_IntToTwoHexChars(0xBE, buf);
67  EXPECT_STREQ("BE", buf);
68  FXSYS_IntToTwoHexChars(0xD0, buf);
69  EXPECT_STREQ("D0", buf);
70  FXSYS_IntToTwoHexChars(0xFF, buf);
71  EXPECT_STREQ("FF", buf);
72}
73
74TEST(fxcrt, FXSYS_IntToFourHexChars) {
75  char buf[5] = {0};
76  FXSYS_IntToFourHexChars(0x0, buf);
77  EXPECT_STREQ("0000", buf);
78  FXSYS_IntToFourHexChars(0xA23, buf);
79  EXPECT_STREQ("0A23", buf);
80  FXSYS_IntToFourHexChars(0xB701, buf);
81  EXPECT_STREQ("B701", buf);
82  FXSYS_IntToFourHexChars(0xFFFF, buf);
83  EXPECT_STREQ("FFFF", buf);
84}
85
86TEST(fxcrt, FXSYS_ToUTF16BE) {
87  char buf[9] = {0};
88  // Test U+0000 to U+D7FF and U+E000 to U+FFFF
89  EXPECT_EQ(4U, FXSYS_ToUTF16BE(0x0, buf));
90  EXPECT_STREQ("0000", buf);
91  EXPECT_EQ(4U, FXSYS_ToUTF16BE(0xD7FF, buf));
92  EXPECT_STREQ("D7FF", buf);
93  EXPECT_EQ(4U, FXSYS_ToUTF16BE(0xE000, buf));
94  EXPECT_STREQ("E000", buf);
95  EXPECT_EQ(4U, FXSYS_ToUTF16BE(0xFFFF, buf));
96  EXPECT_STREQ("FFFF", buf);
97  // Test U+10000 to U+10FFFF
98  EXPECT_EQ(8U, FXSYS_ToUTF16BE(0x10000, buf));
99  EXPECT_STREQ("D800DC00", buf);
100  EXPECT_EQ(8U, FXSYS_ToUTF16BE(0x10FFFF, buf));
101  EXPECT_STREQ("DBFFDFFF", buf);
102  EXPECT_EQ(8U, FXSYS_ToUTF16BE(0x2003E, buf));
103  EXPECT_STREQ("D840DC3E", buf);
104}
105
106TEST(fxcrt, GetBits32) {
107  unsigned char data[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56};
108  for (int nbits = 1; nbits <= 32; ++nbits) {
109    for (int bitpos = 0; bitpos < (int)sizeof(data) * 8 - nbits; ++bitpos) {
110      EXPECT_EQ(ReferenceGetBits32(data, bitpos, nbits),
111                GetBits32(data, bitpos, nbits));
112    }
113  }
114}
115