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/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 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 // One additional test to verify that conversion of numbers in strings with | 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 | 246 // embedded NUL characters. The NUL and extra data after it should be |
247 // interpreted as junk after the number. | 247 // interpreted as junk after the number. |
248 const char input[] = "0xc0ffee\09"; | 248 const char input[] = "0xc0ffee\09"; |
249 std::string input_string(input, arraysize(input) - 1); | 249 std::string input_string(input, arraysize(input) - 1); |
250 int output; | 250 int output; |
251 EXPECT_FALSE(HexStringToInt(input_string, &output)); | 251 EXPECT_FALSE(HexStringToInt(input_string, &output)); |
252 EXPECT_EQ(0xc0ffee, output); | 252 EXPECT_EQ(0xc0ffee, output); |
253 } | 253 } |
254 | 254 |
| 255 TEST(StringNumberConversionsTest, HexStringToInt64) { |
| 256 static const struct { |
| 257 std::string input; |
| 258 int64 output; |
| 259 bool success; |
| 260 } cases[] = { |
| 261 {"0", 0, true}, |
| 262 {"42", 66, true}, |
| 263 {"-42", -66, true}, |
| 264 {"+42", 66, true}, |
| 265 {"7fffffff", INT_MAX, true}, |
| 266 {"80000000", 0x80000000, true}, |
| 267 {"ffffffff", 0xffffffff, true}, |
| 268 {"DeadBeef", 0xdeadbeef, true}, |
| 269 {"0x40acd88557b", 0x40acd88557b, true}, |
| 270 {"0x42", 66, true}, |
| 271 {"-0x42", -66, true}, |
| 272 {"+0x42", 66, true}, |
| 273 {"0x7fffffff", INT_MAX, true}, |
| 274 {"0x80000000", 0x80000000, true}, |
| 275 {"0xffffffff", 0xffffffff, true}, |
| 276 {"0XDeadBeef", 0xdeadbeef, true}, |
| 277 {"100000000", 0x100000000, true}, |
| 278 {"0xffffffffffffffff", -1, true}, |
| 279 {"0x7fffffffffffffff", kint64max, true}, |
| 280 {"0x8000000000000000", kint64min, true}, |
| 281 {"0x0f", 15, true}, |
| 282 {"0f", 15, true}, |
| 283 {" 45", 0x45, false}, |
| 284 {"\t\n\v\f\r 0x45", 0x45, false}, |
| 285 {" 45", 0x45, false}, |
| 286 {"45 ", 0x45, false}, |
| 287 {"45:", 0x45, false}, |
| 288 {"efgh", 0xef, false}, |
| 289 {"0xefgh", 0xef, false}, |
| 290 {"hgfe", 0, false}, |
| 291 {"10000000000000000", -1, false}, |
| 292 {"-", 0, false}, |
| 293 {"", 0, false}, |
| 294 {"0x", 0, false}, |
| 295 }; |
| 296 |
| 297 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 298 int64 output = 0; |
| 299 EXPECT_EQ(cases[i].success, HexStringToInt64(cases[i].input, &output)); |
| 300 EXPECT_EQ(cases[i].output, output); |
| 301 } |
| 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); |
| 307 int64 output; |
| 308 EXPECT_FALSE(HexStringToInt64(input_string, &output)); |
| 309 EXPECT_EQ(0xc0ffee, output); |
| 310 } |
| 311 |
255 TEST(StringNumberConversionsTest, HexStringToBytes) { | 312 TEST(StringNumberConversionsTest, HexStringToBytes) { |
256 static const struct { | 313 static const struct { |
257 const std::string input; | 314 const std::string input; |
258 const char* output; | 315 const char* output; |
259 size_t output_len; | 316 size_t output_len; |
260 bool success; | 317 bool success; |
261 } cases[] = { | 318 } cases[] = { |
262 {"0", "", 0, false}, // odd number of characters fails | 319 {"0", "", 0, false}, // odd number of characters fails |
263 {"00", "\0", 1, true}, | 320 {"00", "\0", 1, true}, |
264 {"42", "\x42", 1, true}, | 321 {"42", "\x42", 1, true}, |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 433 |
377 TEST(StringNumberConversionsTest, HexEncode) { | 434 TEST(StringNumberConversionsTest, HexEncode) { |
378 std::string hex(HexEncode(NULL, 0)); | 435 std::string hex(HexEncode(NULL, 0)); |
379 EXPECT_EQ(hex.length(), 0U); | 436 EXPECT_EQ(hex.length(), 0U); |
380 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 437 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
381 hex = HexEncode(bytes, sizeof(bytes)); | 438 hex = HexEncode(bytes, sizeof(bytes)); |
382 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 439 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
383 } | 440 } |
384 | 441 |
385 } // namespace base | 442 } // namespace base |
OLD | NEW |