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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 {"-+123", 0, false}, | 98 {"-+123", 0, false}, |
99 {"+-123", 0, false}, | 99 {"+-123", 0, false}, |
100 {"-", 0, false}, | 100 {"-", 0, false}, |
101 {"-2147483649", INT_MIN, false}, | 101 {"-2147483649", INT_MIN, false}, |
102 {"-99999999999", INT_MIN, false}, | 102 {"-99999999999", INT_MIN, false}, |
103 {"2147483648", INT_MAX, false}, | 103 {"2147483648", INT_MAX, false}, |
104 {"99999999999", INT_MAX, false}, | 104 {"99999999999", INT_MAX, false}, |
105 }; | 105 }; |
106 | 106 |
107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 108 const char* ascii_chars = cases[i].input.c_str(); |
108 int output = 0; | 109 int output = 0; |
109 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); | 110 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); |
110 EXPECT_EQ(cases[i].output, output); | 111 EXPECT_EQ(cases[i].output, output); |
| 112 output = 0; |
| 113 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input.begin(), |
| 114 cases[i].input.end(), |
| 115 &output)); |
| 116 EXPECT_EQ(cases[i].output, output); |
| 117 output = 0; |
| 118 EXPECT_EQ(cases[i].success, StringToInt( |
| 119 ascii_chars, ascii_chars + cases[i].input.length(), &output)); |
| 120 EXPECT_EQ(cases[i].output, output); |
111 | 121 |
112 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 122 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
| 123 const char16* utf16_chars = utf16_input.c_str(); |
113 output = 0; | 124 output = 0; |
114 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); | 125 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); |
115 EXPECT_EQ(cases[i].output, output); | 126 EXPECT_EQ(cases[i].output, output); |
| 127 output = 0; |
| 128 EXPECT_EQ(cases[i].success, StringToInt(utf16_input.begin(), |
| 129 utf16_input.end(), |
| 130 &output)); |
| 131 EXPECT_EQ(cases[i].output, output); |
| 132 output = 0; |
| 133 EXPECT_EQ(cases[i].success, StringToInt( |
| 134 utf16_chars, utf16_chars + utf16_input.length(), &output)); |
| 135 EXPECT_EQ(cases[i].output, output); |
116 } | 136 } |
117 | 137 |
118 // One additional test to verify that conversion of numbers in strings with | 138 // One additional test to verify that conversion of numbers in strings with |
119 // embedded NUL characters. The NUL and extra data after it should be | 139 // embedded NUL characters. The NUL and extra data after it should be |
120 // interpreted as junk after the number. | 140 // interpreted as junk after the number. |
121 const char input[] = "6\06"; | 141 const char input[] = "6\06"; |
122 std::string input_string(input, arraysize(input) - 1); | 142 std::string input_string(input, arraysize(input) - 1); |
123 int output; | 143 int output; |
124 EXPECT_FALSE(StringToInt(input_string, &output)); | 144 EXPECT_FALSE(StringToInt(input_string, &output)); |
125 EXPECT_EQ(6, output); | 145 EXPECT_EQ(6, output); |
| 146 output = 0; |
| 147 EXPECT_FALSE(StringToInt(input_string.begin(), input_string.end(), &output)); |
| 148 EXPECT_EQ(6, output); |
| 149 output = 0; |
| 150 EXPECT_FALSE(StringToInt(input, input + arraysize(input), &output)); |
| 151 EXPECT_EQ(6, output); |
126 | 152 |
127 string16 utf16_input = UTF8ToUTF16(input_string); | 153 string16 utf16_input = UTF8ToUTF16(input_string); |
| 154 const char16* utf16_chars = utf16_input.c_str(); |
128 output = 0; | 155 output = 0; |
129 EXPECT_FALSE(StringToInt(utf16_input, &output)); | 156 EXPECT_FALSE(StringToInt(utf16_input, &output)); |
130 EXPECT_EQ(6, output); | 157 EXPECT_EQ(6, output); |
| 158 output = 0; |
| 159 EXPECT_FALSE(StringToInt(utf16_input.begin(), utf16_input.end(), &output)); |
| 160 EXPECT_EQ(6, output); |
| 161 output = 0; |
| 162 EXPECT_FALSE(StringToInt(utf16_chars, |
| 163 utf16_chars + utf16_input.length(), |
| 164 &output)); |
| 165 EXPECT_EQ(6, output); |
131 | 166 |
132 output = 0; | 167 output = 0; |
133 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | 168 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
134 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); | 169 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); |
135 EXPECT_EQ(0, output); | 170 EXPECT_EQ(0, output); |
136 } | 171 } |
137 | 172 |
138 TEST(StringNumberConversionsTest, StringToInt64) { | 173 TEST(StringNumberConversionsTest, StringToInt64) { |
139 static const struct { | 174 static const struct { |
140 std::string input; | 175 std::string input; |
(...skipping 27 matching lines...) Expand all Loading... |
168 {"-+123", 0, false}, | 203 {"-+123", 0, false}, |
169 {"+-123", 0, false}, | 204 {"+-123", 0, false}, |
170 {"-", 0, false}, | 205 {"-", 0, false}, |
171 {"-9223372036854775809", kint64min, false}, | 206 {"-9223372036854775809", kint64min, false}, |
172 {"-99999999999999999999", kint64min, false}, | 207 {"-99999999999999999999", kint64min, false}, |
173 {"9223372036854775808", kint64max, false}, | 208 {"9223372036854775808", kint64max, false}, |
174 {"99999999999999999999", kint64max, false}, | 209 {"99999999999999999999", kint64max, false}, |
175 }; | 210 }; |
176 | 211 |
177 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 212 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 213 const char* ascii_chars = cases[i].input.c_str(); |
178 int64 output = 0; | 214 int64 output = 0; |
179 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); | 215 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); |
180 EXPECT_EQ(cases[i].output, output); | 216 EXPECT_EQ(cases[i].output, output); |
| 217 output = 0; |
| 218 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input.begin(), |
| 219 cases[i].input.end(), |
| 220 &output)); |
| 221 EXPECT_EQ(cases[i].output, output); |
| 222 output = 0; |
| 223 EXPECT_EQ(cases[i].success, StringToInt64( |
| 224 ascii_chars, ascii_chars + cases[i].input.length(), &output)); |
| 225 EXPECT_EQ(cases[i].output, output); |
181 | 226 |
182 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 227 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
| 228 const char16* utf16_chars = utf16_input.c_str(); |
183 output = 0; | 229 output = 0; |
184 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); | 230 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); |
185 EXPECT_EQ(cases[i].output, output); | 231 EXPECT_EQ(cases[i].output, output); |
| 232 output = 0; |
| 233 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input.begin(), |
| 234 utf16_input.end(), |
| 235 &output)); |
| 236 EXPECT_EQ(cases[i].output, output); |
| 237 output = 0; |
| 238 EXPECT_EQ(cases[i].success, StringToInt64( |
| 239 utf16_chars, utf16_chars + utf16_input.length(), &output)); |
| 240 EXPECT_EQ(cases[i].output, output); |
186 } | 241 } |
187 | 242 |
188 // One additional test to verify that conversion of numbers in strings with | 243 // One additional test to verify that conversion of numbers in strings with |
189 // embedded NUL characters. The NUL and extra data after it should be | 244 // embedded NUL characters. The NUL and extra data after it should be |
190 // interpreted as junk after the number. | 245 // interpreted as junk after the number. |
191 const char input[] = "6\06"; | 246 const char input[] = "6\06"; |
192 std::string input_string(input, arraysize(input) - 1); | 247 std::string input_string(input, arraysize(input) - 1); |
193 int64 output; | 248 int64 output; |
194 EXPECT_FALSE(StringToInt64(input_string, &output)); | 249 EXPECT_FALSE(StringToInt64(input_string, &output)); |
195 EXPECT_EQ(6, output); | 250 EXPECT_EQ(6, output); |
| 251 output = 0; |
| 252 EXPECT_FALSE(StringToInt64(input_string.begin(), |
| 253 input_string.end(), |
| 254 &output)); |
| 255 EXPECT_EQ(6, output); |
| 256 output = 0; |
| 257 EXPECT_FALSE(StringToInt64(input, input + arraysize(input), &output)); |
| 258 EXPECT_EQ(6, output); |
196 | 259 |
197 string16 utf16_input = UTF8ToUTF16(input_string); | 260 string16 utf16_input = UTF8ToUTF16(input_string); |
| 261 const char16* utf16_chars = utf16_input.c_str(); |
198 output = 0; | 262 output = 0; |
199 EXPECT_FALSE(StringToInt64(utf16_input, &output)); | 263 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
200 EXPECT_EQ(6, output); | 264 EXPECT_EQ(6, output); |
| 265 output = 0; |
| 266 EXPECT_FALSE(StringToInt64(utf16_input.begin(), utf16_input.end(), &output)); |
| 267 EXPECT_EQ(6, output); |
| 268 output = 0; |
| 269 EXPECT_FALSE(StringToInt64(utf16_chars, |
| 270 utf16_chars + utf16_input.length(), |
| 271 &output)); |
| 272 EXPECT_EQ(6, output); |
201 } | 273 } |
202 | 274 |
203 TEST(StringNumberConversionsTest, HexStringToInt) { | 275 TEST(StringNumberConversionsTest, HexStringToInt) { |
204 static const struct { | 276 static const struct { |
205 std::string input; | 277 std::string input; |
206 int output; | 278 int output; |
207 bool success; | 279 bool success; |
208 } cases[] = { | 280 } cases[] = { |
209 {"0", 0, true}, | 281 {"0", 0, true}, |
210 {"42", 66, true}, | 282 {"42", 66, true}, |
(...skipping 20 matching lines...) Expand all Loading... |
231 {"efgh", 0xef, false}, | 303 {"efgh", 0xef, false}, |
232 {"0xefgh", 0xef, false}, | 304 {"0xefgh", 0xef, false}, |
233 {"hgfe", 0, false}, | 305 {"hgfe", 0, false}, |
234 {"100000000", -1, false}, // don't care about |output|, just |success| | 306 {"100000000", -1, false}, // don't care about |output|, just |success| |
235 {"-", 0, false}, | 307 {"-", 0, false}, |
236 {"", 0, false}, | 308 {"", 0, false}, |
237 {"0x", 0, false}, | 309 {"0x", 0, false}, |
238 }; | 310 }; |
239 | 311 |
240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 312 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 313 const char* ascii_chars = cases[i].input.c_str(); |
241 int output = 0; | 314 int output = 0; |
242 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); | 315 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); |
243 EXPECT_EQ(cases[i].output, output); | 316 EXPECT_EQ(cases[i].output, output); |
| 317 output = 0; |
| 318 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input.begin(), |
| 319 cases[i].input.end(), |
| 320 &output)); |
| 321 EXPECT_EQ(cases[i].output, output); |
| 322 output = 0; |
| 323 EXPECT_EQ(cases[i].success, HexStringToInt( |
| 324 ascii_chars, ascii_chars + cases[i].input.length(), &output)); |
| 325 EXPECT_EQ(cases[i].output, output); |
244 } | 326 } |
245 // One additional test to verify that conversion of numbers in strings with | 327 // 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 | 328 // embedded NUL characters. The NUL and extra data after it should be |
247 // interpreted as junk after the number. | 329 // interpreted as junk after the number. |
248 const char input[] = "0xc0ffee\09"; | 330 const char input[] = "0xc0ffee\09"; |
249 std::string input_string(input, arraysize(input) - 1); | 331 std::string input_string(input, arraysize(input) - 1); |
250 int output; | 332 int output; |
251 EXPECT_FALSE(HexStringToInt(input_string, &output)); | 333 EXPECT_FALSE(HexStringToInt(input_string, &output)); |
252 EXPECT_EQ(0xc0ffee, output); | 334 EXPECT_EQ(0xc0ffee, output); |
| 335 output = 0; |
| 336 EXPECT_FALSE(HexStringToInt(input_string.begin(), |
| 337 input_string.end(), |
| 338 &output)); |
| 339 EXPECT_EQ(0xc0ffee, output); |
| 340 output = 0; |
| 341 EXPECT_FALSE(HexStringToInt(input, input + arraysize(input), &output)); |
| 342 EXPECT_EQ(0xc0ffee, output); |
253 } | 343 } |
254 | 344 |
255 TEST(StringNumberConversionsTest, HexStringToBytes) { | 345 TEST(StringNumberConversionsTest, HexStringToBytes) { |
256 static const struct { | 346 static const struct { |
257 const std::string input; | 347 const std::string input; |
258 const char* output; | 348 const char* output; |
259 size_t output_len; | 349 size_t output_len; |
260 bool success; | 350 bool success; |
261 } cases[] = { | 351 } cases[] = { |
262 {"0", "", 0, false}, // odd number of characters fails | 352 {"0", "", 0, false}, // odd number of characters fails |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 | 436 |
347 TEST(StringNumberConversionsTest, HexEncode) { | 437 TEST(StringNumberConversionsTest, HexEncode) { |
348 std::string hex(HexEncode(NULL, 0)); | 438 std::string hex(HexEncode(NULL, 0)); |
349 EXPECT_EQ(hex.length(), 0U); | 439 EXPECT_EQ(hex.length(), 0U); |
350 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 440 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
351 hex = HexEncode(bytes, sizeof(bytes)); | 441 hex = HexEncode(bytes, sizeof(bytes)); |
352 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 442 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
353 } | 443 } |
354 | 444 |
355 } // namespace base | 445 } // namespace base |
OLD | NEW |