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_string_unittest.cpp
Go to the documentation of this file.
1// Copyright 2016 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 <limits>
6
7#include "build/build_config.h"
8#include "core/fxcrt/fx_string.h"
9#include "core/fxcrt/utf16.h"
10#include "testing/gtest/include/gtest/gtest.h"
11#include "third_party/base/containers/span.h"
12
13char* TerminatedFloatToString(float value, pdfium::span<char> buf) {
14 size_t buflen = FloatToString(value, buf);
15 buf[buflen] = '\0';
16 return buf.data();
17}
18
19char* TerminatedDoubleToString(double value, pdfium::span<char> buf) {
20 size_t buflen = DoubleToString(value, buf);
21 buf[buflen] = '\0';
22 return buf.data();
23}
24
26 EXPECT_EQ("", FX_UTF8Encode(WideStringView()));
27 EXPECT_EQ(
28 "x"
29 "\u0080"
30 "\u00ff"
31 "\ud7ff"
32 "\ue000"
33 "\uff2c"
34 "\uffff"
35 "y",
36 FX_UTF8Encode(L"x"
37 L"\u0080"
38 L"\u00ff"
39 L"\ud7ff"
40 L"\ue000"
41 L"\uff2c"
42 L"\uffff"
43 L"y"));
44}
45
47 EXPECT_EQ(
48 "\U00010000"
49 "🎨"
50 "\U0010ffff",
51 FX_UTF8Encode(L"\U00010000"
52 L"\U0001f3a8"
53 L"\U0010ffff"));
54}
55
56#if defined(WCHAR_T_IS_16_BIT)
57TEST(fxstring, FXUTF8EncodeSurrogateErrorRecovery) {
58 EXPECT_EQ("(\xed\xa0\x80)", FX_UTF8Encode(L"(\xd800)")) << "High";
59 EXPECT_EQ("(\xed\xb0\x80)", FX_UTF8Encode(L"(\xdc00)")) << "Low";
60 EXPECT_EQ("(\xed\xa0\x80🎨)", FX_UTF8Encode(L"(\xd800\xd83c\xdfa8)"))
61 << "High-high";
62 EXPECT_EQ("(🎨\xed\xb0\x80)", FX_UTF8Encode(L"(\xd83c\xdfa8\xdc00)"))
63 << "Low-low";
64}
65#endif // defined(WCHAR_T_IS_16_BIT)
66
68 EXPECT_FLOAT_EQ(0.0f, StringToFloat(""));
69 EXPECT_FLOAT_EQ(0.0f, StringToFloat("0"));
70 EXPECT_FLOAT_EQ(0.0f, StringToFloat("0.0"));
71 EXPECT_FLOAT_EQ(0.0f, StringToFloat("-0.0"));
72
73 EXPECT_FLOAT_EQ(0.25f, StringToFloat("0.25"));
74 EXPECT_FLOAT_EQ(-0.25f, StringToFloat("-0.25"));
75
76 EXPECT_FLOAT_EQ(100.0f, StringToFloat("100"));
77 EXPECT_FLOAT_EQ(100.0f, StringToFloat("100.0"));
78 EXPECT_FLOAT_EQ(100.0f, StringToFloat(" 100.0"));
79 EXPECT_FLOAT_EQ(-100.0f, StringToFloat("-100.0000"));
80
81 EXPECT_FLOAT_EQ(3.402823e+38f,
82 StringToFloat("340282300000000000000000000000000000000"));
83 EXPECT_FLOAT_EQ(-3.402823e+38f,
84 StringToFloat("-340282300000000000000000000000000000000"));
85
86 EXPECT_FLOAT_EQ(1.000000119f, StringToFloat("1.000000119"));
87 EXPECT_FLOAT_EQ(1.999999881f, StringToFloat("1.999999881"));
88}
89
91 EXPECT_FLOAT_EQ(0.0f, StringToFloat(L""));
92 EXPECT_FLOAT_EQ(0.0f, StringToFloat(L"0"));
93 EXPECT_FLOAT_EQ(0.0f, StringToFloat(L"0.0"));
94 EXPECT_FLOAT_EQ(0.0f, StringToFloat(L"-0.0"));
95
96 EXPECT_FLOAT_EQ(0.25f, StringToFloat(L"0.25"));
97 EXPECT_FLOAT_EQ(-0.25f, StringToFloat(L"-0.25"));
98
99 EXPECT_FLOAT_EQ(100.0f, StringToFloat(L"100"));
100 EXPECT_FLOAT_EQ(100.0f, StringToFloat(L"100.0"));
101 EXPECT_FLOAT_EQ(100.0f, StringToFloat(L" 100.0"));
102 EXPECT_FLOAT_EQ(-100.0f, StringToFloat(L"-100.0000"));
103
104 EXPECT_FLOAT_EQ(3.402823e+38f,
105 StringToFloat(L"340282300000000000000000000000000000000"));
106 EXPECT_FLOAT_EQ(-3.402823e+38f,
107 StringToFloat(L"-340282300000000000000000000000000000000"));
108
109 EXPECT_FLOAT_EQ(1.000000119f, StringToFloat(L"1.000000119"));
110 EXPECT_FLOAT_EQ(1.999999881f, StringToFloat(L"1.999999881"));
111}
112
113TEST(fxstring, FloatToString) {
114 char buf[32];
115
116 EXPECT_STREQ("0", TerminatedFloatToString(0.0f, buf));
117 EXPECT_STREQ("0", TerminatedFloatToString(-0.0f, buf));
118 EXPECT_STREQ("0",
119 TerminatedFloatToString(std::numeric_limits<float>::min(), buf));
120 EXPECT_STREQ(
121 "0", TerminatedFloatToString(-std::numeric_limits<float>::min(), buf));
122
123 EXPECT_STREQ("0.25", TerminatedFloatToString(0.25f, buf));
124 EXPECT_STREQ("-0.25", TerminatedFloatToString(-0.25f, buf));
125
126 EXPECT_STREQ("100", TerminatedFloatToString(100.0f, buf));
127 EXPECT_STREQ("-100", TerminatedFloatToString(-100.0f, buf));
128
129 // FloatToString won't convert beyond the maximum integer, and values
130 // larger than that get converted to a string representing that.
131 EXPECT_STREQ("2147483647", TerminatedFloatToString(2147483647.0f, buf));
132 EXPECT_STREQ("2147483647", TerminatedFloatToString(2147483647.5f, buf));
133 EXPECT_STREQ("2147483647",
134 TerminatedFloatToString(std::numeric_limits<float>::max(), buf));
135
136 // FloatToString won't convert beyond the minimum integer, and values
137 // smaller than that get converted to a string representing that.
138 EXPECT_STREQ("-2147483647", TerminatedFloatToString(-2147483647.0f, buf));
139 EXPECT_STREQ("-2147483647", TerminatedFloatToString(-2147483647.5f, buf));
140 EXPECT_STREQ("-2147483647", TerminatedFloatToString(
141 -std::numeric_limits<float>::max(), buf));
142
143 // Conversion only acknowledges precision to 5 digit past decimal, and
144 // rounds beyond that.
145 EXPECT_STREQ("1", TerminatedFloatToString(1.000001119f, buf));
146 EXPECT_STREQ("1.00001", TerminatedFloatToString(1.000011119f, buf));
147 EXPECT_STREQ("1.99999", TerminatedFloatToString(1.999988881f, buf));
148 EXPECT_STREQ("2", TerminatedFloatToString(1.999999881f, buf));
149}
150
152 EXPECT_FLOAT_EQ(0.0, StringToDouble(""));
153 EXPECT_FLOAT_EQ(0.0, StringToDouble("0"));
154 EXPECT_FLOAT_EQ(0.0, StringToDouble("0.0"));
155 EXPECT_FLOAT_EQ(0.0, StringToDouble("-0.0"));
156
157 EXPECT_FLOAT_EQ(0.25, StringToDouble("0.25"));
158 EXPECT_FLOAT_EQ(-0.25, StringToDouble("-0.25"));
159
160 EXPECT_FLOAT_EQ(100.0, StringToDouble("100"));
161 EXPECT_FLOAT_EQ(100.0, StringToDouble("100.0"));
162 EXPECT_FLOAT_EQ(100.0, StringToDouble(" 100.0"));
163 EXPECT_FLOAT_EQ(-100.0, StringToDouble("-100.0000"));
164
165 EXPECT_FLOAT_EQ(3.402823e+38,
166 StringToDouble("340282300000000000000000000000000000000"));
167 EXPECT_FLOAT_EQ(-3.402823e+38,
168 StringToDouble("-340282300000000000000000000000000000000"));
169
170 EXPECT_FLOAT_EQ(1.000000119, StringToDouble("1.000000119"));
171 EXPECT_FLOAT_EQ(1.999999881, StringToDouble("1.999999881"));
172}
173
175 EXPECT_FLOAT_EQ(0.0, StringToDouble(L""));
176 EXPECT_FLOAT_EQ(0.0, StringToDouble(L"0"));
177 EXPECT_FLOAT_EQ(0.0, StringToDouble(L"0.0"));
178 EXPECT_FLOAT_EQ(0.0, StringToDouble(L"-0.0"));
179
180 EXPECT_FLOAT_EQ(0.25, StringToDouble(L"0.25"));
181 EXPECT_FLOAT_EQ(-0.25, StringToDouble(L"-0.25"));
182
183 EXPECT_FLOAT_EQ(100.0, StringToDouble(L"100"));
184 EXPECT_FLOAT_EQ(100.0, StringToDouble(L"100.0"));
185 EXPECT_FLOAT_EQ(100.0, StringToDouble(L" 100.0"));
186 EXPECT_FLOAT_EQ(-100.0, StringToDouble(L"-100.0000"));
187
188 EXPECT_FLOAT_EQ(3.402823e+38,
189 StringToDouble(L"340282300000000000000000000000000000000"));
190 EXPECT_FLOAT_EQ(-3.402823e+38,
191 StringToDouble(L"-340282300000000000000000000000000000000"));
192
193 EXPECT_FLOAT_EQ(1.000000119, StringToDouble(L"1.000000119"));
194 EXPECT_FLOAT_EQ(1.999999881, StringToDouble(L"1.999999881"));
195}
196
197TEST(fxstring, DoubleToString) {
198 char buf[32];
199
200 EXPECT_STREQ("0", TerminatedDoubleToString(0.0f, buf));
201 EXPECT_STREQ("0", TerminatedDoubleToString(-0.0f, buf));
202 EXPECT_STREQ(
203 "0", TerminatedDoubleToString(std::numeric_limits<double>::min(), buf));
204 EXPECT_STREQ(
205 "0", TerminatedDoubleToString(-std::numeric_limits<double>::min(), buf));
206
207 EXPECT_STREQ("0.25", TerminatedDoubleToString(0.25f, buf));
208 EXPECT_STREQ("-0.25", TerminatedDoubleToString(-0.25f, buf));
209
210 EXPECT_STREQ("100", TerminatedDoubleToString(100.0f, buf));
211 EXPECT_STREQ("-100", TerminatedDoubleToString(-100.0f, buf));
212
213 // DoubleToString won't convert beyond the maximum integer, and values
214 // larger than that get converted to a string representing that.
215 EXPECT_STREQ("2147483647", TerminatedDoubleToString(2147483647.0f, buf));
216 EXPECT_STREQ("2147483647", TerminatedDoubleToString(2147483647.5f, buf));
217 EXPECT_STREQ("2147483647", TerminatedDoubleToString(
218 std::numeric_limits<double>::max(), buf));
219
220 // DoubleToString won't convert beyond the minimum integer, and values
221 // smaller than that get converted to a string representing that.
222 EXPECT_STREQ("-2147483647", TerminatedDoubleToString(-2147483647.0f, buf));
223 EXPECT_STREQ("-2147483647", TerminatedDoubleToString(-2147483647.5f, buf));
224 EXPECT_STREQ("-2147483647", TerminatedDoubleToString(
225 -std::numeric_limits<double>::max(), buf));
226
227 // Conversion only acknowledges precision to 5 digit past decimal, and
228 // rounds beyond that.
229 EXPECT_STREQ("1", TerminatedDoubleToString(1.000001119f, buf));
230 EXPECT_STREQ("1.00001", TerminatedDoubleToString(1.000011119f, buf));
231 EXPECT_STREQ("1.99999", TerminatedDoubleToString(1.999988881f, buf));
232 EXPECT_STREQ("2", TerminatedDoubleToString(1.999999881f, buf));
233}
234
236 std::vector<ByteString> result;
237 result = fxcrt::Split(ByteString(""), ',');
238 ASSERT_EQ(1u, result.size());
239 EXPECT_EQ("", result[0]);
240
241 result = fxcrt::Split(ByteString("a"), ',');
242 ASSERT_EQ(1u, result.size());
243 EXPECT_EQ("a", result[0]);
244
245 result = fxcrt::Split(ByteString(","), ',');
246 ASSERT_EQ(2u, result.size());
247 EXPECT_EQ("", result[0]);
248 EXPECT_EQ("", result[1]);
249
250 result = fxcrt::Split(ByteString("a,"), ',');
251 ASSERT_EQ(2u, result.size());
252 EXPECT_EQ("a", result[0]);
253 EXPECT_EQ("", result[1]);
254
255 result = fxcrt::Split(ByteString(",b"), ',');
256 ASSERT_EQ(2u, result.size());
257 EXPECT_EQ("", result[0]);
258 EXPECT_EQ("b", result[1]);
259
260 result = fxcrt::Split(ByteString("a,b"), ',');
261 ASSERT_EQ(2u, result.size());
262 EXPECT_EQ("a", result[0]);
263 EXPECT_EQ("b", result[1]);
264
265 result = fxcrt::Split(ByteString("a,b,"), ',');
266 ASSERT_EQ(3u, result.size());
267 EXPECT_EQ("a", result[0]);
268 EXPECT_EQ("b", result[1]);
269 EXPECT_EQ("", result[2]);
270
271 result = fxcrt::Split(ByteString("a,,"), ',');
272 ASSERT_EQ(3u, result.size());
273 EXPECT_EQ("a", result[0]);
274 EXPECT_EQ("", result[1]);
275 EXPECT_EQ("", result[2]);
276
277 result = fxcrt::Split(ByteString(",,a"), ',');
278 ASSERT_EQ(3u, result.size());
279 EXPECT_EQ("", result[0]);
280 EXPECT_EQ("", result[1]);
281 EXPECT_EQ("a", result[2]);
282}
283
285 std::vector<ByteStringView> result;
286 result = fxcrt::Split(ByteStringView(""), ',');
287 ASSERT_EQ(1u, result.size());
288 EXPECT_EQ("", result[0]);
289
290 result = fxcrt::Split(ByteStringView("a"), ',');
291 ASSERT_EQ(1u, result.size());
292 EXPECT_EQ("a", result[0]);
293
294 result = fxcrt::Split(ByteStringView(","), ',');
295 ASSERT_EQ(2u, result.size());
296 EXPECT_EQ("", result[0]);
297 EXPECT_EQ("", result[1]);
298
299 result = fxcrt::Split(ByteStringView("a,"), ',');
300 ASSERT_EQ(2u, result.size());
301 EXPECT_EQ("a", result[0]);
302 EXPECT_EQ("", result[1]);
303
304 result = fxcrt::Split(ByteStringView(",b"), ',');
305 ASSERT_EQ(2u, result.size());
306 EXPECT_EQ("", result[0]);
307 EXPECT_EQ("b", result[1]);
308
309 result = fxcrt::Split(ByteStringView("a,b"), ',');
310 ASSERT_EQ(2u, result.size());
311 EXPECT_EQ("a", result[0]);
312 EXPECT_EQ("b", result[1]);
313
314 result = fxcrt::Split(ByteStringView("a,b,"), ',');
315 ASSERT_EQ(3u, result.size());
316 EXPECT_EQ("a", result[0]);
317 EXPECT_EQ("b", result[1]);
318 EXPECT_EQ("", result[2]);
319
320 result = fxcrt::Split(ByteStringView("a,,"), ',');
321 ASSERT_EQ(3u, result.size());
322 EXPECT_EQ("a", result[0]);
323 EXPECT_EQ("", result[1]);
324 EXPECT_EQ("", result[2]);
325
326 result = fxcrt::Split(ByteStringView(",,a"), ',');
327 ASSERT_EQ(3u, result.size());
328 EXPECT_EQ("", result[0]);
329 EXPECT_EQ("", result[1]);
330 EXPECT_EQ("a", result[2]);
331}
332
334 std::vector<WideString> result;
335 result = fxcrt::Split(WideString(L""), L',');
336 ASSERT_EQ(1u, result.size());
337 EXPECT_EQ(L"", result[0]);
338
339 result = fxcrt::Split(WideString(L"a"), L',');
340 ASSERT_EQ(1u, result.size());
341 EXPECT_EQ(L"a", result[0]);
342
343 result = fxcrt::Split(WideString(L","), L',');
344 ASSERT_EQ(2u, result.size());
345 EXPECT_EQ(L"", result[0]);
346 EXPECT_EQ(L"", result[1]);
347
348 result = fxcrt::Split(WideString(L"a,"), L',');
349 ASSERT_EQ(2u, result.size());
350 EXPECT_EQ(L"a", result[0]);
351 EXPECT_EQ(L"", result[1]);
352
353 result = fxcrt::Split(WideString(L",b"), L',');
354 ASSERT_EQ(2u, result.size());
355 EXPECT_EQ(L"", result[0]);
356 EXPECT_EQ(L"b", result[1]);
357
358 result = fxcrt::Split(WideString(L"a,b"), L',');
359 ASSERT_EQ(2u, result.size());
360 EXPECT_EQ(L"a", result[0]);
361 EXPECT_EQ(L"b", result[1]);
362
363 result = fxcrt::Split(WideString(L"a,b,"), L',');
364 ASSERT_EQ(3u, result.size());
365 EXPECT_EQ(L"a", result[0]);
366 EXPECT_EQ(L"b", result[1]);
367 EXPECT_EQ(L"", result[2]);
368
369 result = fxcrt::Split(WideString(L"a,,"), L',');
370 ASSERT_EQ(3u, result.size());
371 EXPECT_EQ(L"a", result[0]);
372 EXPECT_EQ(L"", result[1]);
373 EXPECT_EQ(L"", result[2]);
374
375 result = fxcrt::Split(WideString(L",,a"), L',');
376 ASSERT_EQ(3u, result.size());
377 EXPECT_EQ(L"", result[0]);
378 EXPECT_EQ(L"", result[1]);
379 EXPECT_EQ(L"a", result[2]);
380}
381
383 std::vector<WideStringView> result;
384 result = fxcrt::Split(WideStringView(L""), L',');
385 ASSERT_EQ(1u, result.size());
386 EXPECT_EQ(L"", result[0]);
387
388 result = fxcrt::Split(WideStringView(L"a"), L',');
389 ASSERT_EQ(1u, result.size());
390 EXPECT_EQ(L"a", result[0]);
391
392 result = fxcrt::Split(WideStringView(L","), L',');
393 ASSERT_EQ(2u, result.size());
394 EXPECT_EQ(L"", result[0]);
395 EXPECT_EQ(L"", result[1]);
396
397 result = fxcrt::Split(WideStringView(L"a,"), L',');
398 ASSERT_EQ(2u, result.size());
399 EXPECT_EQ(L"a", result[0]);
400 EXPECT_EQ(L"", result[1]);
401
402 result = fxcrt::Split(WideStringView(L",b"), L',');
403 ASSERT_EQ(2u, result.size());
404 EXPECT_EQ(L"", result[0]);
405 EXPECT_EQ(L"b", result[1]);
406
407 result = fxcrt::Split(WideStringView(L"a,b"), L',');
408 ASSERT_EQ(2u, result.size());
409 EXPECT_EQ(L"a", result[0]);
410 EXPECT_EQ(L"b", result[1]);
411
412 result = fxcrt::Split(WideStringView(L"a,b,"), L',');
413 ASSERT_EQ(3u, result.size());
414 EXPECT_EQ(L"a", result[0]);
415 EXPECT_EQ(L"b", result[1]);
416 EXPECT_EQ(L"", result[2]);
417
418 result = fxcrt::Split(WideStringView(L"a,,"), L',');
419 ASSERT_EQ(3u, result.size());
420 EXPECT_EQ(L"a", result[0]);
421 EXPECT_EQ(L"", result[1]);
422 EXPECT_EQ(L"", result[2]);
423
424 result = fxcrt::Split(WideStringView(L",,a"), L',');
425 ASSERT_EQ(3u, result.size());
426 EXPECT_EQ(L"", result[0]);
427 EXPECT_EQ(L"", result[1]);
428 EXPECT_EQ(L"a", result[2]);
429}
430
432 std::vector<char> commas(50000, ',');
433 ByteString input(commas.data(), commas.size());
434 std::vector<ByteString> result;
435 result = fxcrt::Split(input, ',');
436 ASSERT_EQ(commas.size() + 1, result.size());
437 EXPECT_EQ("", result.front());
438 EXPECT_EQ("", result.back());
439}
440
442 std::vector<char> commas(50000, ',');
443 ByteStringView input(commas.data(), commas.size());
444 std::vector<ByteStringView> result;
445 result = fxcrt::Split(input, ',');
446 ASSERT_EQ(commas.size() + 1, result.size());
447 EXPECT_EQ("", result.front());
448 EXPECT_EQ("", result.back());
449}
450
452 std::vector<wchar_t> commas(50000, L',');
453 WideString input(commas.data(), commas.size());
454 std::vector<WideString> result;
455 result = fxcrt::Split(input, ',');
456 ASSERT_EQ(commas.size() + 1, result.size());
457 EXPECT_EQ(L"", result.front());
458 EXPECT_EQ(L"", result.back());
459}
460
462 std::vector<wchar_t> commas(50000, L',');
463 WideStringView input(commas.data(), commas.size());
464 std::vector<WideStringView> result;
465 result = fxcrt::Split(input, ',');
466 ASSERT_EQ(commas.size() + 1, result.size());
467 EXPECT_EQ(L"", result.front());
468 EXPECT_EQ(L"", result.back());
469}
ByteString(const char *ptr)
WideString(const wchar_t *ptr)
TEST(FXCRYPT, CryptToBase16)
TEST(FXCRYPT, MD5GenerateEmtpyData)
char * TerminatedFloatToString(float value, pdfium::span< char > buf)
char * TerminatedDoubleToString(double value, pdfium::span< char > buf)