• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include <limits>
8 
9 #include "testing/gtest/include/gtest/gtest.h"
10 
TEST(fxcrt,FXSYS_HexCharToInt)11 TEST(fxcrt, FXSYS_HexCharToInt) {
12   EXPECT_EQ(10, FXSYS_HexCharToInt('a'));
13   EXPECT_EQ(10, FXSYS_HexCharToInt('A'));
14   EXPECT_EQ(7, FXSYS_HexCharToInt('7'));
15   EXPECT_EQ(0, FXSYS_HexCharToInt('i'));
16 }
17 
TEST(fxcrt,FXSYS_DecimalCharToInt)18 TEST(fxcrt, FXSYS_DecimalCharToInt) {
19   EXPECT_EQ(7, FXSYS_DecimalCharToInt('7'));
20   EXPECT_EQ(0, FXSYS_DecimalCharToInt('a'));
21   EXPECT_EQ(7, FXSYS_DecimalCharToInt(L'7'));
22   EXPECT_EQ(0, FXSYS_DecimalCharToInt(L'a'));
23   EXPECT_EQ(0, FXSYS_DecimalCharToInt(static_cast<char>(-78)));
24   EXPECT_EQ(0, FXSYS_DecimalCharToInt(static_cast<wchar_t>(0xb2)));
25 }
26 
TEST(fxcrt,FXSYS_IsDecimalDigit)27 TEST(fxcrt, FXSYS_IsDecimalDigit) {
28   EXPECT_TRUE(FXSYS_IsDecimalDigit('7'));
29   EXPECT_TRUE(FXSYS_IsDecimalDigit(L'7'));
30   EXPECT_FALSE(FXSYS_IsDecimalDigit('a'));
31   EXPECT_FALSE(FXSYS_IsDecimalDigit(L'a'));
32   EXPECT_FALSE(FXSYS_IsDecimalDigit(static_cast<char>(-78)));
33   EXPECT_FALSE(FXSYS_IsDecimalDigit(static_cast<wchar_t>(0xb2)));
34 }
35 
TEST(fxcrt,FXSYS_IntToTwoHexChars)36 TEST(fxcrt, FXSYS_IntToTwoHexChars) {
37   char buf[3] = {0};
38   FXSYS_IntToTwoHexChars(0x0, buf);
39   EXPECT_STREQ("00", buf);
40   FXSYS_IntToTwoHexChars(0x9, buf);
41   EXPECT_STREQ("09", buf);
42   FXSYS_IntToTwoHexChars(0xA, buf);
43   EXPECT_STREQ("0A", buf);
44   FXSYS_IntToTwoHexChars(0x8C, buf);
45   EXPECT_STREQ("8C", buf);
46   FXSYS_IntToTwoHexChars(0xBE, buf);
47   EXPECT_STREQ("BE", buf);
48   FXSYS_IntToTwoHexChars(0xD0, buf);
49   EXPECT_STREQ("D0", buf);
50   FXSYS_IntToTwoHexChars(0xFF, buf);
51   EXPECT_STREQ("FF", buf);
52 }
53 
TEST(fxcrt,FXSYS_IntToFourHexChars)54 TEST(fxcrt, FXSYS_IntToFourHexChars) {
55   char buf[5] = {0};
56   FXSYS_IntToFourHexChars(0x0, buf);
57   EXPECT_STREQ("0000", buf);
58   FXSYS_IntToFourHexChars(0xA23, buf);
59   EXPECT_STREQ("0A23", buf);
60   FXSYS_IntToFourHexChars(0xB701, buf);
61   EXPECT_STREQ("B701", buf);
62   FXSYS_IntToFourHexChars(0xFFFF, buf);
63   EXPECT_STREQ("FFFF", buf);
64 }
65 
TEST(fxcrt,FXSYS_ToUTF16BE)66 TEST(fxcrt, FXSYS_ToUTF16BE) {
67   char buf[9] = {0};
68   // Test U+0000 to U+D7FF and U+E000 to U+FFFF
69   EXPECT_EQ(4U, FXSYS_ToUTF16BE(0x0, buf));
70   EXPECT_STREQ("0000", buf);
71   EXPECT_EQ(4U, FXSYS_ToUTF16BE(0xD7FF, buf));
72   EXPECT_STREQ("D7FF", buf);
73   EXPECT_EQ(4U, FXSYS_ToUTF16BE(0xE000, buf));
74   EXPECT_STREQ("E000", buf);
75   EXPECT_EQ(4U, FXSYS_ToUTF16BE(0xFFFF, buf));
76   EXPECT_STREQ("FFFF", buf);
77   // Test U+10000 to U+10FFFF
78   EXPECT_EQ(8U, FXSYS_ToUTF16BE(0x10000, buf));
79   EXPECT_STREQ("D800DC00", buf);
80   EXPECT_EQ(8U, FXSYS_ToUTF16BE(0x10FFFF, buf));
81   EXPECT_STREQ("DBFFDFFF", buf);
82   EXPECT_EQ(8U, FXSYS_ToUTF16BE(0x2003E, buf));
83   EXPECT_STREQ("D840DC3E", buf);
84 }
85 
TEST(fxcrt,FXSYS_wcstof)86 TEST(fxcrt, FXSYS_wcstof) {
87   int32_t used_len = 0;
88   EXPECT_FLOAT_EQ(-12.0f, FXSYS_wcstof(L"-12", 3, &used_len));
89   EXPECT_EQ(3, used_len);
90 
91   used_len = 0;
92   EXPECT_FLOAT_EQ(1.5362f, FXSYS_wcstof(L"1.5362", 6, &used_len));
93   EXPECT_EQ(6, used_len);
94 
95   used_len = 0;
96   EXPECT_FLOAT_EQ(0.875f, FXSYS_wcstof(L"0.875", 5, &used_len));
97   EXPECT_EQ(5, used_len);
98 
99   used_len = 0;
100   EXPECT_FLOAT_EQ(5.56e-2f, FXSYS_wcstof(L"5.56e-2", 7, &used_len));
101   EXPECT_EQ(7, used_len);
102 
103   used_len = 0;
104   EXPECT_FLOAT_EQ(1.234e10f, FXSYS_wcstof(L"1.234E10", 8, &used_len));
105   EXPECT_EQ(8, used_len);
106 
107   used_len = 0;
108   EXPECT_FLOAT_EQ(0.0f, FXSYS_wcstof(L"1.234E100000000000000", 21, &used_len));
109   EXPECT_EQ(0, used_len);
110 
111   used_len = 0;
112   EXPECT_FLOAT_EQ(0.0f, FXSYS_wcstof(L"1.234E-128", 21, &used_len));
113   EXPECT_EQ(0, used_len);
114 
115   // TODO(dsinclair): This should round as per IEEE 64-bit values.
116   // EXPECT_EQ(L"123456789.01234567", FXSYS_wcstof(L"123456789.012345678"));
117   used_len = 0;
118   EXPECT_FLOAT_EQ(123456789.012345678f,
119                   FXSYS_wcstof(L"123456789.012345678", 19, &used_len));
120   EXPECT_EQ(19, used_len);
121 
122   // TODO(dsinclair): This is spec'd as rounding when > 16 significant digits
123   // prior to the exponent.
124   // EXPECT_EQ(100000000000000000, FXSYS_wcstof(L"99999999999999999"));
125   used_len = 0;
126   EXPECT_FLOAT_EQ(99999999999999999.0f,
127                   FXSYS_wcstof(L"99999999999999999", 17, &used_len));
128   EXPECT_EQ(17, used_len);
129 
130   // For https://crbug.com/pdfium/1217
131   EXPECT_FLOAT_EQ(0.0f, FXSYS_wcstof(L"e76", 3, nullptr));
132 
133   // Overflow to infinity.
134   used_len = 0;
135   EXPECT_TRUE(std::isinf(FXSYS_wcstof(
136       L"88888888888888888888888888888888888888888888888888888888888888888888888"
137       L"88888888888888888888888888888888888888888888888888888888888",
138       130, &used_len)));
139   EXPECT_EQ(130, used_len);
140 
141   used_len = 0;
142   EXPECT_TRUE(std::isinf(FXSYS_wcstof(
143       L"-8888888888888888888888888888888888888888888888888888888888888888888888"
144       L"888888888888888888888888888888888888888888888888888888888888",
145       131, &used_len)));
146   EXPECT_EQ(131, used_len);
147 }
148 
TEST(fxcrt,FXSYS_SafeOps)149 TEST(fxcrt, FXSYS_SafeOps) {
150   const float fMin = std::numeric_limits<float>::min();
151   const float fMax = std::numeric_limits<float>::max();
152   const float fInf = std::numeric_limits<float>::infinity();
153   const float fNan = std::numeric_limits<float>::quiet_NaN();
154   const float ascending[] = {fMin, 1.0f, 2.0f, fMax, fInf, fNan};
155 
156   for (size_t i = 0; i < FX_ArraySize(ascending); ++i) {
157     for (size_t j = 0; j < FX_ArraySize(ascending); ++j) {
158       if (i == j) {
159         EXPECT_TRUE(FXSYS_SafeEQ(ascending[i], ascending[j]))
160             << " at " << i << " " << j;
161       } else {
162         EXPECT_FALSE(FXSYS_SafeEQ(ascending[i], ascending[j]))
163             << " at " << i << " " << j;
164       }
165       if (i < j) {
166         EXPECT_TRUE(FXSYS_SafeLT(ascending[i], ascending[j]))
167             << " at " << i << " " << j;
168       } else {
169         EXPECT_FALSE(FXSYS_SafeLT(ascending[i], ascending[j]))
170             << " at " << i << " " << j;
171       }
172     }
173   }
174 }
175