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