| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 // One additional test to verify that conversion of numbers in strings with | 337 // One additional test to verify that conversion of numbers in strings with |
| 338 // embedded NUL characters. The NUL and extra data after it should be | 338 // embedded NUL characters. The NUL and extra data after it should be |
| 339 // interpreted as junk after the number. | 339 // interpreted as junk after the number. |
| 340 const char input[] = "3.14\0159"; | 340 const char input[] = "3.14\0159"; |
| 341 std::string input_string(input, arraysize(input) - 1); | 341 std::string input_string(input, arraysize(input) - 1); |
| 342 double output; | 342 double output; |
| 343 EXPECT_FALSE(StringToDouble(input_string, &output)); | 343 EXPECT_FALSE(StringToDouble(input_string, &output)); |
| 344 EXPECT_DOUBLE_EQ(3.14, output); | 344 EXPECT_DOUBLE_EQ(3.14, output); |
| 345 } | 345 } |
| 346 | 346 |
| 347 TEST(StringNumberConversionsTest, DoubleToString) { |
| 348 static const struct { |
| 349 double input; |
| 350 const char* expected; |
| 351 } cases[] = { |
| 352 {0.0, "0"}, |
| 353 {1.25, "1.25"}, |
| 354 {1.33518e+012, "1.33518e+12"}, |
| 355 {1.33489e+012, "1.33489e+12"}, |
| 356 {1.33505e+012, "1.33505e+12"}, |
| 357 {1.33545e+009, "1335450000"}, |
| 358 {1.33503e+009, "1335030000"}, |
| 359 }; |
| 360 |
| 361 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 362 EXPECT_EQ(cases[i].expected, DoubleToString(cases[i].input)); |
| 363 } |
| 364 |
| 365 // The following two values were seen in crashes in the wild. |
| 366 const char input_bytes[8] = {0, 0, 0, 0, '\xee', '\x6d', '\x73', '\x42'}; |
| 367 double input = 0; |
| 368 memcpy(&input, input_bytes, arraysize(input_bytes)); |
| 369 EXPECT_EQ("1335179083776", DoubleToString(input)); |
| 370 const char input_bytes2[8] = |
| 371 {0, 0, 0, '\xa0', '\xda', '\x6c', '\x73', '\x42'}; |
| 372 input = 0; |
| 373 memcpy(&input, input_bytes2, arraysize(input_bytes2)); |
| 374 EXPECT_EQ("1334890332160", DoubleToString(input)); |
| 375 } |
| 376 |
| 347 TEST(StringNumberConversionsTest, HexEncode) { | 377 TEST(StringNumberConversionsTest, HexEncode) { |
| 348 std::string hex(HexEncode(NULL, 0)); | 378 std::string hex(HexEncode(NULL, 0)); |
| 349 EXPECT_EQ(hex.length(), 0U); | 379 EXPECT_EQ(hex.length(), 0U); |
| 350 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 380 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 351 hex = HexEncode(bytes, sizeof(bytes)); | 381 hex = HexEncode(bytes, sizeof(bytes)); |
| 352 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 382 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 353 } | 383 } |
| 354 | 384 |
| 355 } // namespace base | 385 } // namespace base |
| OLD | NEW |