1 // Copyright (c) 2012 The Chromium 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.
5 #include "base/strings/string_number_conversions.h"
14 #include "base/format_macros.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 template <typename INT
>
24 struct IntToStringTest
{
26 const char* sexpected
;
27 const char* uexpected
;
32 TEST(StringNumberConversionsTest
, IntToString
) {
33 static const IntToStringTest
<int> int_tests
[] = {
35 { -1, "-1", "4294967295" },
36 { std::numeric_limits
<int>::max(), "2147483647", "2147483647" },
37 { std::numeric_limits
<int>::min(), "-2147483648", "2147483648" },
39 static const IntToStringTest
<int64
> int64_tests
[] = {
41 { -1, "-1", "18446744073709551615" },
42 { std::numeric_limits
<int64
>::max(),
43 "9223372036854775807",
44 "9223372036854775807", },
45 { std::numeric_limits
<int64
>::min(),
46 "-9223372036854775808",
47 "9223372036854775808" },
50 for (size_t i
= 0; i
< arraysize(int_tests
); ++i
) {
51 const IntToStringTest
<int>* test
= &int_tests
[i
];
52 EXPECT_EQ(IntToString(test
->num
), test
->sexpected
);
53 EXPECT_EQ(IntToString16(test
->num
), UTF8ToUTF16(test
->sexpected
));
54 EXPECT_EQ(UintToString(test
->num
), test
->uexpected
);
55 EXPECT_EQ(UintToString16(test
->num
), UTF8ToUTF16(test
->uexpected
));
57 for (size_t i
= 0; i
< arraysize(int64_tests
); ++i
) {
58 const IntToStringTest
<int64
>* test
= &int64_tests
[i
];
59 EXPECT_EQ(Int64ToString(test
->num
), test
->sexpected
);
60 EXPECT_EQ(Int64ToString16(test
->num
), UTF8ToUTF16(test
->sexpected
));
61 EXPECT_EQ(Uint64ToString(test
->num
), test
->uexpected
);
62 EXPECT_EQ(Uint64ToString16(test
->num
), UTF8ToUTF16(test
->uexpected
));
66 TEST(StringNumberConversionsTest
, Uint64ToString
) {
73 {INT_MAX
, "2147483647"},
74 {kuint64max
, "18446744073709551615"},
77 for (size_t i
= 0; i
< arraysize(cases
); ++i
)
78 EXPECT_EQ(cases
[i
].output
, Uint64ToString(cases
[i
].input
));
81 TEST(StringNumberConversionsTest
, SizeTToString
) {
82 size_t size_t_max
= std::numeric_limits
<size_t>::max();
83 std::string size_t_max_string
= StringPrintf("%" PRIuS
, size_t_max
);
92 {INT_MAX
, "2147483647"},
93 {2147483648U, "2147483648"},
94 #if SIZE_MAX > 4294967295U
95 {99999999999U, "99999999999"},
97 {size_t_max
, size_t_max_string
},
100 for (size_t i
= 0; i
< arraysize(cases
); ++i
)
101 EXPECT_EQ(cases
[i
].output
, Uint64ToString(cases
[i
].input
));
104 TEST(StringNumberConversionsTest
, StringToInt
) {
105 static const struct {
112 {"42\x99", 42, false},
113 {"\x99" "42\x99", 0, false},
114 {"-2147483648", INT_MIN
, true},
115 {"2147483647", INT_MAX
, true},
119 {"\t\n\v\f\r 42", 42, false},
120 {"blah42", 0, false},
121 {"42blah", 42, false},
122 {"blah42blah", 0, false},
123 {"-273.15", -273, false},
124 {"+98.6", 98, false},
130 {"-2147483649", INT_MIN
, false},
131 {"-99999999999", INT_MIN
, false},
132 {"2147483648", INT_MAX
, false},
133 {"99999999999", INT_MAX
, false},
136 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
138 EXPECT_EQ(cases
[i
].success
, StringToInt(cases
[i
].input
, &output
));
139 EXPECT_EQ(cases
[i
].output
, output
);
141 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
143 EXPECT_EQ(cases
[i
].success
, StringToInt(utf16_input
, &output
));
144 EXPECT_EQ(cases
[i
].output
, output
);
147 // One additional test to verify that conversion of numbers in strings with
148 // embedded NUL characters. The NUL and extra data after it should be
149 // interpreted as junk after the number.
150 const char input
[] = "6\06";
151 std::string
input_string(input
, arraysize(input
) - 1);
153 EXPECT_FALSE(StringToInt(input_string
, &output
));
154 EXPECT_EQ(6, output
);
156 string16 utf16_input
= UTF8ToUTF16(input_string
);
158 EXPECT_FALSE(StringToInt(utf16_input
, &output
));
159 EXPECT_EQ(6, output
);
162 const char16 negative_wide_input
[] = { 0xFF4D, '4', '2', 0};
163 EXPECT_FALSE(StringToInt(string16(negative_wide_input
), &output
));
164 EXPECT_EQ(0, output
);
167 TEST(StringNumberConversionsTest
, StringToUint
) {
168 static const struct {
175 {"42\x99", 42, false},
176 {"\x99" "42\x99", 0, false},
177 {"-2147483648", 0, false},
178 {"2147483647", INT_MAX
, true},
182 {"\t\n\v\f\r 42", 42, false},
183 {"blah42", 0, false},
184 {"42blah", 42, false},
185 {"blah42blah", 0, false},
186 {"-273.15", 0, false},
187 {"+98.6", 98, false},
193 {"-2147483649", 0, false},
194 {"-99999999999", 0, false},
195 {"4294967295", UINT_MAX
, true},
196 {"4294967296", UINT_MAX
, false},
197 {"99999999999", UINT_MAX
, false},
200 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
202 EXPECT_EQ(cases
[i
].success
, StringToUint(cases
[i
].input
, &output
));
203 EXPECT_EQ(cases
[i
].output
, output
);
205 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
207 EXPECT_EQ(cases
[i
].success
, StringToUint(utf16_input
, &output
));
208 EXPECT_EQ(cases
[i
].output
, output
);
211 // One additional test to verify that conversion of numbers in strings with
212 // embedded NUL characters. The NUL and extra data after it should be
213 // interpreted as junk after the number.
214 const char input
[] = "6\06";
215 std::string
input_string(input
, arraysize(input
) - 1);
217 EXPECT_FALSE(StringToUint(input_string
, &output
));
218 EXPECT_EQ(6U, output
);
220 string16 utf16_input
= UTF8ToUTF16(input_string
);
222 EXPECT_FALSE(StringToUint(utf16_input
, &output
));
223 EXPECT_EQ(6U, output
);
226 const char16 negative_wide_input
[] = { 0xFF4D, '4', '2', 0};
227 EXPECT_FALSE(StringToUint(string16(negative_wide_input
), &output
));
228 EXPECT_EQ(0U, output
);
231 TEST(StringNumberConversionsTest
, StringToInt64
) {
232 static const struct {
239 {"-2147483648", INT_MIN
, true},
240 {"2147483647", INT_MAX
, true},
241 {"-2147483649", INT64_C(-2147483649), true},
242 {"-99999999999", INT64_C(-99999999999), true},
243 {"2147483648", INT64_C(2147483648), true},
244 {"99999999999", INT64_C(99999999999), true},
245 {"9223372036854775807", kint64max
, true},
246 {"-9223372036854775808", kint64min
, true},
253 {"\t\n\v\f\r 42", 42, false},
254 {"blah42", 0, false},
255 {"42blah", 42, false},
256 {"blah42blah", 0, false},
257 {"-273.15", -273, false},
258 {"+98.6", 98, false},
264 {"-9223372036854775809", kint64min
, false},
265 {"-99999999999999999999", kint64min
, false},
266 {"9223372036854775808", kint64max
, false},
267 {"99999999999999999999", kint64max
, false},
270 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
272 EXPECT_EQ(cases
[i
].success
, StringToInt64(cases
[i
].input
, &output
));
273 EXPECT_EQ(cases
[i
].output
, output
);
275 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
277 EXPECT_EQ(cases
[i
].success
, StringToInt64(utf16_input
, &output
));
278 EXPECT_EQ(cases
[i
].output
, output
);
281 // One additional test to verify that conversion of numbers in strings with
282 // embedded NUL characters. The NUL and extra data after it should be
283 // interpreted as junk after the number.
284 const char input
[] = "6\06";
285 std::string
input_string(input
, arraysize(input
) - 1);
287 EXPECT_FALSE(StringToInt64(input_string
, &output
));
288 EXPECT_EQ(6, output
);
290 string16 utf16_input
= UTF8ToUTF16(input_string
);
292 EXPECT_FALSE(StringToInt64(utf16_input
, &output
));
293 EXPECT_EQ(6, output
);
296 TEST(StringNumberConversionsTest
, StringToUint64
) {
297 static const struct {
304 {"-2147483648", 0, false},
305 {"2147483647", INT_MAX
, true},
306 {"-2147483649", 0, false},
307 {"-99999999999", 0, false},
308 {"2147483648", UINT64_C(2147483648), true},
309 {"99999999999", UINT64_C(99999999999), true},
310 {"9223372036854775807", kint64max
, true},
311 {"-9223372036854775808", 0, false},
318 {"\t\n\v\f\r 42", 42, false},
319 {"blah42", 0, false},
320 {"42blah", 42, false},
321 {"blah42blah", 0, false},
322 {"-273.15", 0, false},
323 {"+98.6", 98, false},
329 {"-9223372036854775809", 0, false},
330 {"-99999999999999999999", 0, false},
331 {"9223372036854775808", UINT64_C(9223372036854775808), true},
332 {"99999999999999999999", kuint64max
, false},
333 {"18446744073709551615", kuint64max
, true},
334 {"18446744073709551616", kuint64max
, false},
337 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
339 EXPECT_EQ(cases
[i
].success
, StringToUint64(cases
[i
].input
, &output
));
340 EXPECT_EQ(cases
[i
].output
, output
);
342 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
344 EXPECT_EQ(cases
[i
].success
, StringToUint64(utf16_input
, &output
));
345 EXPECT_EQ(cases
[i
].output
, output
);
348 // One additional test to verify that conversion of numbers in strings with
349 // embedded NUL characters. The NUL and extra data after it should be
350 // interpreted as junk after the number.
351 const char input
[] = "6\06";
352 std::string
input_string(input
, arraysize(input
) - 1);
354 EXPECT_FALSE(StringToUint64(input_string
, &output
));
355 EXPECT_EQ(6U, output
);
357 string16 utf16_input
= UTF8ToUTF16(input_string
);
359 EXPECT_FALSE(StringToUint64(utf16_input
, &output
));
360 EXPECT_EQ(6U, output
);
363 TEST(StringNumberConversionsTest
, StringToSizeT
) {
364 size_t size_t_max
= std::numeric_limits
<size_t>::max();
365 std::string size_t_max_string
= StringPrintf("%" PRIuS
, size_t_max
);
367 static const struct {
374 {"-2147483648", 0, false},
375 {"2147483647", INT_MAX
, true},
376 {"-2147483649", 0, false},
377 {"-99999999999", 0, false},
378 {"2147483648", 2147483648U, true},
379 #if SIZE_MAX > 4294967295U
380 {"99999999999", 99999999999U, true},
382 {"-9223372036854775808", 0, false},
389 {"\t\n\v\f\r 42", 42, false},
390 {"blah42", 0, false},
391 {"42blah", 42, false},
392 {"blah42blah", 0, false},
393 {"-273.15", 0, false},
394 {"+98.6", 98, false},
400 {"-9223372036854775809", 0, false},
401 {"-99999999999999999999", 0, false},
402 {"999999999999999999999999", size_t_max
, false},
403 {size_t_max_string
, size_t_max
, true},
406 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
408 EXPECT_EQ(cases
[i
].success
, StringToSizeT(cases
[i
].input
, &output
));
409 EXPECT_EQ(cases
[i
].output
, output
);
411 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
413 EXPECT_EQ(cases
[i
].success
, StringToSizeT(utf16_input
, &output
));
414 EXPECT_EQ(cases
[i
].output
, output
);
417 // One additional test to verify that conversion of numbers in strings with
418 // embedded NUL characters. The NUL and extra data after it should be
419 // interpreted as junk after the number.
420 const char input
[] = "6\06";
421 std::string
input_string(input
, arraysize(input
) - 1);
423 EXPECT_FALSE(StringToSizeT(input_string
, &output
));
424 EXPECT_EQ(6U, output
);
426 string16 utf16_input
= UTF8ToUTF16(input_string
);
428 EXPECT_FALSE(StringToSizeT(utf16_input
, &output
));
429 EXPECT_EQ(6U, output
);
432 TEST(StringNumberConversionsTest
, HexStringToInt
) {
433 static const struct {
442 {"7fffffff", INT_MAX
, true},
443 {"-80000000", INT_MIN
, true},
444 {"80000000", INT_MAX
, false}, // Overflow test.
445 {"-80000001", INT_MIN
, false}, // Underflow test.
447 {"-0x42", -66, true},
449 {"0x7fffffff", INT_MAX
, true},
450 {"-0x80000000", INT_MIN
, true},
451 {"-80000000", INT_MIN
, true},
452 {"80000000", INT_MAX
, false}, // Overflow test.
453 {"-80000001", INT_MIN
, false}, // Underflow test.
456 {" 45", 0x45, false},
457 {"\t\n\v\f\r 0x45", 0x45, false},
458 {" 45", 0x45, false},
459 {"45 ", 0x45, false},
460 {"45:", 0x45, false},
461 {"efgh", 0xef, false},
462 {"0xefgh", 0xef, false},
469 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
471 EXPECT_EQ(cases
[i
].success
, HexStringToInt(cases
[i
].input
, &output
));
472 EXPECT_EQ(cases
[i
].output
, output
);
474 // One additional test to verify that conversion of numbers in strings with
475 // embedded NUL characters. The NUL and extra data after it should be
476 // interpreted as junk after the number.
477 const char input
[] = "0xc0ffee\0" "9";
478 std::string
input_string(input
, arraysize(input
) - 1);
480 EXPECT_FALSE(HexStringToInt(input_string
, &output
));
481 EXPECT_EQ(0xc0ffee, output
);
484 TEST(StringNumberConversionsTest
, HexStringToUInt
) {
485 static const struct {
494 {"7fffffff", INT_MAX
, true},
495 {"-80000000", 0, false},
496 {"ffffffff", 0xffffffff, true},
497 {"DeadBeef", 0xdeadbeef, true},
498 {"0x42", 0x42, true},
500 {"+0x42", 0x42, true},
501 {"0x7fffffff", INT_MAX
, true},
502 {"-0x80000000", 0, false},
503 {"0xffffffff", kuint32max
, true},
504 {"0XDeadBeef", 0xdeadbeef, true},
505 {"0x7fffffffffffffff", kuint32max
, false}, // Overflow test.
506 {"-0x8000000000000000", 0, false},
507 {"0x8000000000000000", kuint32max
, false}, // Overflow test.
508 {"-0x8000000000000001", 0, false},
509 {"0xFFFFFFFFFFFFFFFF", kuint32max
, false}, // Overflow test.
510 {"FFFFFFFFFFFFFFFF", kuint32max
, false}, // Overflow test.
511 {"0x0000000000000000", 0, true},
512 {"0000000000000000", 0, true},
513 {"1FFFFFFFFFFFFFFFF", kuint32max
, false}, // Overflow test.
514 {"0x0f", 0x0f, true},
516 {" 45", 0x45, false},
517 {"\t\n\v\f\r 0x45", 0x45, false},
518 {" 45", 0x45, false},
519 {"45 ", 0x45, false},
520 {"45:", 0x45, false},
521 {"efgh", 0xef, false},
522 {"0xefgh", 0xef, false},
529 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
531 EXPECT_EQ(cases
[i
].success
, HexStringToUInt(cases
[i
].input
, &output
));
532 EXPECT_EQ(cases
[i
].output
, output
);
534 // One additional test to verify that conversion of numbers in strings with
535 // embedded NUL characters. The NUL and extra data after it should be
536 // interpreted as junk after the number.
537 const char input
[] = "0xc0ffee\0" "9";
538 std::string
input_string(input
, arraysize(input
) - 1);
540 EXPECT_FALSE(HexStringToUInt(input_string
, &output
));
541 EXPECT_EQ(0xc0ffeeU
, output
);
544 TEST(StringNumberConversionsTest
, HexStringToInt64
) {
545 static const struct {
554 {"40acd88557b", INT64_C(4444444448123), true},
555 {"7fffffff", INT_MAX
, true},
556 {"-80000000", INT_MIN
, true},
557 {"ffffffff", 0xffffffff, true},
558 {"DeadBeef", 0xdeadbeef, true},
560 {"-0x42", -66, true},
562 {"0x40acd88557b", INT64_C(4444444448123), true},
563 {"0x7fffffff", INT_MAX
, true},
564 {"-0x80000000", INT_MIN
, true},
565 {"0xffffffff", 0xffffffff, true},
566 {"0XDeadBeef", 0xdeadbeef, true},
567 {"0x7fffffffffffffff", kint64max
, true},
568 {"-0x8000000000000000", kint64min
, true},
569 {"0x8000000000000000", kint64max
, false}, // Overflow test.
570 {"-0x8000000000000001", kint64min
, false}, // Underflow test.
573 {" 45", 0x45, false},
574 {"\t\n\v\f\r 0x45", 0x45, false},
575 {" 45", 0x45, false},
576 {"45 ", 0x45, false},
577 {"45:", 0x45, false},
578 {"efgh", 0xef, false},
579 {"0xefgh", 0xef, false},
586 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
588 EXPECT_EQ(cases
[i
].success
, HexStringToInt64(cases
[i
].input
, &output
));
589 EXPECT_EQ(cases
[i
].output
, output
);
591 // One additional test to verify that conversion of numbers in strings with
592 // embedded NUL characters. The NUL and extra data after it should be
593 // interpreted as junk after the number.
594 const char input
[] = "0xc0ffee\0" "9";
595 std::string
input_string(input
, arraysize(input
) - 1);
597 EXPECT_FALSE(HexStringToInt64(input_string
, &output
));
598 EXPECT_EQ(0xc0ffee, output
);
601 TEST(StringNumberConversionsTest
, HexStringToUInt64
) {
602 static const struct {
611 {"40acd88557b", INT64_C(4444444448123), true},
612 {"7fffffff", INT_MAX
, true},
613 {"-80000000", 0, false},
614 {"ffffffff", 0xffffffff, true},
615 {"DeadBeef", 0xdeadbeef, true},
619 {"0x40acd88557b", INT64_C(4444444448123), true},
620 {"0x7fffffff", INT_MAX
, true},
621 {"-0x80000000", 0, false},
622 {"0xffffffff", 0xffffffff, true},
623 {"0XDeadBeef", 0xdeadbeef, true},
624 {"0x7fffffffffffffff", kint64max
, true},
625 {"-0x8000000000000000", 0, false},
626 {"0x8000000000000000", UINT64_C(0x8000000000000000), true},
627 {"-0x8000000000000001", 0, false},
628 {"0xFFFFFFFFFFFFFFFF", kuint64max
, true},
629 {"FFFFFFFFFFFFFFFF", kuint64max
, true},
630 {"0x0000000000000000", 0, true},
631 {"0000000000000000", 0, true},
632 {"1FFFFFFFFFFFFFFFF", kuint64max
, false}, // Overflow test.
635 {" 45", 0x45, false},
636 {"\t\n\v\f\r 0x45", 0x45, false},
637 {" 45", 0x45, false},
638 {"45 ", 0x45, false},
639 {"45:", 0x45, false},
640 {"efgh", 0xef, false},
641 {"0xefgh", 0xef, false},
648 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
650 EXPECT_EQ(cases
[i
].success
, HexStringToUInt64(cases
[i
].input
, &output
));
651 EXPECT_EQ(cases
[i
].output
, output
);
653 // One additional test to verify that conversion of numbers in strings with
654 // embedded NUL characters. The NUL and extra data after it should be
655 // interpreted as junk after the number.
656 const char input
[] = "0xc0ffee\0" "9";
657 std::string
input_string(input
, arraysize(input
) - 1);
659 EXPECT_FALSE(HexStringToUInt64(input_string
, &output
));
660 EXPECT_EQ(0xc0ffeeU
, output
);
663 TEST(StringNumberConversionsTest
, HexStringToBytes
) {
664 static const struct {
665 const std::string input
;
670 {"0", "", 0, false}, // odd number of characters fails
671 {"00", "\0", 1, true},
672 {"42", "\x42", 1, true},
673 {"-42", "", 0, false}, // any non-hex value fails
674 {"+42", "", 0, false},
675 {"7fffffff", "\x7f\xff\xff\xff", 4, true},
676 {"80000000", "\x80\0\0\0", 4, true},
677 {"deadbeef", "\xde\xad\xbe\xef", 4, true},
678 {"DeadBeef", "\xde\xad\xbe\xef", 4, true},
679 {"0x42", "", 0, false}, // leading 0x fails (x is not hex)
680 {"0f", "\xf", 1, true},
681 {"45 ", "\x45", 1, false},
682 {"efgh", "\xef", 1, false},
684 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true},
685 {"0123456789ABCDEF012345",
686 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true},
690 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
691 std::vector
<uint8
> output
;
692 std::vector
<uint8
> compare
;
693 EXPECT_EQ(cases
[i
].success
, HexStringToBytes(cases
[i
].input
, &output
)) <<
694 i
<< ": " << cases
[i
].input
;
695 for (size_t j
= 0; j
< cases
[i
].output_len
; ++j
)
696 compare
.push_back(static_cast<uint8
>(cases
[i
].output
[j
]));
697 ASSERT_EQ(output
.size(), compare
.size()) << i
<< ": " << cases
[i
].input
;
698 EXPECT_TRUE(std::equal(output
.begin(), output
.end(), compare
.begin())) <<
699 i
<< ": " << cases
[i
].input
;
703 TEST(StringNumberConversionsTest
, StringToDouble
) {
704 static const struct {
711 {"-42", -42.0, true},
712 {"123.45", 123.45, true},
713 {"-123.45", -123.45, true},
714 {"+123.45", 123.45, true},
715 {"2.99792458e8", 299792458.0, true},
716 {"149597870.691E+3", 149597870691.0, true},
718 {"9e99999999999999999999", HUGE_VAL
, false},
719 {"-9e99999999999999999999", -HUGE_VAL
, false},
720 {"1e-2", 0.01, true},
721 {"42 ", 42.0, false},
722 {" 1e-2", 0.01, false},
723 {"1e-2 ", 0.01, false},
724 {"-1E-7", -0.0000001, true},
725 {"01e02", 100, true},
726 {"2.3e15", 2.3e15
, true},
727 {"\t\n\v\f\r -123.45e2", -12345.0, false},
728 {"+123 e4", 123.0, false},
729 {"123e ", 123.0, false},
730 {"123e", 123.0, false},
731 {" 2.99", 2.99, false},
732 {"1e3.4", 1000.0, false},
733 {"nothing", 0.0, false},
739 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
742 EXPECT_EQ(cases
[i
].success
, StringToDouble(cases
[i
].input
, &output
));
743 if (cases
[i
].success
)
744 EXPECT_EQ(1, errno
) << i
; // confirm that errno is unchanged.
745 EXPECT_DOUBLE_EQ(cases
[i
].output
, output
);
748 // One additional test to verify that conversion of numbers in strings with
749 // embedded NUL characters. The NUL and extra data after it should be
750 // interpreted as junk after the number.
751 const char input
[] = "3.14\0" "159";
752 std::string
input_string(input
, arraysize(input
) - 1);
754 EXPECT_FALSE(StringToDouble(input_string
, &output
));
755 EXPECT_DOUBLE_EQ(3.14, output
);
758 TEST(StringNumberConversionsTest
, DoubleToString
) {
759 static const struct {
761 const char* expected
;
765 {1.33518e+012, "1.33518e+12"},
766 {1.33489e+012, "1.33489e+12"},
767 {1.33505e+012, "1.33505e+12"},
768 {1.33545e+009, "1335450000"},
769 {1.33503e+009, "1335030000"},
772 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
773 EXPECT_EQ(cases
[i
].expected
, DoubleToString(cases
[i
].input
));
776 // The following two values were seen in crashes in the wild.
777 const char input_bytes
[8] = {0, 0, 0, 0, '\xee', '\x6d', '\x73', '\x42'};
779 memcpy(&input
, input_bytes
, arraysize(input_bytes
));
780 EXPECT_EQ("1335179083776", DoubleToString(input
));
781 const char input_bytes2
[8] =
782 {0, 0, 0, '\xa0', '\xda', '\x6c', '\x73', '\x42'};
784 memcpy(&input
, input_bytes2
, arraysize(input_bytes2
));
785 EXPECT_EQ("1334890332160", DoubleToString(input
));
788 TEST(StringNumberConversionsTest
, HexEncode
) {
789 std::string
hex(HexEncode(NULL
, 0));
790 EXPECT_EQ(hex
.length(), 0U);
791 unsigned char bytes
[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
792 hex
= HexEncode(bytes
, sizeof(bytes
));
793 EXPECT_EQ(hex
.compare("01FF02FE038081"), 0);