Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: base/strings/string_number_conversions_unittest.cc

Issue 14109020: HexStringToUInt64 should fail for negative input (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clamp to closest value in the type range Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/strings/string_number_conversions.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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...) Expand 10 before | Expand all | Expand 10 after
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", static_cast<uint64>(-66), true}, 320 {"-42", 0, 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", static_cast<uint64>(INT_MIN), true}, 324 {"-80000000", 0, 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", static_cast<uint64>(-66), true}, 328 {"-0x42", 0, 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", static_cast<uint64>(INT_MIN), true}, 332 {"-0x80000000", 0, 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", 0, false},
337 {"0x8000000000000000", GG_UINT64_C(0x8000000000000000), true}, 337 {"0x8000000000000000", GG_UINT64_C(0x8000000000000000), true},
338 {"-0x8000000000000001", GG_UINT64_C(0x7fffffffffffffff), true}, 338 {"-0x8000000000000001", 0, 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},
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 495
496 TEST(StringNumberConversionsTest, HexEncode) { 496 TEST(StringNumberConversionsTest, HexEncode) {
497 std::string hex(HexEncode(NULL, 0)); 497 std::string hex(HexEncode(NULL, 0));
498 EXPECT_EQ(hex.length(), 0U); 498 EXPECT_EQ(hex.length(), 0U);
499 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; 499 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
500 hex = HexEncode(bytes, sizeof(bytes)); 500 hex = HexEncode(bytes, sizeof(bytes));
501 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); 501 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
502 } 502 }
503 503
504 } // namespace base 504 } // namespace base
OLDNEW
« no previous file with comments | « base/strings/string_number_conversions.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698