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