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 "base/strings/string_number_conversions.h" | 5 #include "base/strings/string_number_conversions.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 {"-+123", 0, false}, | 130 {"-+123", 0, false}, |
131 {"+-123", 0, false}, | 131 {"+-123", 0, false}, |
132 {"-", 0, false}, | 132 {"-", 0, false}, |
133 {"-2147483649", INT_MIN, false}, | 133 {"-2147483649", INT_MIN, false}, |
134 {"-99999999999", INT_MIN, false}, | 134 {"-99999999999", INT_MIN, false}, |
135 {"2147483648", INT_MAX, false}, | 135 {"2147483648", INT_MAX, false}, |
136 {"99999999999", INT_MAX, false}, | 136 {"99999999999", INT_MAX, false}, |
137 }; | 137 }; |
138 | 138 |
139 for (size_t i = 0; i < arraysize(cases); ++i) { | 139 for (size_t i = 0; i < arraysize(cases); ++i) { |
140 int output = 0; | 140 int output = cases[i].output ^ 1; // Ensure StringToInt wrote something. |
141 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); | 141 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); |
142 EXPECT_EQ(cases[i].output, output); | 142 EXPECT_EQ(cases[i].output, output); |
143 | 143 |
144 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 144 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
145 output = 0; | 145 output = cases[i].output ^ 1; // Ensure StringToInt wrote something. |
146 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); | 146 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); |
147 EXPECT_EQ(cases[i].output, output); | 147 EXPECT_EQ(cases[i].output, output); |
148 } | 148 } |
149 | 149 |
150 // One additional test to verify that conversion of numbers in strings with | 150 // One additional test to verify that conversion of numbers in strings with |
151 // embedded NUL characters. The NUL and extra data after it should be | 151 // embedded NUL characters. The NUL and extra data after it should be |
152 // interpreted as junk after the number. | 152 // interpreted as junk after the number. |
153 const char input[] = "6\06"; | 153 const char input[] = "6\06"; |
154 std::string input_string(input, arraysize(input) - 1); | 154 std::string input_string(input, arraysize(input) - 1); |
155 int output; | 155 int output; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 {"+-123", 0, false}, | 194 {"+-123", 0, false}, |
195 {"-", 0, false}, | 195 {"-", 0, false}, |
196 {"-2147483649", 0, false}, | 196 {"-2147483649", 0, false}, |
197 {"-99999999999", 0, false}, | 197 {"-99999999999", 0, false}, |
198 {"4294967295", UINT_MAX, true}, | 198 {"4294967295", UINT_MAX, true}, |
199 {"4294967296", UINT_MAX, false}, | 199 {"4294967296", UINT_MAX, false}, |
200 {"99999999999", UINT_MAX, false}, | 200 {"99999999999", UINT_MAX, false}, |
201 }; | 201 }; |
202 | 202 |
203 for (size_t i = 0; i < arraysize(cases); ++i) { | 203 for (size_t i = 0; i < arraysize(cases); ++i) { |
204 unsigned output = 0; | 204 unsigned output = |
| 205 cases[i].output ^ 1; // Ensure StringToUint wrote something. |
205 EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output)); | 206 EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output)); |
206 EXPECT_EQ(cases[i].output, output); | 207 EXPECT_EQ(cases[i].output, output); |
207 | 208 |
208 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 209 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
209 output = 0; | 210 output = cases[i].output ^ 1; // Ensure StringToUint wrote something. |
210 EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output)); | 211 EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output)); |
211 EXPECT_EQ(cases[i].output, output); | 212 EXPECT_EQ(cases[i].output, output); |
212 } | 213 } |
213 | 214 |
214 // One additional test to verify that conversion of numbers in strings with | 215 // One additional test to verify that conversion of numbers in strings with |
215 // embedded NUL characters. The NUL and extra data after it should be | 216 // embedded NUL characters. The NUL and extra data after it should be |
216 // interpreted as junk after the number. | 217 // interpreted as junk after the number. |
217 const char input[] = "6\06"; | 218 const char input[] = "6\06"; |
218 std::string input_string(input, arraysize(input) - 1); | 219 std::string input_string(input, arraysize(input) - 1); |
219 unsigned output; | 220 unsigned output; |
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 | 799 |
799 TEST(StringNumberConversionsTest, HexEncode) { | 800 TEST(StringNumberConversionsTest, HexEncode) { |
800 std::string hex(HexEncode(NULL, 0)); | 801 std::string hex(HexEncode(NULL, 0)); |
801 EXPECT_EQ(hex.length(), 0U); | 802 EXPECT_EQ(hex.length(), 0U); |
802 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 803 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
803 hex = HexEncode(bytes, sizeof(bytes)); | 804 hex = HexEncode(bytes, sizeof(bytes)); |
804 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 805 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
805 } | 806 } |
806 | 807 |
807 } // namespace base | 808 } // namespace base |
OLD | NEW |