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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 static const struct { | 204 static const struct { |
205 std::string input; | 205 std::string input; |
206 int output; | 206 int output; |
207 bool success; | 207 bool success; |
208 } cases[] = { | 208 } cases[] = { |
209 {"0", 0, true}, | 209 {"0", 0, true}, |
210 {"42", 66, true}, | 210 {"42", 66, true}, |
211 {"-42", -66, true}, | 211 {"-42", -66, true}, |
212 {"+42", 66, true}, | 212 {"+42", 66, true}, |
213 {"7fffffff", INT_MAX, true}, | 213 {"7fffffff", INT_MAX, true}, |
214 {"80000000", INT_MIN, true}, | 214 {"-80000000", INT_MIN, true}, |
215 {"ffffffff", -1, true}, | 215 {"80000000", INT_MAX, false}, // Overflow test. |
216 {"DeadBeef", 0xdeadbeef, true}, | 216 {"-80000001", INT_MIN, false}, // Underflow test. |
217 {"0x42", 66, true}, | 217 {"0x42", 66, true}, |
218 {"-0x42", -66, true}, | 218 {"-0x42", -66, true}, |
219 {"+0x42", 66, true}, | 219 {"+0x42", 66, true}, |
220 {"0x7fffffff", INT_MAX, true}, | 220 {"0x7fffffff", INT_MAX, true}, |
221 {"0x80000000", INT_MIN, true}, | 221 {"-0x80000000", INT_MIN, true}, |
222 {"0xffffffff", -1, true}, | 222 {"-80000000", INT_MIN, true}, |
223 {"0XDeadBeef", 0xdeadbeef, true}, | 223 {"80000000", INT_MAX, false}, // Overflow test. |
| 224 {"-80000001", INT_MIN, false}, // Underflow test. |
224 {"0x0f", 15, true}, | 225 {"0x0f", 15, true}, |
225 {"0f", 15, true}, | 226 {"0f", 15, true}, |
226 {" 45", 0x45, false}, | 227 {" 45", 0x45, false}, |
227 {"\t\n\v\f\r 0x45", 0x45, false}, | 228 {"\t\n\v\f\r 0x45", 0x45, false}, |
228 {" 45", 0x45, false}, | 229 {" 45", 0x45, false}, |
229 {"45 ", 0x45, false}, | 230 {"45 ", 0x45, false}, |
230 {"45:", 0x45, false}, | 231 {"45:", 0x45, false}, |
231 {"efgh", 0xef, false}, | 232 {"efgh", 0xef, false}, |
232 {"0xefgh", 0xef, false}, | 233 {"0xefgh", 0xef, false}, |
233 {"hgfe", 0, false}, | 234 {"hgfe", 0, false}, |
234 {"100000000", -1, false}, // don't care about |output|, just |success| | |
235 {"-", 0, false}, | 235 {"-", 0, false}, |
236 {"", 0, false}, | 236 {"", 0, false}, |
237 {"0x", 0, false}, | 237 {"0x", 0, false}, |
238 }; | 238 }; |
239 | 239 |
240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
241 int output = 0; | 241 int output = 0; |
242 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); | 242 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); |
243 EXPECT_EQ(cases[i].output, output); | 243 EXPECT_EQ(cases[i].output, output); |
244 } | 244 } |
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 {"40acd88557b", 4444444448123, true}, |
| 266 {"7fffffff", INT_MAX, true}, |
| 267 {"-80000000", INT_MIN, true}, |
| 268 {"ffffffff", 0xffffffff, true}, |
| 269 {"DeadBeef", 0xdeadbeef, true}, |
| 270 {"0x42", 66, true}, |
| 271 {"-0x42", -66, true}, |
| 272 {"+0x42", 66, true}, |
| 273 {"0x40acd88557b", 4444444448123, true}, |
| 274 {"0x7fffffff", INT_MAX, true}, |
| 275 {"-0x80000000", INT_MIN, true}, |
| 276 {"0xffffffff", 0xffffffff, true}, |
| 277 {"0XDeadBeef", 0xdeadbeef, true}, |
| 278 {"0x7fffffffffffffff", kint64max, true}, |
| 279 {"-0x8000000000000000", kint64min, true}, |
| 280 {"0x8000000000000000", kint64max, false}, // Overflow test. |
| 281 {"-0x8000000000000001", kint64min, false}, // Underflow test. |
| 282 {"0x0f", 15, true}, |
| 283 {"0f", 15, true}, |
| 284 {" 45", 0x45, false}, |
| 285 {"\t\n\v\f\r 0x45", 0x45, false}, |
| 286 {" 45", 0x45, false}, |
| 287 {"45 ", 0x45, false}, |
| 288 {"45:", 0x45, false}, |
| 289 {"efgh", 0xef, false}, |
| 290 {"0xefgh", 0xef, false}, |
| 291 {"hgfe", 0, 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 |