1 // Copyright (c) 2010 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
) {
108 const char* ascii_chars
= cases
[i
].input
.c_str();
110 EXPECT_EQ(cases
[i
].success
, StringToInt(cases
[i
].input
, &output
));
111 EXPECT_EQ(cases
[i
].output
, output
);
113 EXPECT_EQ(cases
[i
].success
, StringToInt(cases
[i
].input
.begin(),
114 cases
[i
].input
.end(),
116 EXPECT_EQ(cases
[i
].output
, output
);
118 EXPECT_EQ(cases
[i
].success
, StringToInt(
119 ascii_chars
, ascii_chars
+ cases
[i
].input
.length(), &output
));
120 EXPECT_EQ(cases
[i
].output
, output
);
122 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
123 const char16
* utf16_chars
= utf16_input
.c_str();
125 EXPECT_EQ(cases
[i
].success
, StringToInt(utf16_input
, &output
));
126 EXPECT_EQ(cases
[i
].output
, output
);
128 EXPECT_EQ(cases
[i
].success
, StringToInt(utf16_input
.begin(),
131 EXPECT_EQ(cases
[i
].output
, output
);
133 EXPECT_EQ(cases
[i
].success
, StringToInt(
134 utf16_chars
, utf16_chars
+ utf16_input
.length(), &output
));
135 EXPECT_EQ(cases
[i
].output
, output
);
138 // One additional test to verify that conversion of numbers in strings with
139 // embedded NUL characters. The NUL and extra data after it should be
140 // interpreted as junk after the number.
141 const char input
[] = "6\06";
142 std::string
input_string(input
, arraysize(input
) - 1);
144 EXPECT_FALSE(StringToInt(input_string
, &output
));
145 EXPECT_EQ(6, output
);
147 EXPECT_FALSE(StringToInt(input_string
.begin(), input_string
.end(), &output
));
148 EXPECT_EQ(6, output
);
150 EXPECT_FALSE(StringToInt(input
, input
+ arraysize(input
), &output
));
151 EXPECT_EQ(6, output
);
153 string16 utf16_input
= UTF8ToUTF16(input_string
);
154 const char16
* utf16_chars
= utf16_input
.c_str();
156 EXPECT_FALSE(StringToInt(utf16_input
, &output
));
157 EXPECT_EQ(6, output
);
159 EXPECT_FALSE(StringToInt(utf16_input
.begin(), utf16_input
.end(), &output
));
160 EXPECT_EQ(6, output
);
162 EXPECT_FALSE(StringToInt(utf16_chars
,
163 utf16_chars
+ utf16_input
.length(),
165 EXPECT_EQ(6, output
);
168 const char16 negative_wide_input
[] = { 0xFF4D, '4', '2', 0};
169 EXPECT_FALSE(StringToInt(string16(negative_wide_input
), &output
));
170 EXPECT_EQ(0, output
);
173 TEST(StringNumberConversionsTest
, StringToInt64
) {
174 static const struct {
181 {"-2147483648", INT_MIN
, true},
182 {"2147483647", INT_MAX
, true},
183 {"-2147483649", GG_INT64_C(-2147483649), true},
184 {"-99999999999", GG_INT64_C(-99999999999), true},
185 {"2147483648", GG_INT64_C(2147483648), true},
186 {"99999999999", GG_INT64_C(99999999999), true},
187 {"9223372036854775807", kint64max
, true},
188 {"-9223372036854775808", kint64min
, true},
195 {"\t\n\v\f\r 42", 42, false},
196 {"blah42", 0, false},
197 {"42blah", 42, false},
198 {"blah42blah", 0, false},
199 {"-273.15", -273, false},
200 {"+98.6", 98, false},
206 {"-9223372036854775809", kint64min
, false},
207 {"-99999999999999999999", kint64min
, false},
208 {"9223372036854775808", kint64max
, false},
209 {"99999999999999999999", kint64max
, false},
212 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
213 const char* ascii_chars
= cases
[i
].input
.c_str();
215 EXPECT_EQ(cases
[i
].success
, StringToInt64(cases
[i
].input
, &output
));
216 EXPECT_EQ(cases
[i
].output
, output
);
218 EXPECT_EQ(cases
[i
].success
, StringToInt64(cases
[i
].input
.begin(),
219 cases
[i
].input
.end(),
221 EXPECT_EQ(cases
[i
].output
, output
);
223 EXPECT_EQ(cases
[i
].success
, StringToInt64(
224 ascii_chars
, ascii_chars
+ cases
[i
].input
.length(), &output
));
225 EXPECT_EQ(cases
[i
].output
, output
);
227 string16 utf16_input
= UTF8ToUTF16(cases
[i
].input
);
228 const char16
* utf16_chars
= utf16_input
.c_str();
230 EXPECT_EQ(cases
[i
].success
, StringToInt64(utf16_input
, &output
));
231 EXPECT_EQ(cases
[i
].output
, output
);
233 EXPECT_EQ(cases
[i
].success
, StringToInt64(utf16_input
.begin(),
236 EXPECT_EQ(cases
[i
].output
, output
);
238 EXPECT_EQ(cases
[i
].success
, StringToInt64(
239 utf16_chars
, utf16_chars
+ utf16_input
.length(), &output
));
240 EXPECT_EQ(cases
[i
].output
, output
);
243 // One additional test to verify that conversion of numbers in strings with
244 // embedded NUL characters. The NUL and extra data after it should be
245 // interpreted as junk after the number.
246 const char input
[] = "6\06";
247 std::string
input_string(input
, arraysize(input
) - 1);
249 EXPECT_FALSE(StringToInt64(input_string
, &output
));
250 EXPECT_EQ(6, output
);
252 EXPECT_FALSE(StringToInt64(input_string
.begin(),
255 EXPECT_EQ(6, output
);
257 EXPECT_FALSE(StringToInt64(input
, input
+ arraysize(input
), &output
));
258 EXPECT_EQ(6, output
);
260 string16 utf16_input
= UTF8ToUTF16(input_string
);
261 const char16
* utf16_chars
= utf16_input
.c_str();
263 EXPECT_FALSE(StringToInt64(utf16_input
, &output
));
264 EXPECT_EQ(6, output
);
266 EXPECT_FALSE(StringToInt64(utf16_input
.begin(), utf16_input
.end(), &output
));
267 EXPECT_EQ(6, output
);
269 EXPECT_FALSE(StringToInt64(utf16_chars
,
270 utf16_chars
+ utf16_input
.length(),
272 EXPECT_EQ(6, output
);
275 TEST(StringNumberConversionsTest
, HexStringToInt
) {
276 static const struct {
285 {"7fffffff", INT_MAX
, true},
286 {"80000000", INT_MIN
, true},
287 {"ffffffff", -1, true},
288 {"DeadBeef", 0xdeadbeef, true},
290 {"-0x42", -66, true},
292 {"0x7fffffff", INT_MAX
, true},
293 {"0x80000000", INT_MIN
, true},
294 {"0xffffffff", -1, true},
295 {"0XDeadBeef", 0xdeadbeef, true},
298 {" 45", 0x45, false},
299 {"\t\n\v\f\r 0x45", 0x45, false},
300 {" 45", 0x45, false},
301 {"45 ", 0x45, false},
302 {"45:", 0x45, false},
303 {"efgh", 0xef, false},
304 {"0xefgh", 0xef, false},
306 {"100000000", -1, false}, // don't care about |output|, just |success|
311 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
312 const char* ascii_chars
= cases
[i
].input
.c_str();
314 EXPECT_EQ(cases
[i
].success
, HexStringToInt(cases
[i
].input
, &output
));
315 EXPECT_EQ(cases
[i
].output
, output
);
317 EXPECT_EQ(cases
[i
].success
, HexStringToInt(cases
[i
].input
.begin(),
318 cases
[i
].input
.end(),
320 EXPECT_EQ(cases
[i
].output
, output
);
322 EXPECT_EQ(cases
[i
].success
, HexStringToInt(
323 ascii_chars
, ascii_chars
+ cases
[i
].input
.length(), &output
));
324 EXPECT_EQ(cases
[i
].output
, output
);
326 // One additional test to verify that conversion of numbers in strings with
327 // embedded NUL characters. The NUL and extra data after it should be
328 // interpreted as junk after the number.
329 const char input
[] = "0xc0ffee\09";
330 std::string
input_string(input
, arraysize(input
) - 1);
332 EXPECT_FALSE(HexStringToInt(input_string
, &output
));
333 EXPECT_EQ(0xc0ffee, output
);
335 EXPECT_FALSE(HexStringToInt(input_string
.begin(),
338 EXPECT_EQ(0xc0ffee, output
);
340 EXPECT_FALSE(HexStringToInt(input
, input
+ arraysize(input
), &output
));
341 EXPECT_EQ(0xc0ffee, output
);
344 TEST(StringNumberConversionsTest
, HexStringToBytes
) {
345 static const struct {
346 const std::string input
;
351 {"0", "", 0, false}, // odd number of characters fails
352 {"00", "\0", 1, true},
353 {"42", "\x42", 1, true},
354 {"-42", "", 0, false}, // any non-hex value fails
355 {"+42", "", 0, false},
356 {"7fffffff", "\x7f\xff\xff\xff", 4, true},
357 {"80000000", "\x80\0\0\0", 4, true},
358 {"deadbeef", "\xde\xad\xbe\xef", 4, true},
359 {"DeadBeef", "\xde\xad\xbe\xef", 4, true},
360 {"0x42", "", 0, false}, // leading 0x fails (x is not hex)
361 {"0f", "\xf", 1, true},
362 {"45 ", "\x45", 1, false},
363 {"efgh", "\xef", 1, false},
365 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true},
366 {"0123456789ABCDEF012345",
367 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true},
371 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
372 std::vector
<uint8
> output
;
373 std::vector
<uint8
> compare
;
374 EXPECT_EQ(cases
[i
].success
, HexStringToBytes(cases
[i
].input
, &output
)) <<
375 i
<< ": " << cases
[i
].input
;
376 for (size_t j
= 0; j
< cases
[i
].output_len
; ++j
)
377 compare
.push_back(static_cast<uint8
>(cases
[i
].output
[j
]));
378 ASSERT_EQ(output
.size(), compare
.size()) << i
<< ": " << cases
[i
].input
;
379 EXPECT_TRUE(std::equal(output
.begin(), output
.end(), compare
.begin())) <<
380 i
<< ": " << cases
[i
].input
;
384 TEST(StringNumberConversionsTest
, StringToDouble
) {
385 static const struct {
392 {"-42", -42.0, true},
393 {"123.45", 123.45, true},
394 {"-123.45", -123.45, true},
395 {"+123.45", 123.45, true},
396 {"2.99792458e8", 299792458.0, true},
397 {"149597870.691E+3", 149597870691.0, true},
399 {"9e99999999999999999999", HUGE_VAL
, false},
400 {"-9e99999999999999999999", -HUGE_VAL
, false},
401 {"1e-2", 0.01, true},
402 {"42 ", 42.0, false},
403 {" 1e-2", 0.01, false},
404 {"1e-2 ", 0.01, false},
405 {"-1E-7", -0.0000001, true},
406 {"01e02", 100, true},
407 {"2.3e15", 2.3e15
, true},
408 {"\t\n\v\f\r -123.45e2", -12345.0, false},
409 {"+123 e4", 123.0, false},
410 {"123e ", 123.0, false},
411 {"123e", 123.0, false},
412 {" 2.99", 2.99, false},
413 {"1e3.4", 1000.0, false},
414 {"nothing", 0.0, false},
420 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
422 EXPECT_EQ(cases
[i
].success
, StringToDouble(cases
[i
].input
, &output
));
423 EXPECT_DOUBLE_EQ(cases
[i
].output
, output
);
426 // One additional test to verify that conversion of numbers in strings with
427 // embedded NUL characters. The NUL and extra data after it should be
428 // interpreted as junk after the number.
429 const char input
[] = "3.14\0159";
430 std::string
input_string(input
, arraysize(input
) - 1);
432 EXPECT_FALSE(StringToDouble(input_string
, &output
));
433 EXPECT_DOUBLE_EQ(3.14, output
);
436 TEST(StringNumberConversionsTest
, HexEncode
) {
437 std::string
hex(HexEncode(NULL
, 0));
438 EXPECT_EQ(hex
.length(), 0U);
439 unsigned char bytes
[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
440 hex
= HexEncode(bytes
, sizeof(bytes
));
441 EXPECT_EQ(hex
.compare("01FF02FE038081"), 0);