Limit App Host installation / usage for platform apps to Google Chome only.
[chromium-blink-merge.git] / base / string_number_conversions_unittest.cc
blob30b9b8c06ea416ee82820521f9b6f69ec5e16b1e
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
3 // found in the LICENSE file.
5 #include <math.h>
7 #include <limits>
9 #include "base/string_number_conversions.h"
10 #include "base/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace base {
15 namespace {
17 template <typename INT>
18 struct IntToStringTest {
19 INT num;
20 const char* sexpected;
21 const char* uexpected;
24 } // namespace
26 TEST(StringNumberConversionsTest, IntToString) {
27 static const IntToStringTest<int> int_tests[] = {
28 { 0, "0", "0" },
29 { -1, "-1", "4294967295" },
30 { std::numeric_limits<int>::max(), "2147483647", "2147483647" },
31 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" },
33 static const IntToStringTest<int64> int64_tests[] = {
34 { 0, "0", "0" },
35 { -1, "-1", "18446744073709551615" },
36 { std::numeric_limits<int64>::max(),
37 "9223372036854775807",
38 "9223372036854775807", },
39 { std::numeric_limits<int64>::min(),
40 "-9223372036854775808",
41 "9223372036854775808" },
44 for (size_t i = 0; i < arraysize(int_tests); ++i) {
45 const IntToStringTest<int>* test = &int_tests[i];
46 EXPECT_EQ(IntToString(test->num), test->sexpected);
47 EXPECT_EQ(IntToString16(test->num), UTF8ToUTF16(test->sexpected));
48 EXPECT_EQ(UintToString(test->num), test->uexpected);
49 EXPECT_EQ(UintToString16(test->num), UTF8ToUTF16(test->uexpected));
51 for (size_t i = 0; i < arraysize(int64_tests); ++i) {
52 const IntToStringTest<int64>* test = &int64_tests[i];
53 EXPECT_EQ(Int64ToString(test->num), test->sexpected);
54 EXPECT_EQ(Int64ToString16(test->num), UTF8ToUTF16(test->sexpected));
55 EXPECT_EQ(Uint64ToString(test->num), test->uexpected);
56 EXPECT_EQ(Uint64ToString16(test->num), UTF8ToUTF16(test->uexpected));
60 TEST(StringNumberConversionsTest, Uint64ToString) {
61 static const struct {
62 uint64 input;
63 std::string output;
64 } cases[] = {
65 {0, "0"},
66 {42, "42"},
67 {INT_MAX, "2147483647"},
68 {kuint64max, "18446744073709551615"},
71 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i)
72 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input));
75 TEST(StringNumberConversionsTest, StringToInt) {
76 static const struct {
77 std::string input;
78 int output;
79 bool success;
80 } cases[] = {
81 {"0", 0, true},
82 {"42", 42, true},
83 {"42\x99", 42, false},
84 {"\x99" "42\x99", 0, false},
85 {"-2147483648", INT_MIN, true},
86 {"2147483647", INT_MAX, true},
87 {"", 0, false},
88 {" 42", 42, false},
89 {"42 ", 42, false},
90 {"\t\n\v\f\r 42", 42, false},
91 {"blah42", 0, false},
92 {"42blah", 42, false},
93 {"blah42blah", 0, false},
94 {"-273.15", -273, false},
95 {"+98.6", 98, false},
96 {"--123", 0, false},
97 {"++123", 0, false},
98 {"-+123", 0, false},
99 {"+-123", 0, false},
100 {"-", 0, false},
101 {"-2147483649", INT_MIN, false},
102 {"-99999999999", INT_MIN, false},
103 {"2147483648", INT_MAX, false},
104 {"99999999999", INT_MAX, false},
107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
108 int output = 0;
109 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output));
110 EXPECT_EQ(cases[i].output, output);
112 string16 utf16_input = UTF8ToUTF16(cases[i].input);
113 output = 0;
114 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output));
115 EXPECT_EQ(cases[i].output, output);
118 // 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
120 // interpreted as junk after the number.
121 const char input[] = "6\06";
122 std::string input_string(input, arraysize(input) - 1);
123 int output;
124 EXPECT_FALSE(StringToInt(input_string, &output));
125 EXPECT_EQ(6, output);
127 string16 utf16_input = UTF8ToUTF16(input_string);
128 output = 0;
129 EXPECT_FALSE(StringToInt(utf16_input, &output));
130 EXPECT_EQ(6, output);
132 output = 0;
133 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0};
134 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output));
135 EXPECT_EQ(0, output);
138 TEST(StringNumberConversionsTest, StringToInt64) {
139 static const struct {
140 std::string input;
141 int64 output;
142 bool success;
143 } cases[] = {
144 {"0", 0, true},
145 {"42", 42, true},
146 {"-2147483648", INT_MIN, true},
147 {"2147483647", INT_MAX, true},
148 {"-2147483649", GG_INT64_C(-2147483649), true},
149 {"-99999999999", GG_INT64_C(-99999999999), true},
150 {"2147483648", GG_INT64_C(2147483648), true},
151 {"99999999999", GG_INT64_C(99999999999), true},
152 {"9223372036854775807", kint64max, true},
153 {"-9223372036854775808", kint64min, true},
154 {"09", 9, true},
155 {"-09", -9, true},
156 {"", 0, false},
157 {" 42", 42, false},
158 {"42 ", 42, false},
159 {"0x42", 0, false},
160 {"\t\n\v\f\r 42", 42, false},
161 {"blah42", 0, false},
162 {"42blah", 42, false},
163 {"blah42blah", 0, false},
164 {"-273.15", -273, false},
165 {"+98.6", 98, false},
166 {"--123", 0, false},
167 {"++123", 0, false},
168 {"-+123", 0, false},
169 {"+-123", 0, false},
170 {"-", 0, false},
171 {"-9223372036854775809", kint64min, false},
172 {"-99999999999999999999", kint64min, false},
173 {"9223372036854775808", kint64max, false},
174 {"99999999999999999999", kint64max, false},
177 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
178 int64 output = 0;
179 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output));
180 EXPECT_EQ(cases[i].output, output);
182 string16 utf16_input = UTF8ToUTF16(cases[i].input);
183 output = 0;
184 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output));
185 EXPECT_EQ(cases[i].output, output);
188 // 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
190 // interpreted as junk after the number.
191 const char input[] = "6\06";
192 std::string input_string(input, arraysize(input) - 1);
193 int64 output;
194 EXPECT_FALSE(StringToInt64(input_string, &output));
195 EXPECT_EQ(6, output);
197 string16 utf16_input = UTF8ToUTF16(input_string);
198 output = 0;
199 EXPECT_FALSE(StringToInt64(utf16_input, &output));
200 EXPECT_EQ(6, output);
203 TEST(StringNumberConversionsTest, HexStringToInt) {
204 static const struct {
205 std::string input;
206 int output;
207 bool success;
208 } cases[] = {
209 {"0", 0, true},
210 {"42", 66, true},
211 {"-42", -66, true},
212 {"+42", 66, true},
213 {"7fffffff", INT_MAX, true},
214 {"80000000", INT_MIN, true},
215 {"ffffffff", -1, true},
216 {"DeadBeef", 0xdeadbeef, true},
217 {"0x42", 66, true},
218 {"-0x42", -66, true},
219 {"+0x42", 66, true},
220 {"0x7fffffff", INT_MAX, true},
221 {"0x80000000", INT_MIN, true},
222 {"0xffffffff", -1, true},
223 {"0XDeadBeef", 0xdeadbeef, true},
224 {"0x0f", 15, true},
225 {"0f", 15, true},
226 {" 45", 0x45, false},
227 {"\t\n\v\f\r 0x45", 0x45, false},
228 {" 45", 0x45, false},
229 {"45 ", 0x45, false},
230 {"45:", 0x45, false},
231 {"efgh", 0xef, false},
232 {"0xefgh", 0xef, false},
233 {"hgfe", 0, false},
234 {"100000000", -1, false}, // don't care about |output|, just |success|
235 {"-", 0, false},
236 {"", 0, false},
237 {"0x", 0, false},
240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
241 int output = 0;
242 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output));
243 EXPECT_EQ(cases[i].output, output);
245 // 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
247 // interpreted as junk after the number.
248 const char input[] = "0xc0ffee\09";
249 std::string input_string(input, arraysize(input) - 1);
250 int output;
251 EXPECT_FALSE(HexStringToInt(input_string, &output));
252 EXPECT_EQ(0xc0ffee, output);
255 TEST(StringNumberConversionsTest, HexStringToBytes) {
256 static const struct {
257 const std::string input;
258 const char* output;
259 size_t output_len;
260 bool success;
261 } cases[] = {
262 {"0", "", 0, false}, // odd number of characters fails
263 {"00", "\0", 1, true},
264 {"42", "\x42", 1, true},
265 {"-42", "", 0, false}, // any non-hex value fails
266 {"+42", "", 0, false},
267 {"7fffffff", "\x7f\xff\xff\xff", 4, true},
268 {"80000000", "\x80\0\0\0", 4, true},
269 {"deadbeef", "\xde\xad\xbe\xef", 4, true},
270 {"DeadBeef", "\xde\xad\xbe\xef", 4, true},
271 {"0x42", "", 0, false}, // leading 0x fails (x is not hex)
272 {"0f", "\xf", 1, true},
273 {"45 ", "\x45", 1, false},
274 {"efgh", "\xef", 1, false},
275 {"", "", 0, false},
276 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true},
277 {"0123456789ABCDEF012345",
278 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true},
282 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
283 std::vector<uint8> output;
284 std::vector<uint8> compare;
285 EXPECT_EQ(cases[i].success, HexStringToBytes(cases[i].input, &output)) <<
286 i << ": " << cases[i].input;
287 for (size_t j = 0; j < cases[i].output_len; ++j)
288 compare.push_back(static_cast<uint8>(cases[i].output[j]));
289 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input;
290 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) <<
291 i << ": " << cases[i].input;
295 TEST(StringNumberConversionsTest, StringToDouble) {
296 static const struct {
297 std::string input;
298 double output;
299 bool success;
300 } cases[] = {
301 {"0", 0.0, true},
302 {"42", 42.0, true},
303 {"-42", -42.0, true},
304 {"123.45", 123.45, true},
305 {"-123.45", -123.45, true},
306 {"+123.45", 123.45, true},
307 {"2.99792458e8", 299792458.0, true},
308 {"149597870.691E+3", 149597870691.0, true},
309 {"6.", 6.0, true},
310 {"9e99999999999999999999", HUGE_VAL, false},
311 {"-9e99999999999999999999", -HUGE_VAL, false},
312 {"1e-2", 0.01, true},
313 {"42 ", 42.0, false},
314 {" 1e-2", 0.01, false},
315 {"1e-2 ", 0.01, false},
316 {"-1E-7", -0.0000001, true},
317 {"01e02", 100, true},
318 {"2.3e15", 2.3e15, true},
319 {"\t\n\v\f\r -123.45e2", -12345.0, false},
320 {"+123 e4", 123.0, false},
321 {"123e ", 123.0, false},
322 {"123e", 123.0, false},
323 {" 2.99", 2.99, false},
324 {"1e3.4", 1000.0, false},
325 {"nothing", 0.0, false},
326 {"-", 0.0, false},
327 {"+", 0.0, false},
328 {"", 0.0, false},
331 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
332 double output;
333 EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output));
334 EXPECT_DOUBLE_EQ(cases[i].output, output);
337 // One additional test to verify that conversion of numbers in strings with
338 // embedded NUL characters. The NUL and extra data after it should be
339 // interpreted as junk after the number.
340 const char input[] = "3.14\0159";
341 std::string input_string(input, arraysize(input) - 1);
342 double output;
343 EXPECT_FALSE(StringToDouble(input_string, &output));
344 EXPECT_DOUBLE_EQ(3.14, output);
347 TEST(StringNumberConversionsTest, DoubleToString) {
348 static const struct {
349 double input;
350 const char* expected;
351 } cases[] = {
352 {0.0, "0"},
353 {1.25, "1.25"},
354 {1.33518e+012, "1.33518e+12"},
355 {1.33489e+012, "1.33489e+12"},
356 {1.33505e+012, "1.33505e+12"},
357 {1.33545e+009, "1335450000"},
358 {1.33503e+009, "1335030000"},
361 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
362 EXPECT_EQ(cases[i].expected, DoubleToString(cases[i].input));
365 // The following two values were seen in crashes in the wild.
366 const char input_bytes[8] = {0, 0, 0, 0, '\xee', '\x6d', '\x73', '\x42'};
367 double input = 0;
368 memcpy(&input, input_bytes, arraysize(input_bytes));
369 EXPECT_EQ("1335179083776", DoubleToString(input));
370 const char input_bytes2[8] =
371 {0, 0, 0, '\xa0', '\xda', '\x6c', '\x73', '\x42'};
372 input = 0;
373 memcpy(&input, input_bytes2, arraysize(input_bytes2));
374 EXPECT_EQ("1334890332160", DoubleToString(input));
377 TEST(StringNumberConversionsTest, HexEncode) {
378 std::string hex(HexEncode(NULL, 0));
379 EXPECT_EQ(hex.length(), 0U);
380 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
381 hex = HexEncode(bytes, sizeof(bytes));
382 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
385 } // namespace base