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" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 template <typename INT> | 17 template <typename INT> |
18 struct IntToStringTest { | 18 struct IntToStringTest { |
19 INT num; | 19 INT num; |
20 const char* sexpected; | 20 const char* sexpected; |
21 const char* uexpected; | 21 const char* uexpected; |
22 }; | 22 }; |
23 | 23 |
24 } // namespace | 24 } // namespace |
25 | 25 |
26 TEST(StringNumberConversionsTest, IntToString) { | 26 TEST(StringNumberConversionsTest, IntToString) { |
27 static const IntToStringTest<int> int_tests[] = { | 27 static const IntToStringTest<int> int_tests[] = { |
28 { 0, "0", "0" }, | 28 { 0, "0", "0" }, |
29 { -1, "-1", "4294967295" }, | 29 { -1, "-1", "4294967295" }, |
30 { std::numeric_limits<int>::max(), "2147483647", "2147483647" }, | 30 { std::numeric_limits<int>::max(), "2147483647", "2147483647" }, |
31 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" }, | 31 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" }, |
32 }; | 32 }; |
33 static const IntToStringTest<int64> int64_tests[] = { | 33 static const IntToStringTest<int64> int64_tests[] = { |
34 { 0, "0", "0" }, | 34 { 0, "0", "0" }, |
35 { -1, "-1", "18446744073709551615" }, | 35 { -1, "-1", "18446744073709551615" }, |
36 { std::numeric_limits<int64>::max(), | 36 { std::numeric_limits<int64>::max(), |
37 "9223372036854775807", | 37 "9223372036854775807", |
38 "9223372036854775807", }, | 38 "9223372036854775807", }, |
39 { std::numeric_limits<int64>::min(), | 39 { std::numeric_limits<int64>::min(), |
40 "-9223372036854775808", | 40 "-9223372036854775808", |
41 "9223372036854775808" }, | 41 "9223372036854775808" }, |
42 }; | 42 }; |
43 | 43 |
44 for (size_t i = 0; i < arraysize(int_tests); ++i) { | 44 for (size_t i = 0; i < arraysize(int_tests); ++i) { |
45 const IntToStringTest<int>* test = &int_tests[i]; | 45 const IntToStringTest<int>* test = &int_tests[i]; |
46 EXPECT_EQ(IntToString(test->num), test->sexpected); | 46 EXPECT_EQ(IntToString(test->num), test->sexpected); |
47 EXPECT_EQ(IntToString16(test->num), UTF8ToUTF16(test->sexpected)); | 47 EXPECT_EQ(IntToString16(test->num), UTF8ToUTF16(test->sexpected)); |
48 EXPECT_EQ(UintToString(test->num), test->uexpected); | 48 EXPECT_EQ(UintToString(test->num), test->uexpected); |
49 EXPECT_EQ(UintToString16(test->num), UTF8ToUTF16(test->uexpected)); | 49 EXPECT_EQ(UintToString16(test->num), UTF8ToUTF16(test->uexpected)); |
50 } | 50 } |
51 for (size_t i = 0; i < arraysize(int64_tests); ++i) { | 51 for (size_t i = 0; i < arraysize(int64_tests); ++i) { |
(...skipping 13 matching lines...) Expand all Loading... |
65 {0, "0"}, | 65 {0, "0"}, |
66 {42, "42"}, | 66 {42, "42"}, |
67 {INT_MAX, "2147483647"}, | 67 {INT_MAX, "2147483647"}, |
68 {kuint64max, "18446744073709551615"}, | 68 {kuint64max, "18446744073709551615"}, |
69 }; | 69 }; |
70 | 70 |
71 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) | 71 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) |
72 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input)); | 72 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input)); |
73 } | 73 } |
74 | 74 |
| 75 TEST(StringNumberConversionsTest, PointerToString) { |
| 76 static const struct { |
| 77 void* input; |
| 78 const char* output; |
| 79 } cases[] = { |
| 80 {0, "0"}, |
| 81 {reinterpret_cast<void*>(42), "42"}, |
| 82 {reinterpret_cast<void*>(UINT_MAX), "4294967295"}, |
| 83 }; |
| 84 |
| 85 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) |
| 86 EXPECT_EQ(std::string(cases[i].output), PointerToString(cases[i].input)); |
| 87 |
| 88 if (sizeof(void*) == sizeof(uint64)) { |
| 89 EXPECT_EQ(std::string("18446744073709551615"), |
| 90 PointerToString(reinterpret_cast<void*>(ULLONG_MAX))); |
| 91 } |
| 92 } |
| 93 |
75 TEST(StringNumberConversionsTest, StringToInt) { | 94 TEST(StringNumberConversionsTest, StringToInt) { |
76 static const struct { | 95 static const struct { |
77 std::string input; | 96 std::string input; |
78 int output; | 97 int output; |
79 bool success; | 98 bool success; |
80 } cases[] = { | 99 } cases[] = { |
81 {"0", 0, true}, | 100 {"0", 0, true}, |
82 {"42", 42, true}, | 101 {"42", 42, true}, |
83 {"42\x99", 42, false}, | 102 {"42\x99", 42, false}, |
84 {"\x99" "42\x99", 0, false}, | 103 {"\x99" "42\x99", 0, false}, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 int64 output; | 212 int64 output; |
194 EXPECT_FALSE(StringToInt64(input_string, &output)); | 213 EXPECT_FALSE(StringToInt64(input_string, &output)); |
195 EXPECT_EQ(6, output); | 214 EXPECT_EQ(6, output); |
196 | 215 |
197 string16 utf16_input = UTF8ToUTF16(input_string); | 216 string16 utf16_input = UTF8ToUTF16(input_string); |
198 output = 0; | 217 output = 0; |
199 EXPECT_FALSE(StringToInt64(utf16_input, &output)); | 218 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
200 EXPECT_EQ(6, output); | 219 EXPECT_EQ(6, output); |
201 } | 220 } |
202 | 221 |
| 222 TEST(StringNumberConversionsTest, StringToPointer) { |
| 223 static const struct { |
| 224 const char* input; |
| 225 void* output; |
| 226 } cases[] = { |
| 227 {"0", 0}, |
| 228 {"42", reinterpret_cast<void*>(42)}, |
| 229 {"4294967295", reinterpret_cast<void*>(UINT_MAX)}, |
| 230 }; |
| 231 |
| 232 void* value; |
| 233 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 234 EXPECT_TRUE(StringToPointer(cases[i].input, &value)); |
| 235 EXPECT_EQ(cases[i].output, value); |
| 236 } |
| 237 |
| 238 if (sizeof(void*) == sizeof(uint64)) { |
| 239 EXPECT_TRUE(StringToPointer(std::string("18446744073709551615"), &value)); |
| 240 EXPECT_EQ(reinterpret_cast<void*>(ULLONG_MAX), value); |
| 241 } |
| 242 } |
| 243 |
203 TEST(StringNumberConversionsTest, HexStringToInt) { | 244 TEST(StringNumberConversionsTest, HexStringToInt) { |
204 static const struct { | 245 static const struct { |
205 std::string input; | 246 std::string input; |
206 int output; | 247 int output; |
207 bool success; | 248 bool success; |
208 } cases[] = { | 249 } cases[] = { |
209 {"0", 0, true}, | 250 {"0", 0, true}, |
210 {"42", 66, true}, | 251 {"42", 66, true}, |
211 {"-42", -66, true}, | 252 {"-42", -66, true}, |
212 {"+42", 66, true}, | 253 {"+42", 66, true}, |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 417 |
377 TEST(StringNumberConversionsTest, HexEncode) { | 418 TEST(StringNumberConversionsTest, HexEncode) { |
378 std::string hex(HexEncode(NULL, 0)); | 419 std::string hex(HexEncode(NULL, 0)); |
379 EXPECT_EQ(hex.length(), 0U); | 420 EXPECT_EQ(hex.length(), 0U); |
380 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 421 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
381 hex = HexEncode(bytes, sizeof(bytes)); | 422 hex = HexEncode(bytes, sizeof(bytes)); |
382 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 423 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
383 } | 424 } |
384 | 425 |
385 } // namespace base | 426 } // namespace base |
OLD | NEW |