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 299 matching lines...) Loading... |
310 } | 310 } |
311 | 311 |
312 TEST(StringNumberConversionsTest, HexStringToUInt64) { | 312 TEST(StringNumberConversionsTest, HexStringToUInt64) { |
313 static const struct { | 313 static const struct { |
314 std::string input; | 314 std::string input; |
315 uint64 output; | 315 uint64 output; |
316 bool success; | 316 bool success; |
317 } cases[] = { | 317 } cases[] = { |
318 {"0", 0, true}, | 318 {"0", 0, true}, |
319 {"42", 66, true}, | 319 {"42", 66, true}, |
320 {"-42", -66, true}, | 320 {"-42", static_cast<uint64>(-66), false}, |
321 {"+42", 66, true}, | 321 {"+42", 66, true}, |
322 {"40acd88557b", GG_INT64_C(4444444448123), true}, | 322 {"40acd88557b", GG_INT64_C(4444444448123), true}, |
323 {"7fffffff", INT_MAX, true}, | 323 {"7fffffff", INT_MAX, true}, |
324 {"-80000000", INT_MIN, true}, | 324 {"-80000000", static_cast<uint64>(INT_MIN), false}, |
325 {"ffffffff", 0xffffffff, true}, | 325 {"ffffffff", 0xffffffff, true}, |
326 {"DeadBeef", 0xdeadbeef, true}, | 326 {"DeadBeef", 0xdeadbeef, true}, |
327 {"0x42", 66, true}, | 327 {"0x42", 66, true}, |
328 {"-0x42", -66, true}, | 328 {"-0x42", static_cast<uint64>(-66), false}, |
329 {"+0x42", 66, true}, | 329 {"+0x42", 66, true}, |
330 {"0x40acd88557b", GG_INT64_C(4444444448123), true}, | 330 {"0x40acd88557b", GG_INT64_C(4444444448123), true}, |
331 {"0x7fffffff", INT_MAX, true}, | 331 {"0x7fffffff", INT_MAX, true}, |
332 {"-0x80000000", INT_MIN, true}, | 332 {"-0x80000000", static_cast<uint64>(INT_MIN), false}, |
333 {"0xffffffff", 0xffffffff, true}, | 333 {"0xffffffff", 0xffffffff, true}, |
334 {"0XDeadBeef", 0xdeadbeef, true}, | 334 {"0XDeadBeef", 0xdeadbeef, true}, |
335 {"0x7fffffffffffffff", kint64max, true}, | 335 {"0x7fffffffffffffff", kint64max, true}, |
336 {"-0x8000000000000000", GG_UINT64_C(0x8000000000000000), true}, | 336 {"-0x8000000000000000", GG_UINT64_C(0x8000000000000000), false}, |
337 {"0x8000000000000000", GG_UINT64_C(0x8000000000000000), true}, | 337 {"0x8000000000000000", GG_UINT64_C(0x8000000000000000), true}, |
338 {"-0x8000000000000001", GG_UINT64_C(0x7fffffffffffffff), true}, | 338 {"-0x8000000000000001", GG_UINT64_C(0x7fffffffffffffff), false}, |
339 {"0xFFFFFFFFFFFFFFFF", kuint64max, true}, | 339 {"0xFFFFFFFFFFFFFFFF", kuint64max, true}, |
340 {"FFFFFFFFFFFFFFFF", kuint64max, true}, | 340 {"FFFFFFFFFFFFFFFF", kuint64max, true}, |
341 {"0x0000000000000000", 0, true}, | 341 {"0x0000000000000000", 0, true}, |
342 {"0000000000000000", 0, true}, | 342 {"0000000000000000", 0, true}, |
343 {"1FFFFFFFFFFFFFFFF", kuint64max, false}, // Overflow test. | 343 {"1FFFFFFFFFFFFFFFF", kuint64max, false}, // Overflow test. |
344 {"0x0f", 15, true}, | 344 {"0x0f", 15, true}, |
345 {"0f", 15, true}, | 345 {"0f", 15, true}, |
346 {" 45", 0x45, false}, | 346 {" 45", 0x45, false}, |
347 {"\t\n\v\f\r 0x45", 0x45, false}, | 347 {"\t\n\v\f\r 0x45", 0x45, false}, |
348 {" 45", 0x45, false}, | 348 {" 45", 0x45, false}, |
349 {"45 ", 0x45, false}, | 349 {"45 ", 0x45, false}, |
350 {"45:", 0x45, false}, | 350 {"45:", 0x45, false}, |
351 {"efgh", 0xef, false}, | 351 {"efgh", 0xef, false}, |
352 {"0xefgh", 0xef, false}, | 352 {"0xefgh", 0xef, false}, |
353 {"hgfe", 0, false}, | 353 {"hgfe", 0, false}, |
354 {"-", 0, false}, | 354 {"-", 0, false}, |
355 {"", 0, false}, | 355 {"", 0, false}, |
356 {"0x", 0, false}, | 356 {"0x", 0, false}, |
357 }; | 357 }; |
358 | 358 |
359 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 359 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
360 uint64 output = 0; | 360 uint64 output = 0; |
361 EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output)); | 361 |
362 EXPECT_EQ(cases[i].output, output); | 362 bool rv = HexStringToUInt64(cases[i].input, &output); |
| 363 EXPECT_TRUE(rv == cases[i].success); |
| 364 if (rv) { |
| 365 EXPECT_EQ(cases[i].output, output); |
| 366 } |
363 } | 367 } |
364 // One additional test to verify that conversion of numbers in strings with | 368 // One additional test to verify that conversion of numbers in strings with |
365 // embedded NUL characters. The NUL and extra data after it should be | 369 // embedded NUL characters. The NUL and extra data after it should be |
366 // interpreted as junk after the number. | 370 // interpreted as junk after the number. |
367 const char input[] = "0xc0ffee\09"; | 371 const char input[] = "0xc0ffee\09"; |
368 std::string input_string(input, arraysize(input) - 1); | 372 std::string input_string(input, arraysize(input) - 1); |
369 uint64 output; | 373 uint64 output; |
370 EXPECT_FALSE(HexStringToUInt64(input_string, &output)); | 374 EXPECT_FALSE(HexStringToUInt64(input_string, &output)); |
371 EXPECT_EQ(0xc0ffeeU, output); | 375 EXPECT_EQ(0xc0ffeeU, output); |
372 } | 376 } |
(...skipping 122 matching lines...) Loading... |
495 | 499 |
496 TEST(StringNumberConversionsTest, HexEncode) { | 500 TEST(StringNumberConversionsTest, HexEncode) { |
497 std::string hex(HexEncode(NULL, 0)); | 501 std::string hex(HexEncode(NULL, 0)); |
498 EXPECT_EQ(hex.length(), 0U); | 502 EXPECT_EQ(hex.length(), 0U); |
499 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 503 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
500 hex = HexEncode(bytes, sizeof(bytes)); | 504 hex = HexEncode(bytes, sizeof(bytes)); |
501 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 505 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
502 } | 506 } |
503 | 507 |
504 } // namespace base | 508 } // namespace base |
OLD | NEW |