OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <limits> |
| 6 #include <math.h> // For HUGE_VAL. |
| 7 |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace base { |
| 13 |
| 14 namespace { |
| 15 |
| 16 template <typename INT> |
| 17 struct IntToStringTest { |
| 18 INT num; |
| 19 const char* sexpected; |
| 20 const char* uexpected; |
| 21 }; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 TEST(StringNumberConversionsTest, IntToString) { |
| 26 static const IntToStringTest<int> int_tests[] = { |
| 27 { 0, "0", "0" }, |
| 28 { -1, "-1", "4294967295" }, |
| 29 { std::numeric_limits<int>::max(), "2147483647", "2147483647" }, |
| 30 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" }, |
| 31 }; |
| 32 static const IntToStringTest<int64> int64_tests[] = { |
| 33 { 0, "0", "0" }, |
| 34 { -1, "-1", "18446744073709551615" }, |
| 35 { std::numeric_limits<int64>::max(), |
| 36 "9223372036854775807", |
| 37 "9223372036854775807", }, |
| 38 { std::numeric_limits<int64>::min(), |
| 39 "-9223372036854775808", |
| 40 "9223372036854775808" }, |
| 41 }; |
| 42 |
| 43 for (size_t i = 0; i < arraysize(int_tests); ++i) { |
| 44 const IntToStringTest<int>* test = &int_tests[i]; |
| 45 EXPECT_EQ(IntToString(test->num), test->sexpected); |
| 46 EXPECT_EQ(IntToString16(test->num), UTF8ToUTF16(test->sexpected)); |
| 47 EXPECT_EQ(UintToString(test->num), test->uexpected); |
| 48 EXPECT_EQ(UintToString16(test->num), UTF8ToUTF16(test->uexpected)); |
| 49 } |
| 50 for (size_t i = 0; i < arraysize(int64_tests); ++i) { |
| 51 const IntToStringTest<int64>* test = &int64_tests[i]; |
| 52 EXPECT_EQ(Int64ToString(test->num), test->sexpected); |
| 53 EXPECT_EQ(Int64ToString16(test->num), UTF8ToUTF16(test->sexpected)); |
| 54 EXPECT_EQ(Uint64ToString(test->num), test->uexpected); |
| 55 EXPECT_EQ(Uint64ToString16(test->num), UTF8ToUTF16(test->uexpected)); |
| 56 } |
| 57 } |
| 58 |
| 59 TEST(StringNumberConversionsTest, Uint64ToString) { |
| 60 static const struct { |
| 61 uint64 input; |
| 62 std::string output; |
| 63 } cases[] = { |
| 64 {0, "0"}, |
| 65 {42, "42"}, |
| 66 {INT_MAX, "2147483647"}, |
| 67 {kuint64max, "18446744073709551615"}, |
| 68 }; |
| 69 |
| 70 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) |
| 71 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input)); |
| 72 } |
| 73 |
| 74 TEST(StringNumberConversionsTest, StringToInt) { |
| 75 static const struct { |
| 76 std::string input; |
| 77 int output; |
| 78 bool success; |
| 79 } cases[] = { |
| 80 {"0", 0, true}, |
| 81 {"42", 42, true}, |
| 82 {"-2147483648", INT_MIN, true}, |
| 83 {"2147483647", INT_MAX, true}, |
| 84 {"", 0, false}, |
| 85 {" 42", 42, false}, |
| 86 {"42 ", 42, false}, |
| 87 {"\t\n\v\f\r 42", 42, false}, |
| 88 {"blah42", 0, false}, |
| 89 {"42blah", 42, false}, |
| 90 {"blah42blah", 0, false}, |
| 91 {"-273.15", -273, false}, |
| 92 {"+98.6", 98, false}, |
| 93 {"--123", 0, false}, |
| 94 {"++123", 0, false}, |
| 95 {"-+123", 0, false}, |
| 96 {"+-123", 0, false}, |
| 97 {"-", 0, false}, |
| 98 {"-2147483649", INT_MIN, false}, |
| 99 {"-99999999999", INT_MIN, false}, |
| 100 {"2147483648", INT_MAX, false}, |
| 101 {"99999999999", INT_MAX, false}, |
| 102 }; |
| 103 |
| 104 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 105 int output; |
| 106 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); |
| 107 EXPECT_EQ(cases[i].output, output); |
| 108 |
| 109 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
| 110 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); |
| 111 EXPECT_EQ(cases[i].output, output); |
| 112 } |
| 113 |
| 114 // One additional test to verify that conversion of numbers in strings with |
| 115 // embedded NUL characters. The NUL and extra data after it should be |
| 116 // interpreted as junk after the number. |
| 117 const char input[] = "6\06"; |
| 118 std::string input_string(input, arraysize(input) - 1); |
| 119 int output; |
| 120 EXPECT_FALSE(StringToInt(input_string, &output)); |
| 121 EXPECT_EQ(6, output); |
| 122 |
| 123 string16 utf16_input = UTF8ToUTF16(input_string); |
| 124 EXPECT_FALSE(StringToInt(utf16_input, &output)); |
| 125 EXPECT_EQ(6, output); |
| 126 } |
| 127 |
| 128 TEST(StringNumberConversionsTest, StringToInt64) { |
| 129 static const struct { |
| 130 std::string input; |
| 131 int64 output; |
| 132 bool success; |
| 133 } cases[] = { |
| 134 {"0", 0, true}, |
| 135 {"42", 42, true}, |
| 136 {"-2147483648", INT_MIN, true}, |
| 137 {"2147483647", INT_MAX, true}, |
| 138 {"-2147483649", GG_INT64_C(-2147483649), true}, |
| 139 {"-99999999999", GG_INT64_C(-99999999999), true}, |
| 140 {"2147483648", GG_INT64_C(2147483648), true}, |
| 141 {"99999999999", GG_INT64_C(99999999999), true}, |
| 142 {"9223372036854775807", kint64max, true}, |
| 143 {"-9223372036854775808", kint64min, true}, |
| 144 {"09", 9, true}, |
| 145 {"-09", -9, true}, |
| 146 {"", 0, false}, |
| 147 {" 42", 42, false}, |
| 148 {"42 ", 42, false}, |
| 149 {"\t\n\v\f\r 42", 42, false}, |
| 150 {"blah42", 0, false}, |
| 151 {"42blah", 42, false}, |
| 152 {"blah42blah", 0, false}, |
| 153 {"-273.15", -273, false}, |
| 154 {"+98.6", 98, false}, |
| 155 {"--123", 0, false}, |
| 156 {"++123", 0, false}, |
| 157 {"-+123", 0, false}, |
| 158 {"+-123", 0, false}, |
| 159 {"-", 0, false}, |
| 160 {"-9223372036854775809", kint64min, false}, |
| 161 {"-99999999999999999999", kint64min, false}, |
| 162 {"9223372036854775808", kint64max, false}, |
| 163 {"99999999999999999999", kint64max, false}, |
| 164 }; |
| 165 |
| 166 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 167 int64 output; |
| 168 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); |
| 169 EXPECT_EQ(cases[i].output, output); |
| 170 |
| 171 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
| 172 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); |
| 173 EXPECT_EQ(cases[i].output, output); |
| 174 } |
| 175 |
| 176 // One additional test to verify that conversion of numbers in strings with |
| 177 // embedded NUL characters. The NUL and extra data after it should be |
| 178 // interpreted as junk after the number. |
| 179 const char input[] = "6\06"; |
| 180 std::string input_string(input, arraysize(input) - 1); |
| 181 int64 output; |
| 182 EXPECT_FALSE(StringToInt64(input_string, &output)); |
| 183 EXPECT_EQ(6, output); |
| 184 |
| 185 string16 utf16_input = UTF8ToUTF16(input_string); |
| 186 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
| 187 EXPECT_EQ(6, output); |
| 188 } |
| 189 |
| 190 TEST(StringNumberConversionsTest, HexStringToInt) { |
| 191 static const struct { |
| 192 std::string input; |
| 193 int output; |
| 194 bool success; |
| 195 } cases[] = { |
| 196 {"0", 0, true}, |
| 197 {"42", 66, true}, |
| 198 {"-42", -66, true}, |
| 199 {"+42", 66, true}, |
| 200 {"7fffffff", INT_MAX, true}, |
| 201 {"80000000", INT_MIN, true}, |
| 202 {"ffffffff", -1, true}, |
| 203 {"DeadBeef", 0xdeadbeef, true}, |
| 204 {"0x42", 66, true}, |
| 205 {"-0x42", -66, true}, |
| 206 {"+0x42", 66, true}, |
| 207 {"0x7fffffff", INT_MAX, true}, |
| 208 {"0x80000000", INT_MIN, true}, |
| 209 {"0xffffffff", -1, true}, |
| 210 {"0XDeadBeef", 0xdeadbeef, true}, |
| 211 {"0x0f", 15, true}, |
| 212 {"0f", 15, true}, |
| 213 {" 45", 0x45, false}, |
| 214 {"\t\n\v\f\r 0x45", 0x45, false}, |
| 215 {" 45", 0x45, false}, |
| 216 {"45 ", 0x45, false}, |
| 217 {"efgh", 0xef, false}, |
| 218 {"0xefgh", 0xef, false}, |
| 219 {"hgfe", 0, false}, |
| 220 {"100000000", -1, false}, // don't care about |output|, just |success| |
| 221 {"-", 0, false}, |
| 222 {"", 0, false}, |
| 223 }; |
| 224 |
| 225 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 226 int output; |
| 227 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); |
| 228 EXPECT_EQ(cases[i].output, output); |
| 229 } |
| 230 // One additional test to verify that conversion of numbers in strings with |
| 231 // embedded NUL characters. The NUL and extra data after it should be |
| 232 // interpreted as junk after the number. |
| 233 const char input[] = "0xc0ffee\09"; |
| 234 std::string input_string(input, arraysize(input) - 1); |
| 235 int output; |
| 236 EXPECT_FALSE(HexStringToInt(input_string, &output)); |
| 237 EXPECT_EQ(0xc0ffee, output); |
| 238 } |
| 239 |
| 240 TEST(StringNumberConversionsTest, HexStringToBytes) { |
| 241 static const struct { |
| 242 const std::string input; |
| 243 const char* output; |
| 244 size_t output_len; |
| 245 bool success; |
| 246 } cases[] = { |
| 247 {"0", "", 0, false}, // odd number of characters fails |
| 248 {"00", "\0", 1, true}, |
| 249 {"42", "\x42", 1, true}, |
| 250 {"-42", "", 0, false}, // any non-hex value fails |
| 251 {"+42", "", 0, false}, |
| 252 {"7fffffff", "\x7f\xff\xff\xff", 4, true}, |
| 253 {"80000000", "\x80\0\0\0", 4, true}, |
| 254 {"deadbeef", "\xde\xad\xbe\xef", 4, true}, |
| 255 {"DeadBeef", "\xde\xad\xbe\xef", 4, true}, |
| 256 {"0x42", "", 0, false}, // leading 0x fails (x is not hex) |
| 257 {"0f", "\xf", 1, true}, |
| 258 {"45 ", "\x45", 1, false}, |
| 259 {"efgh", "\xef", 1, false}, |
| 260 {"", "", 0, false}, |
| 261 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true}, |
| 262 {"0123456789ABCDEF012345", |
| 263 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true}, |
| 264 }; |
| 265 |
| 266 |
| 267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 268 std::vector<uint8> output; |
| 269 std::vector<uint8> compare; |
| 270 EXPECT_EQ(cases[i].success, HexStringToBytes(cases[i].input, &output)) << |
| 271 i << ": " << cases[i].input; |
| 272 for (size_t j = 0; j < cases[i].output_len; ++j) |
| 273 compare.push_back(static_cast<uint8>(cases[i].output[j])); |
| 274 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; |
| 275 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << |
| 276 i << ": " << cases[i].input; |
| 277 } |
| 278 } |
| 279 |
| 280 TEST(StringNumberConversionsTest, StringToDouble) { |
| 281 static const struct { |
| 282 std::string input; |
| 283 double output; |
| 284 bool success; |
| 285 } cases[] = { |
| 286 {"0", 0.0, true}, |
| 287 {"42", 42.0, true}, |
| 288 {"-42", -42.0, true}, |
| 289 {"123.45", 123.45, true}, |
| 290 {"-123.45", -123.45, true}, |
| 291 {"+123.45", 123.45, true}, |
| 292 {"2.99792458e8", 299792458.0, true}, |
| 293 {"149597870.691E+3", 149597870691.0, true}, |
| 294 {"6.", 6.0, true}, |
| 295 {"9e99999999999999999999", HUGE_VAL, false}, |
| 296 {"-9e99999999999999999999", -HUGE_VAL, false}, |
| 297 {"1e-2", 0.01, true}, |
| 298 {" 1e-2", 0.01, false}, |
| 299 {"1e-2 ", 0.01, false}, |
| 300 {"-1E-7", -0.0000001, true}, |
| 301 {"01e02", 100, true}, |
| 302 {"2.3e15", 2.3e15, true}, |
| 303 {"\t\n\v\f\r -123.45e2", -12345.0, false}, |
| 304 {"+123 e4", 123.0, false}, |
| 305 {"123e ", 123.0, false}, |
| 306 {"123e", 123.0, false}, |
| 307 {" 2.99", 2.99, false}, |
| 308 {"1e3.4", 1000.0, false}, |
| 309 {"nothing", 0.0, false}, |
| 310 {"-", 0.0, false}, |
| 311 {"+", 0.0, false}, |
| 312 {"", 0.0, false}, |
| 313 }; |
| 314 |
| 315 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 316 double output; |
| 317 EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output)); |
| 318 EXPECT_DOUBLE_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[] = "3.14\0159"; |
| 325 std::string input_string(input, arraysize(input) - 1); |
| 326 double output; |
| 327 EXPECT_FALSE(StringToDouble(input_string, &output)); |
| 328 EXPECT_DOUBLE_EQ(3.14, output); |
| 329 } |
| 330 |
| 331 TEST(StringNumberConversionsTest, HexEncode) { |
| 332 std::string hex(HexEncode(NULL, 0)); |
| 333 EXPECT_EQ(hex.length(), 0U); |
| 334 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 335 hex = HexEncode(bytes, sizeof(bytes)); |
| 336 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 337 } |
| 338 |
| 339 } // namespace base |
OLD | NEW |