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 <errno.h> | 5 #include <errno.h> |
6 #include <math.h> | 6 #include <math.h> |
| 7 #include <stdint.h> |
7 | 8 |
8 #include <limits> | 9 #include <limits> |
9 | 10 |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 namespace { | 17 namespace { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 output = 0; | 130 output = 0; |
130 EXPECT_FALSE(StringToInt(utf16_input, &output)); | 131 EXPECT_FALSE(StringToInt(utf16_input, &output)); |
131 EXPECT_EQ(6, output); | 132 EXPECT_EQ(6, output); |
132 | 133 |
133 output = 0; | 134 output = 0; |
134 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | 135 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
135 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); | 136 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); |
136 EXPECT_EQ(0, output); | 137 EXPECT_EQ(0, output); |
137 } | 138 } |
138 | 139 |
| 140 TEST(StringNumberConversionsTest, StringToUint) { |
| 141 static const struct { |
| 142 std::string input; |
| 143 unsigned output; |
| 144 bool success; |
| 145 } cases[] = { |
| 146 {"0", 0, true}, |
| 147 {"42", 42, true}, |
| 148 {"42\x99", 42, false}, |
| 149 {"\x99" "42\x99", 0, false}, |
| 150 {"-2147483648", 0, false}, |
| 151 {"2147483647", INT_MAX, true}, |
| 152 {"", 0, false}, |
| 153 {" 42", 42, false}, |
| 154 {"42 ", 42, false}, |
| 155 {"\t\n\v\f\r 42", 42, false}, |
| 156 {"blah42", 0, false}, |
| 157 {"42blah", 42, false}, |
| 158 {"blah42blah", 0, false}, |
| 159 {"-273.15", 0, false}, |
| 160 {"+98.6", 98, false}, |
| 161 {"--123", 0, false}, |
| 162 {"++123", 0, false}, |
| 163 {"-+123", 0, false}, |
| 164 {"+-123", 0, false}, |
| 165 {"-", 0, false}, |
| 166 {"-2147483649", 0, false}, |
| 167 {"-99999999999", 0, false}, |
| 168 {"4294967295", UINT_MAX, true}, |
| 169 {"4294967296", UINT_MAX, false}, |
| 170 {"99999999999", UINT_MAX, false}, |
| 171 }; |
| 172 |
| 173 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 174 unsigned output = 0; |
| 175 EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output)); |
| 176 EXPECT_EQ(cases[i].output, output); |
| 177 |
| 178 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
| 179 output = 0; |
| 180 EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output)); |
| 181 EXPECT_EQ(cases[i].output, output); |
| 182 } |
| 183 |
| 184 // One additional test to verify that conversion of numbers in strings with |
| 185 // embedded NUL characters. The NUL and extra data after it should be |
| 186 // interpreted as junk after the number. |
| 187 const char input[] = "6\06"; |
| 188 std::string input_string(input, arraysize(input) - 1); |
| 189 unsigned output; |
| 190 EXPECT_FALSE(StringToUint(input_string, &output)); |
| 191 EXPECT_EQ(6U, output); |
| 192 |
| 193 string16 utf16_input = UTF8ToUTF16(input_string); |
| 194 output = 0; |
| 195 EXPECT_FALSE(StringToUint(utf16_input, &output)); |
| 196 EXPECT_EQ(6U, output); |
| 197 |
| 198 output = 0; |
| 199 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
| 200 EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output)); |
| 201 EXPECT_EQ(0U, output); |
| 202 } |
| 203 |
139 TEST(StringNumberConversionsTest, StringToInt64) { | 204 TEST(StringNumberConversionsTest, StringToInt64) { |
140 static const struct { | 205 static const struct { |
141 std::string input; | 206 std::string input; |
142 int64 output; | 207 int64 output; |
143 bool success; | 208 bool success; |
144 } cases[] = { | 209 } cases[] = { |
145 {"0", 0, true}, | 210 {"0", 0, true}, |
146 {"42", 42, true}, | 211 {"42", 42, true}, |
147 {"-2147483648", INT_MIN, true}, | 212 {"-2147483648", INT_MIN, true}, |
148 {"2147483647", INT_MAX, true}, | 213 {"2147483647", INT_MAX, true}, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 int64 output; | 259 int64 output; |
195 EXPECT_FALSE(StringToInt64(input_string, &output)); | 260 EXPECT_FALSE(StringToInt64(input_string, &output)); |
196 EXPECT_EQ(6, output); | 261 EXPECT_EQ(6, output); |
197 | 262 |
198 string16 utf16_input = UTF8ToUTF16(input_string); | 263 string16 utf16_input = UTF8ToUTF16(input_string); |
199 output = 0; | 264 output = 0; |
200 EXPECT_FALSE(StringToInt64(utf16_input, &output)); | 265 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
201 EXPECT_EQ(6, output); | 266 EXPECT_EQ(6, output); |
202 } | 267 } |
203 | 268 |
| 269 TEST(StringNumberConversionsTest, StringToUint64) { |
| 270 static const struct { |
| 271 std::string input; |
| 272 uint64 output; |
| 273 bool success; |
| 274 } cases[] = { |
| 275 {"0", 0, true}, |
| 276 {"42", 42, true}, |
| 277 {"-2147483648", 0, false}, |
| 278 {"2147483647", INT_MAX, true}, |
| 279 {"-2147483649", 0, false}, |
| 280 {"-99999999999", 0, false}, |
| 281 {"2147483648", GG_INT64_C(2147483648), true}, |
| 282 {"99999999999", GG_INT64_C(99999999999), true}, |
| 283 {"9223372036854775807", kint64max, true}, |
| 284 {"-9223372036854775808", 0, false}, |
| 285 {"09", 9, true}, |
| 286 {"-09", 0, false}, |
| 287 {"", 0, false}, |
| 288 {" 42", 42, false}, |
| 289 {"42 ", 42, false}, |
| 290 {"0x42", 0, false}, |
| 291 {"\t\n\v\f\r 42", 42, false}, |
| 292 {"blah42", 0, false}, |
| 293 {"42blah", 42, false}, |
| 294 {"blah42blah", 0, false}, |
| 295 {"-273.15", 0, false}, |
| 296 {"+98.6", 98, false}, |
| 297 {"--123", 0, false}, |
| 298 {"++123", 0, false}, |
| 299 {"-+123", 0, false}, |
| 300 {"+-123", 0, false}, |
| 301 {"-", 0, false}, |
| 302 {"-9223372036854775809", 0, false}, |
| 303 {"-99999999999999999999", 0, false}, |
| 304 {"9223372036854775808", 9223372036854775808U, true}, |
| 305 {"99999999999999999999", kuint64max, false}, |
| 306 {"18446744073709551615", kuint64max, true}, |
| 307 {"18446744073709551616", kuint64max, false}, |
| 308 }; |
| 309 |
| 310 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 311 uint64 output = 0; |
| 312 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); |
| 313 EXPECT_EQ(cases[i].output, output); |
| 314 |
| 315 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
| 316 output = 0; |
| 317 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output)); |
| 318 EXPECT_EQ(cases[i].output, output); |
| 319 } |
| 320 |
| 321 // One additional test to verify that conversion of numbers in strings with |
| 322 // embedded NUL characters. The NUL and extra data after it should be |
| 323 // interpreted as junk after the number. |
| 324 const char input[] = "6\06"; |
| 325 std::string input_string(input, arraysize(input) - 1); |
| 326 uint64 output; |
| 327 EXPECT_FALSE(StringToUint64(input_string, &output)); |
| 328 EXPECT_EQ(6U, output); |
| 329 |
| 330 string16 utf16_input = UTF8ToUTF16(input_string); |
| 331 output = 0; |
| 332 EXPECT_FALSE(StringToUint64(utf16_input, &output)); |
| 333 EXPECT_EQ(6U, output); |
| 334 } |
| 335 |
| 336 TEST(StringNumberConversionsTest, StringToSizeT) { |
| 337 static const struct { |
| 338 std::string input; |
| 339 size_t output; |
| 340 bool success; |
| 341 } cases[] = { |
| 342 {"0", 0, true}, |
| 343 {"42", 42, true}, |
| 344 {"-2147483648", 0, false}, |
| 345 {"2147483647", INT_MAX, true}, |
| 346 {"-2147483649", 0, false}, |
| 347 {"-99999999999", 0, false}, |
| 348 {"2147483648", 2147483648U, true}, |
| 349 #if SIZE_MAX > 4294967295U |
| 350 {"99999999999", 99999999999U, true}, |
| 351 #endif |
| 352 {"-9223372036854775808", 0, false}, |
| 353 {"09", 9, true}, |
| 354 {"-09", 0, false}, |
| 355 {"", 0, false}, |
| 356 {" 42", 42, false}, |
| 357 {"42 ", 42, false}, |
| 358 {"0x42", 0, false}, |
| 359 {"\t\n\v\f\r 42", 42, false}, |
| 360 {"blah42", 0, false}, |
| 361 {"42blah", 42, false}, |
| 362 {"blah42blah", 0, false}, |
| 363 {"-273.15", 0, false}, |
| 364 {"+98.6", 98, false}, |
| 365 {"--123", 0, false}, |
| 366 {"++123", 0, false}, |
| 367 {"-+123", 0, false}, |
| 368 {"+-123", 0, false}, |
| 369 {"-", 0, false}, |
| 370 {"-9223372036854775809", 0, false}, |
| 371 {"-99999999999999999999", 0, false}, |
| 372 {"999999999999999999999999", std::numeric_limits<size_t>::max(), false}, |
| 373 }; |
| 374 |
| 375 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 376 size_t output = 0; |
| 377 EXPECT_EQ(cases[i].success, StringToSizeT(cases[i].input, &output)); |
| 378 EXPECT_EQ(cases[i].output, output); |
| 379 |
| 380 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
| 381 output = 0; |
| 382 EXPECT_EQ(cases[i].success, StringToSizeT(utf16_input, &output)); |
| 383 EXPECT_EQ(cases[i].output, output); |
| 384 } |
| 385 |
| 386 // One additional test to verify that conversion of numbers in strings with |
| 387 // embedded NUL characters. The NUL and extra data after it should be |
| 388 // interpreted as junk after the number. |
| 389 const char input[] = "6\06"; |
| 390 std::string input_string(input, arraysize(input) - 1); |
| 391 size_t output; |
| 392 EXPECT_FALSE(StringToSizeT(input_string, &output)); |
| 393 EXPECT_EQ(6U, output); |
| 394 |
| 395 string16 utf16_input = UTF8ToUTF16(input_string); |
| 396 output = 0; |
| 397 EXPECT_FALSE(StringToSizeT(utf16_input, &output)); |
| 398 EXPECT_EQ(6U, output); |
| 399 } |
| 400 |
204 TEST(StringNumberConversionsTest, HexStringToInt) { | 401 TEST(StringNumberConversionsTest, HexStringToInt) { |
205 static const struct { | 402 static const struct { |
206 std::string input; | 403 std::string input; |
207 int64 output; | 404 int64 output; |
208 bool success; | 405 bool success; |
209 } cases[] = { | 406 } cases[] = { |
210 {"0", 0, true}, | 407 {"0", 0, true}, |
211 {"42", 66, true}, | 408 {"42", 66, true}, |
212 {"-42", -66, true}, | 409 {"-42", -66, true}, |
213 {"+42", 66, true}, | 410 {"+42", 66, true}, |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 | 696 |
500 TEST(StringNumberConversionsTest, HexEncode) { | 697 TEST(StringNumberConversionsTest, HexEncode) { |
501 std::string hex(HexEncode(NULL, 0)); | 698 std::string hex(HexEncode(NULL, 0)); |
502 EXPECT_EQ(hex.length(), 0U); | 699 EXPECT_EQ(hex.length(), 0U); |
503 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 700 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
504 hex = HexEncode(bytes, sizeof(bytes)); | 701 hex = HexEncode(bytes, sizeof(bytes)); |
505 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 702 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
506 } | 703 } |
507 | 704 |
508 } // namespace base | 705 } // namespace base |
OLD | NEW |