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