Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <math.h> | 5 #include <math.h> |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 output = 0; | 128 output = 0; |
| 129 EXPECT_FALSE(StringToInt(utf16_input, &output)); | 129 EXPECT_FALSE(StringToInt(utf16_input, &output)); |
| 130 EXPECT_EQ(6, output); | 130 EXPECT_EQ(6, output); |
| 131 | 131 |
| 132 output = 0; | 132 output = 0; |
| 133 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | 133 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
| 134 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); | 134 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); |
| 135 EXPECT_EQ(0, output); | 135 EXPECT_EQ(0, output); |
| 136 } | 136 } |
| 137 | 137 |
| 138 TEST(StringNumberConversionsTest, StringToUint) { | |
| 139 static const struct { | |
| 140 std::string input; | |
| 141 unsigned output; | |
| 142 bool success; | |
| 143 } cases[] = { | |
| 144 {"0", 0, true}, | |
| 145 {"42", 42, true}, | |
| 146 {"42\x99", 42, false}, | |
| 147 {"\x99" "42\x99", 0, false}, | |
| 148 {"-2147483648", 0, false}, | |
| 149 {"2147483647", INT_MAX, true}, | |
| 150 {"", 0, false}, | |
| 151 {" 42", 42, false}, | |
| 152 {"42 ", 42, false}, | |
| 153 {"\t\n\v\f\r 42", 42, false}, | |
| 154 {"blah42", 0, false}, | |
| 155 {"42blah", 42, false}, | |
| 156 {"blah42blah", 0, false}, | |
| 157 {"-273.15", 0, false}, | |
| 158 {"+98.6", 98, false}, | |
| 159 {"--123", 0, false}, | |
| 160 {"++123", 0, false}, | |
| 161 {"-+123", 0, false}, | |
| 162 {"+-123", 0, false}, | |
| 163 {"-", 0, false}, | |
| 164 {"-2147483649", 0, false}, | |
| 165 {"-99999999999", 0, false}, | |
| 166 {"4294967295", UINT_MAX, true}, | |
| 167 {"4294967296", UINT_MAX, false}, | |
| 168 {"99999999999", UINT_MAX, false}, | |
| 169 }; | |
| 170 | |
| 171 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 172 unsigned output = 0; | |
| 173 EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output)); | |
| 174 EXPECT_EQ(cases[i].output, output); | |
| 175 | |
| 176 string16 utf16_input = UTF8ToUTF16(cases[i].input); | |
| 177 output = 0; | |
| 178 EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output)); | |
| 179 EXPECT_EQ(cases[i].output, output); | |
| 180 } | |
| 181 | |
| 182 // One additional test to verify that conversion of numbers in strings with | |
| 183 // embedded NUL characters. The NUL and extra data after it should be | |
| 184 // interpreted as junk after the number. | |
| 185 const char input[] = "6\06"; | |
| 186 std::string input_string(input, arraysize(input) - 1); | |
| 187 unsigned output; | |
| 188 EXPECT_FALSE(StringToUint(input_string, &output)); | |
| 189 EXPECT_EQ(6U, output); | |
| 190 | |
| 191 string16 utf16_input = UTF8ToUTF16(input_string); | |
| 192 output = 0; | |
| 193 EXPECT_FALSE(StringToUint(utf16_input, &output)); | |
| 194 EXPECT_EQ(6U, output); | |
| 195 | |
| 196 output = 0; | |
| 197 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | |
| 198 EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output)); | |
| 199 EXPECT_EQ(0U, output); | |
| 200 } | |
| 201 | |
| 138 TEST(StringNumberConversionsTest, StringToInt64) { | 202 TEST(StringNumberConversionsTest, StringToInt64) { |
| 139 static const struct { | 203 static const struct { |
| 140 std::string input; | 204 std::string input; |
| 141 int64 output; | 205 int64 output; |
| 142 bool success; | 206 bool success; |
| 143 } cases[] = { | 207 } cases[] = { |
| 144 {"0", 0, true}, | 208 {"0", 0, true}, |
| 145 {"42", 42, true}, | 209 {"42", 42, true}, |
| 146 {"-2147483648", INT_MIN, true}, | 210 {"-2147483648", INT_MIN, true}, |
| 147 {"2147483647", INT_MAX, true}, | 211 {"2147483647", INT_MAX, true}, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 int64 output; | 257 int64 output; |
| 194 EXPECT_FALSE(StringToInt64(input_string, &output)); | 258 EXPECT_FALSE(StringToInt64(input_string, &output)); |
| 195 EXPECT_EQ(6, output); | 259 EXPECT_EQ(6, output); |
| 196 | 260 |
| 197 string16 utf16_input = UTF8ToUTF16(input_string); | 261 string16 utf16_input = UTF8ToUTF16(input_string); |
| 198 output = 0; | 262 output = 0; |
| 199 EXPECT_FALSE(StringToInt64(utf16_input, &output)); | 263 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
| 200 EXPECT_EQ(6, output); | 264 EXPECT_EQ(6, output); |
| 201 } | 265 } |
| 202 | 266 |
| 267 TEST(StringNumberConversionsTest, StringToUint64) { | |
| 268 static const struct { | |
| 269 std::string input; | |
| 270 uint64 output; | |
| 271 bool success; | |
| 272 } cases[] = { | |
| 273 {"0", 0, true}, | |
| 274 {"42", 42, true}, | |
| 275 {"-2147483648", 0, false}, | |
| 276 {"2147483647", INT_MAX, true}, | |
| 277 {"-2147483649", 0, false}, | |
| 278 {"-99999999999", 0, false}, | |
| 279 {"2147483648", GG_INT64_C(2147483648), true}, | |
| 280 {"99999999999", GG_INT64_C(99999999999), true}, | |
| 281 {"9223372036854775807", kint64max, true}, | |
| 282 {"-9223372036854775808", 0, false}, | |
| 283 {"09", 9, true}, | |
| 284 {"-09", 0, false}, | |
| 285 {"", 0, false}, | |
| 286 {" 42", 42, false}, | |
| 287 {"42 ", 42, false}, | |
| 288 {"0x42", 0, false}, | |
| 289 {"\t\n\v\f\r 42", 42, false}, | |
| 290 {"blah42", 0, false}, | |
| 291 {"42blah", 42, false}, | |
| 292 {"blah42blah", 0, false}, | |
| 293 {"-273.15", 0, false}, | |
| 294 {"+98.6", 98, false}, | |
| 295 {"--123", 0, false}, | |
| 296 {"++123", 0, false}, | |
| 297 {"-+123", 0, false}, | |
| 298 {"+-123", 0, false}, | |
| 299 {"-", 0, false}, | |
| 300 {"-9223372036854775809", 0, false}, | |
| 301 {"-99999999999999999999", 0, false}, | |
| 302 {"9223372036854775808", 9223372036854775808U, true}, | |
| 303 {"99999999999999999999", kuint64max, false}, | |
| 304 {"18446744073709551615", kuint64max, true}, | |
| 305 {"18446744073709551616", kuint64max, false}, | |
| 306 }; | |
| 307 | |
| 308 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 309 uint64 output = 0; | |
| 310 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); | |
| 311 EXPECT_EQ(cases[i].output, output); | |
| 312 | |
| 313 string16 utf16_input = UTF8ToUTF16(cases[i].input); | |
| 314 output = 0; | |
| 315 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output)); | |
| 316 EXPECT_EQ(cases[i].output, output); | |
| 317 } | |
| 318 | |
| 319 // One additional test to verify that conversion of numbers in strings with | |
| 320 // embedded NUL characters. The NUL and extra data after it should be | |
| 321 // interpreted as junk after the number. | |
| 322 const char input[] = "6\06"; | |
| 323 std::string input_string(input, arraysize(input) - 1); | |
| 324 uint64 output; | |
| 325 EXPECT_FALSE(StringToUint64(input_string, &output)); | |
| 326 EXPECT_EQ(6U, output); | |
| 327 | |
| 328 string16 utf16_input = UTF8ToUTF16(input_string); | |
| 329 output = 0; | |
| 330 EXPECT_FALSE(StringToUint64(utf16_input, &output)); | |
| 331 EXPECT_EQ(6U, output); | |
| 332 } | |
| 333 | |
| 334 TEST(StringNumberConversionsTest, StringToSizeT) { | |
| 335 static const struct { | |
| 336 std::string input; | |
| 337 size_t output; | |
| 338 bool success; | |
| 339 } cases[] = { | |
| 340 {"0", 0, true}, | |
| 341 {"42", 42, true}, | |
| 342 {"-2147483648", 0, false}, | |
| 343 {"2147483647", INT_MAX, true}, | |
| 344 {"-2147483649", 0, false}, | |
| 345 {"-99999999999", 0, false}, | |
| 346 {"2147483648", static_cast<size_t>(2147483648), true}, | |
| 347 {"99999999999", static_cast<size_t>(99999999999), true}, | |
|
Vitaly Buka corp
2013/05/18 08:55:17
it cant work on 32bit platform where size_t 32bit
Mostyn Bramley-Moore
2013/05/18 10:49:06
Thanks for the review.
On 2013/05/18 08:55:17, Vi
| |
| 348 {"9223372036854775807", static_cast<size_t>(kint64max), true}, | |
| 349 {"-9223372036854775808", 0, false}, | |
| 350 {"09", 9, true}, | |
| 351 {"-09", 0, false}, | |
| 352 {"", 0, false}, | |
| 353 {" 42", 42, false}, | |
| 354 {"42 ", 42, false}, | |
| 355 {"0x42", 0, false}, | |
| 356 {"\t\n\v\f\r 42", 42, false}, | |
| 357 {"blah42", 0, false}, | |
| 358 {"42blah", 42, false}, | |
| 359 {"blah42blah", 0, false}, | |
| 360 {"-273.15", 0, false}, | |
| 361 {"+98.6", 98, false}, | |
| 362 {"--123", 0, false}, | |
| 363 {"++123", 0, false}, | |
| 364 {"-+123", 0, false}, | |
| 365 {"+-123", 0, false}, | |
| 366 {"-", 0, false}, | |
| 367 {"-9223372036854775809", 0, false}, | |
| 368 {"-99999999999999999999", 0, false}, | |
| 369 {"999999999999999999999999", std::numeric_limits<size_t>::max(), false}, | |
| 370 }; | |
| 371 | |
| 372 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 373 size_t output = 0; | |
| 374 EXPECT_EQ(cases[i].success, StringToSizeT(cases[i].input, &output)); | |
| 375 EXPECT_EQ(cases[i].output, output); | |
| 376 | |
| 377 string16 utf16_input = UTF8ToUTF16(cases[i].input); | |
| 378 output = 0; | |
| 379 EXPECT_EQ(cases[i].success, StringToSizeT(utf16_input, &output)); | |
| 380 EXPECT_EQ(cases[i].output, output); | |
| 381 } | |
| 382 | |
| 383 // One additional test to verify that conversion of numbers in strings with | |
| 384 // embedded NUL characters. The NUL and extra data after it should be | |
| 385 // interpreted as junk after the number. | |
| 386 const char input[] = "6\06"; | |
| 387 std::string input_string(input, arraysize(input) - 1); | |
| 388 size_t output; | |
| 389 EXPECT_FALSE(StringToSizeT(input_string, &output)); | |
| 390 EXPECT_EQ(6U, output); | |
| 391 | |
| 392 string16 utf16_input = UTF8ToUTF16(input_string); | |
| 393 output = 0; | |
| 394 EXPECT_FALSE(StringToSizeT(utf16_input, &output)); | |
| 395 EXPECT_EQ(6U, output); | |
| 396 } | |
| 397 | |
| 203 TEST(StringNumberConversionsTest, HexStringToInt) { | 398 TEST(StringNumberConversionsTest, HexStringToInt) { |
| 204 static const struct { | 399 static const struct { |
| 205 std::string input; | 400 std::string input; |
| 206 int64 output; | 401 int64 output; |
| 207 bool success; | 402 bool success; |
| 208 } cases[] = { | 403 } cases[] = { |
| 209 {"0", 0, true}, | 404 {"0", 0, true}, |
| 210 {"42", 66, true}, | 405 {"42", 66, true}, |
| 211 {"-42", -66, true}, | 406 {"-42", -66, true}, |
| 212 {"+42", 66, true}, | 407 {"+42", 66, true}, |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 495 | 690 |
| 496 TEST(StringNumberConversionsTest, HexEncode) { | 691 TEST(StringNumberConversionsTest, HexEncode) { |
| 497 std::string hex(HexEncode(NULL, 0)); | 692 std::string hex(HexEncode(NULL, 0)); |
| 498 EXPECT_EQ(hex.length(), 0U); | 693 EXPECT_EQ(hex.length(), 0U); |
| 499 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 694 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 500 hex = HexEncode(bytes, sizeof(bytes)); | 695 hex = HexEncode(bytes, sizeof(bytes)); |
| 501 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 696 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 502 } | 697 } |
| 503 | 698 |
| 504 } // namespace base | 699 } // namespace base |
| OLD | NEW |