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.
9 #include "base/string_number_conversions.h"
10 #include "base/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 template <typename INT
>
18 struct IntToStringTest
{
20 const char* sexpected
;
21 const char* uexpected
;
26 TEST(StringNumberConversionsTest
, IntToString
) {
27 static const IntToStringTest
<int> int_tests
[] = {
29 { -1, "-1", "4294967295" },
30 { std::numeric_limits
<int>::max(), "2147483647", "2147483647" },
31 { std::numeric_limits
<int>::min(), "-2147483648", "2147483648" },
33 static const IntToStringTest
<int64
> int64_tests
[] = {
35 { -1, "-1", "18446744073709551615" },
36 { std::numeric_limits
<int64
>::max(),
37 "9223372036854775807",
38 "9223372036854775807", },
39 { std::numeric_limits
<int64
>::min(),
40 "-9223372036854775808",
41 "9223372036854775808" },
44 for (size_t i
= 0; i
< arraysize(int_tests
); ++i
) {
45 const IntToStringTest
<int>* test
= &int_tests
[i
];
46 EXPECT_EQ(IntToString(test
->num
), test
->sexpected
);
47 EXPECT_EQ(IntToString16(test
->num
), UTF8ToUTF16(test
->sexpected
));
48 EXPECT_EQ(UintToString(test
->num
), test
->uexpected
);
49 EXPECT_EQ(UintToString16(test
->num
), UTF8ToUTF16(test
->uexpected
));
51 for (size_t i
= 0; i
< arraysize(int64_tests
); ++i
) {
52 const IntToStringTest
<int64
>* test
= &int64_tests
[i
];
53 EXPECT_EQ(Int64ToString(test
->num
), test
->sexpected
);
54 EXPECT_EQ(Int64ToString16(test
->num
), UTF8ToUTF16(test
->sexpected
));
55 EXPECT_EQ(Uint64ToString(test
->num
), test
->uexpected
);
56 EXPECT_EQ(Uint64ToString16(test
->num
), UTF8ToUTF16(test
->uexpected
));
60 TEST(StringNumberConversionsTest
, Uint64ToString
) {
67 {INT_MAX
, "2147483647"},
68 {kuint64max
, "18446744073709551615"},
71 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
)
72 EXPECT_EQ(cases
[i
].output
, Uint64ToString(cases
[i
].input
));
75 TEST(StringNumberConversionsTest
, StringToInt
) {
83 {"42\x99", 42, false},
84 {"\x99" "42\x99", 0, false},
85 {"-2147483648", INT_MIN
, true},
86 {"2147483647", INT_MAX
, true},
90 {"\t\n\v\f\r 42", 42, false},
92 {"42blah", 42, false},
93 {"blah42blah", 0, false},
94 {"-273.15", -273, false},
101 {"-2147483649", INT_MIN
, false},
102 {"-99999999999", INT_MIN
, false},
103 {"2147483648", INT_MAX
, false},
104 {"99999999999", INT_MAX
, false},
107 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
109 EXPECT_EQ(cases
[i
].success
, StringToInt(cases
[i
].input
, &output
));
110 EXPECT_EQ(cases
[i
].output
, output
);
112 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
114 EXPECT_EQ(cases
[i
].success
, StringToInt(utf16_input
, &output
));
115 EXPECT_EQ(cases
[i
].output
, output
);
118 // One additional test to verify that conversion of numbers in strings with
119 // embedded NUL characters. The NUL and extra data after it should be
120 // interpreted as junk after the number.
121 const char input
[] = "6\06";
122 std::string
input_string(input
, arraysize(input
) - 1);
124 EXPECT_FALSE(StringToInt(input_string
, &output
));
125 EXPECT_EQ(6, output
);
127 string16 utf16_input
= UTF8ToUTF16(input_string
);
129 EXPECT_FALSE(StringToInt(utf16_input
, &output
));
130 EXPECT_EQ(6, output
);
133 const char16 negative_wide_input
[] = { 0xFF4D, '4', '2', 0};
134 EXPECT_FALSE(StringToInt(string16(negative_wide_input
), &output
));
135 EXPECT_EQ(0, output
);
138 TEST(StringNumberConversionsTest
, StringToInt64
) {
139 static const struct {
146 {"-2147483648", INT_MIN
, true},
147 {"2147483647", INT_MAX
, true},
148 {"-2147483649", GG_INT64_C(-2147483649), true},
149 {"-99999999999", GG_INT64_C(-99999999999), true},
150 {"2147483648", GG_INT64_C(2147483648), true},
151 {"99999999999", GG_INT64_C(99999999999), true},
152 {"9223372036854775807", kint64max
, true},
153 {"-9223372036854775808", kint64min
, true},
160 {"\t\n\v\f\r 42", 42, false},
161 {"blah42", 0, false},
162 {"42blah", 42, false},
163 {"blah42blah", 0, false},
164 {"-273.15", -273, false},
165 {"+98.6", 98, false},
171 {"-9223372036854775809", kint64min
, false},
172 {"-99999999999999999999", kint64min
, false},
173 {"9223372036854775808", kint64max
, false},
174 {"99999999999999999999", kint64max
, false},
177 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
179 EXPECT_EQ(cases
[i
].success
, StringToInt64(cases
[i
].input
, &output
));
180 EXPECT_EQ(cases
[i
].output
, output
);
182 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
184 EXPECT_EQ(cases
[i
].success
, StringToInt64(utf16_input
, &output
));
185 EXPECT_EQ(cases
[i
].output
, output
);
188 // One additional test to verify that conversion of numbers in strings with
189 // embedded NUL characters. The NUL and extra data after it should be
190 // interpreted as junk after the number.
191 const char input
[] = "6\06";
192 std::string
input_string(input
, arraysize(input
) - 1);
194 EXPECT_FALSE(StringToInt64(input_string
, &output
));
195 EXPECT_EQ(6, output
);
197 string16 utf16_input
= UTF8ToUTF16(input_string
);
199 EXPECT_FALSE(StringToInt64(utf16_input
, &output
));
200 EXPECT_EQ(6, output
);
203 TEST(StringNumberConversionsTest
, HexStringToInt
) {
204 static const struct {
213 {"7fffffff", INT_MAX
, true},
214 {"-80000000", INT_MIN
, true},
215 {"80000000", INT_MAX
, false}, // Overflow test.
216 {"-80000001", INT_MIN
, false}, // Underflow test.
218 {"-0x42", -66, true},
220 {"0x7fffffff", INT_MAX
, true},
221 {"-0x80000000", INT_MIN
, true},
222 {"-80000000", INT_MIN
, true},
223 {"80000000", INT_MAX
, false}, // Overflow test.
224 {"-80000001", INT_MIN
, false}, // Underflow test.
227 {" 45", 0x45, false},
228 {"\t\n\v\f\r 0x45", 0x45, false},
229 {" 45", 0x45, false},
230 {"45 ", 0x45, false},
231 {"45:", 0x45, false},
232 {"efgh", 0xef, false},
233 {"0xefgh", 0xef, false},
240 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
242 EXPECT_EQ(cases
[i
].success
, HexStringToInt(cases
[i
].input
, &output
));
243 EXPECT_EQ(cases
[i
].output
, output
);
245 // One additional test to verify that conversion of numbers in strings with
246 // embedded NUL characters. The NUL and extra data after it should be
247 // interpreted as junk after the number.
248 const char input
[] = "0xc0ffee\09";
249 std::string
input_string(input
, arraysize(input
) - 1);
251 EXPECT_FALSE(HexStringToInt(input_string
, &output
));
252 EXPECT_EQ(0xc0ffee, output
);
255 TEST(StringNumberConversionsTest
, HexStringToInt64
) {
256 static const struct {
265 {"40acd88557b", GG_INT64_C(4444444448123), true},
266 {"7fffffff", INT_MAX
, true},
267 {"-80000000", INT_MIN
, true},
268 {"ffffffff", 0xffffffff, true},
269 {"DeadBeef", 0xdeadbeef, true},
271 {"-0x42", -66, true},
273 {"0x40acd88557b", GG_INT64_C(4444444448123), true},
274 {"0x7fffffff", INT_MAX
, true},
275 {"-0x80000000", INT_MIN
, true},
276 {"0xffffffff", 0xffffffff, true},
277 {"0XDeadBeef", 0xdeadbeef, true},
278 {"0x7fffffffffffffff", kint64max
, true},
279 {"-0x8000000000000000", kint64min
, true},
280 {"0x8000000000000000", kint64max
, false}, // Overflow test.
281 {"-0x8000000000000001", kint64min
, false}, // Underflow test.
284 {" 45", 0x45, false},
285 {"\t\n\v\f\r 0x45", 0x45, false},
286 {" 45", 0x45, false},
287 {"45 ", 0x45, false},
288 {"45:", 0x45, false},
289 {"efgh", 0xef, false},
290 {"0xefgh", 0xef, false},
297 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
299 EXPECT_EQ(cases
[i
].success
, HexStringToInt64(cases
[i
].input
, &output
));
300 EXPECT_EQ(cases
[i
].output
, output
);
302 // One additional test to verify that conversion of numbers in strings with
303 // embedded NUL characters. The NUL and extra data after it should be
304 // interpreted as junk after the number.
305 const char input
[] = "0xc0ffee\09";
306 std::string
input_string(input
, arraysize(input
) - 1);
308 EXPECT_FALSE(HexStringToInt64(input_string
, &output
));
309 EXPECT_EQ(0xc0ffee, output
);
312 TEST(StringNumberConversionsTest
, HexStringToBytes
) {
313 static const struct {
314 const std::string input
;
319 {"0", "", 0, false}, // odd number of characters fails
320 {"00", "\0", 1, true},
321 {"42", "\x42", 1, true},
322 {"-42", "", 0, false}, // any non-hex value fails
323 {"+42", "", 0, false},
324 {"7fffffff", "\x7f\xff\xff\xff", 4, true},
325 {"80000000", "\x80\0\0\0", 4, true},
326 {"deadbeef", "\xde\xad\xbe\xef", 4, true},
327 {"DeadBeef", "\xde\xad\xbe\xef", 4, true},
328 {"0x42", "", 0, false}, // leading 0x fails (x is not hex)
329 {"0f", "\xf", 1, true},
330 {"45 ", "\x45", 1, false},
331 {"efgh", "\xef", 1, false},
333 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true},
334 {"0123456789ABCDEF012345",
335 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true},
339 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
340 std::vector
<uint8
> output
;
341 std::vector
<uint8
> compare
;
342 EXPECT_EQ(cases
[i
].success
, HexStringToBytes(cases
[i
].input
, &output
)) <<
343 i
<< ": " << cases
[i
].input
;
344 for (size_t j
= 0; j
< cases
[i
].output_len
; ++j
)
345 compare
.push_back(static_cast<uint8
>(cases
[i
].output
[j
]));
346 ASSERT_EQ(output
.size(), compare
.size()) << i
<< ": " << cases
[i
].input
;
347 EXPECT_TRUE(std::equal(output
.begin(), output
.end(), compare
.begin())) <<
348 i
<< ": " << cases
[i
].input
;
352 TEST(StringNumberConversionsTest
, StringToDouble
) {
353 static const struct {
360 {"-42", -42.0, true},
361 {"123.45", 123.45, true},
362 {"-123.45", -123.45, true},
363 {"+123.45", 123.45, true},
364 {"2.99792458e8", 299792458.0, true},
365 {"149597870.691E+3", 149597870691.0, true},
367 {"9e99999999999999999999", HUGE_VAL
, false},
368 {"-9e99999999999999999999", -HUGE_VAL
, false},
369 {"1e-2", 0.01, true},
370 {"42 ", 42.0, false},
371 {" 1e-2", 0.01, false},
372 {"1e-2 ", 0.01, false},
373 {"-1E-7", -0.0000001, true},
374 {"01e02", 100, true},
375 {"2.3e15", 2.3e15
, true},
376 {"\t\n\v\f\r -123.45e2", -12345.0, false},
377 {"+123 e4", 123.0, false},
378 {"123e ", 123.0, false},
379 {"123e", 123.0, false},
380 {" 2.99", 2.99, false},
381 {"1e3.4", 1000.0, false},
382 {"nothing", 0.0, false},
388 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
390 EXPECT_EQ(cases
[i
].success
, StringToDouble(cases
[i
].input
, &output
));
391 EXPECT_DOUBLE_EQ(cases
[i
].output
, output
);
394 // One additional test to verify that conversion of numbers in strings with
395 // embedded NUL characters. The NUL and extra data after it should be
396 // interpreted as junk after the number.
397 const char input
[] = "3.14\0159";
398 std::string
input_string(input
, arraysize(input
) - 1);
400 EXPECT_FALSE(StringToDouble(input_string
, &output
));
401 EXPECT_DOUBLE_EQ(3.14, output
);
404 TEST(StringNumberConversionsTest
, DoubleToString
) {
405 static const struct {
407 const char* expected
;
411 {1.33518e+012, "1.33518e+12"},
412 {1.33489e+012, "1.33489e+12"},
413 {1.33505e+012, "1.33505e+12"},
414 {1.33545e+009, "1335450000"},
415 {1.33503e+009, "1335030000"},
418 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
419 EXPECT_EQ(cases
[i
].expected
, DoubleToString(cases
[i
].input
));
422 // The following two values were seen in crashes in the wild.
423 const char input_bytes
[8] = {0, 0, 0, 0, '\xee', '\x6d', '\x73', '\x42'};
425 memcpy(&input
, input_bytes
, arraysize(input_bytes
));
426 EXPECT_EQ("1335179083776", DoubleToString(input
));
427 const char input_bytes2
[8] =
428 {0, 0, 0, '\xa0', '\xda', '\x6c', '\x73', '\x42'};
430 memcpy(&input
, input_bytes2
, arraysize(input_bytes2
));
431 EXPECT_EQ("1334890332160", DoubleToString(input
));
434 TEST(StringNumberConversionsTest
, HexEncode
) {
435 std::string
hex(HexEncode(NULL
, 0));
436 EXPECT_EQ(hex
.length(), 0U);
437 unsigned char bytes
[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
438 hex
= HexEncode(bytes
, sizeof(bytes
));
439 EXPECT_EQ(hex
.compare("01FF02FE038081"), 0);