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