123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796 |
- // Copyright (c) 2012 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include <errno.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <cmath>
- #include <limits>
- #include "butil/format_macros.h"
- #include "butil/strings/string_number_conversions.h"
- #include "butil/strings/stringprintf.h"
- #include "butil/strings/utf_string_conversions.h"
- #include <gtest/gtest.h>
- namespace butil {
- namespace {
- template <typename INT>
- struct IntToStringTest {
- INT num;
- const char* sexpected;
- const char* uexpected;
- };
- } // namespace
- TEST(StringNumberConversionsTest, IntToString) {
- static const IntToStringTest<int> int_tests[] = {
- { 0, "0", "0" },
- { -1, "-1", "4294967295" },
- { std::numeric_limits<int>::max(), "2147483647", "2147483647" },
- { std::numeric_limits<int>::min(), "-2147483648", "2147483648" },
- };
- static const IntToStringTest<int64_t> int64_tests[] = {
- { 0, "0", "0" },
- { -1, "-1", "18446744073709551615" },
- { std::numeric_limits<int64_t>::max(),
- "9223372036854775807",
- "9223372036854775807", },
- { std::numeric_limits<int64_t>::min(),
- "-9223372036854775808",
- "9223372036854775808" },
- };
- for (size_t i = 0; i < arraysize(int_tests); ++i) {
- const IntToStringTest<int>* test = &int_tests[i];
- EXPECT_EQ(IntToString(test->num), test->sexpected);
- EXPECT_EQ(IntToString16(test->num), UTF8ToUTF16(test->sexpected));
- EXPECT_EQ(UintToString(test->num), test->uexpected);
- EXPECT_EQ(UintToString16(test->num), UTF8ToUTF16(test->uexpected));
- }
- for (size_t i = 0; i < arraysize(int64_tests); ++i) {
- const IntToStringTest<int64_t>* test = &int64_tests[i];
- EXPECT_EQ(Int64ToString(test->num), test->sexpected);
- EXPECT_EQ(Int64ToString16(test->num), UTF8ToUTF16(test->sexpected));
- EXPECT_EQ(Uint64ToString(test->num), test->uexpected);
- EXPECT_EQ(Uint64ToString16(test->num), UTF8ToUTF16(test->uexpected));
- }
- }
- TEST(StringNumberConversionsTest, Uint64ToString) {
- static const struct {
- uint64_t input;
- std::string output;
- } cases[] = {
- {0, "0"},
- {42, "42"},
- {INT_MAX, "2147483647"},
- {kuint64max, "18446744073709551615"},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i)
- EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input));
- }
- TEST(StringNumberConversionsTest, SizeTToString) {
- size_t size_t_max = std::numeric_limits<size_t>::max();
- std::string size_t_max_string = StringPrintf("%" PRIuS, size_t_max);
- static const struct {
- size_t input;
- std::string output;
- } cases[] = {
- {0, "0"},
- {9, "9"},
- {42, "42"},
- {INT_MAX, "2147483647"},
- {2147483648U, "2147483648"},
- #if SIZE_MAX > 4294967295U
- {99999999999U, "99999999999"},
- #endif
- {size_t_max, size_t_max_string},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i)
- EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input));
- }
- TEST(StringNumberConversionsTest, StringToInt) {
- static const struct {
- std::string input;
- int output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 42, true},
- {"42\x99", 42, false},
- {"\x99" "42\x99", 0, false},
- {"-2147483648", INT_MIN, true},
- {"2147483647", INT_MAX, true},
- {"", 0, false},
- {" 42", 42, false},
- {"42 ", 42, false},
- {"\t\n\v\f\r 42", 42, false},
- {"blah42", 0, false},
- {"42blah", 42, false},
- {"blah42blah", 0, false},
- {"-273.15", -273, false},
- {"+98.6", 98, false},
- {"--123", 0, false},
- {"++123", 0, false},
- {"-+123", 0, false},
- {"+-123", 0, false},
- {"-", 0, false},
- {"-2147483649", INT_MIN, false},
- {"-99999999999", INT_MIN, false},
- {"2147483648", INT_MAX, false},
- {"99999999999", INT_MAX, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- int output = 0;
- EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- string16 utf16_input = UTF8ToUTF16(cases[i].input);
- output = 0;
- EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "6\06";
- std::string input_string(input, arraysize(input) - 1);
- int output;
- EXPECT_FALSE(StringToInt(input_string, &output));
- EXPECT_EQ(6, output);
- string16 utf16_input = UTF8ToUTF16(input_string);
- output = 0;
- EXPECT_FALSE(StringToInt(utf16_input, &output));
- EXPECT_EQ(6, output);
- output = 0;
- const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0};
- EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output));
- EXPECT_EQ(0, output);
- }
- TEST(StringNumberConversionsTest, StringToUint) {
- static const struct {
- std::string input;
- unsigned output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 42, true},
- {"42\x99", 42, false},
- {"\x99" "42\x99", 0, false},
- {"-2147483648", 0, false},
- {"2147483647", INT_MAX, true},
- {"", 0, false},
- {" 42", 42, false},
- {"42 ", 42, false},
- {"\t\n\v\f\r 42", 42, false},
- {"blah42", 0, false},
- {"42blah", 42, false},
- {"blah42blah", 0, false},
- {"-273.15", 0, false},
- {"+98.6", 98, false},
- {"--123", 0, false},
- {"++123", 0, false},
- {"-+123", 0, false},
- {"+-123", 0, false},
- {"-", 0, false},
- {"-2147483649", 0, false},
- {"-99999999999", 0, false},
- {"4294967295", UINT_MAX, true},
- {"4294967296", UINT_MAX, false},
- {"99999999999", UINT_MAX, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- unsigned output = 0;
- EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- string16 utf16_input = UTF8ToUTF16(cases[i].input);
- output = 0;
- EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "6\06";
- std::string input_string(input, arraysize(input) - 1);
- unsigned output;
- EXPECT_FALSE(StringToUint(input_string, &output));
- EXPECT_EQ(6U, output);
- string16 utf16_input = UTF8ToUTF16(input_string);
- output = 0;
- EXPECT_FALSE(StringToUint(utf16_input, &output));
- EXPECT_EQ(6U, output);
- output = 0;
- const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0};
- EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output));
- EXPECT_EQ(0U, output);
- }
- TEST(StringNumberConversionsTest, StringToInt64) {
- static const struct {
- std::string input;
- int64_t output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 42, true},
- {"-2147483648", INT_MIN, true},
- {"2147483647", INT_MAX, true},
- {"-2147483649", GG_INT64_C(-2147483649), true},
- {"-99999999999", GG_INT64_C(-99999999999), true},
- {"2147483648", GG_INT64_C(2147483648), true},
- {"99999999999", GG_INT64_C(99999999999), true},
- {"9223372036854775807", kint64max, true},
- {"-9223372036854775808", kint64min, true},
- {"09", 9, true},
- {"-09", -9, true},
- {"", 0, false},
- {" 42", 42, false},
- {"42 ", 42, false},
- {"0x42", 0, false},
- {"\t\n\v\f\r 42", 42, false},
- {"blah42", 0, false},
- {"42blah", 42, false},
- {"blah42blah", 0, false},
- {"-273.15", -273, false},
- {"+98.6", 98, false},
- {"--123", 0, false},
- {"++123", 0, false},
- {"-+123", 0, false},
- {"+-123", 0, false},
- {"-", 0, false},
- {"-9223372036854775809", kint64min, false},
- {"-99999999999999999999", kint64min, false},
- {"9223372036854775808", kint64max, false},
- {"99999999999999999999", kint64max, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- int64_t output = 0;
- EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- string16 utf16_input = UTF8ToUTF16(cases[i].input);
- output = 0;
- EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "6\06";
- std::string input_string(input, arraysize(input) - 1);
- int64_t output;
- EXPECT_FALSE(StringToInt64(input_string, &output));
- EXPECT_EQ(6, output);
- string16 utf16_input = UTF8ToUTF16(input_string);
- output = 0;
- EXPECT_FALSE(StringToInt64(utf16_input, &output));
- EXPECT_EQ(6, output);
- }
- TEST(StringNumberConversionsTest, StringToUint64) {
- static const struct {
- std::string input;
- uint64_t output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 42, true},
- {"-2147483648", 0, false},
- {"2147483647", INT_MAX, true},
- {"-2147483649", 0, false},
- {"-99999999999", 0, false},
- {"2147483648", GG_UINT64_C(2147483648), true},
- {"99999999999", GG_UINT64_C(99999999999), true},
- {"9223372036854775807", kint64max, true},
- {"-9223372036854775808", 0, false},
- {"09", 9, true},
- {"-09", 0, false},
- {"", 0, false},
- {" 42", 42, false},
- {"42 ", 42, false},
- {"0x42", 0, false},
- {"\t\n\v\f\r 42", 42, false},
- {"blah42", 0, false},
- {"42blah", 42, false},
- {"blah42blah", 0, false},
- {"-273.15", 0, false},
- {"+98.6", 98, false},
- {"--123", 0, false},
- {"++123", 0, false},
- {"-+123", 0, false},
- {"+-123", 0, false},
- {"-", 0, false},
- {"-9223372036854775809", 0, false},
- {"-99999999999999999999", 0, false},
- {"9223372036854775808", GG_UINT64_C(9223372036854775808), true},
- {"99999999999999999999", kuint64max, false},
- {"18446744073709551615", kuint64max, true},
- {"18446744073709551616", kuint64max, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- uint64_t output = 0;
- EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- string16 utf16_input = UTF8ToUTF16(cases[i].input);
- output = 0;
- EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "6\06";
- std::string input_string(input, arraysize(input) - 1);
- uint64_t output;
- EXPECT_FALSE(StringToUint64(input_string, &output));
- EXPECT_EQ(6U, output);
- string16 utf16_input = UTF8ToUTF16(input_string);
- output = 0;
- EXPECT_FALSE(StringToUint64(utf16_input, &output));
- EXPECT_EQ(6U, output);
- }
- TEST(StringNumberConversionsTest, StringToSizeT) {
- size_t size_t_max = std::numeric_limits<size_t>::max();
- std::string size_t_max_string = StringPrintf("%" PRIuS, size_t_max);
- static const struct {
- std::string input;
- size_t output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 42, true},
- {"-2147483648", 0, false},
- {"2147483647", INT_MAX, true},
- {"-2147483649", 0, false},
- {"-99999999999", 0, false},
- {"2147483648", 2147483648U, true},
- #if SIZE_MAX > 4294967295U
- {"99999999999", 99999999999U, true},
- #endif
- {"-9223372036854775808", 0, false},
- {"09", 9, true},
- {"-09", 0, false},
- {"", 0, false},
- {" 42", 42, false},
- {"42 ", 42, false},
- {"0x42", 0, false},
- {"\t\n\v\f\r 42", 42, false},
- {"blah42", 0, false},
- {"42blah", 42, false},
- {"blah42blah", 0, false},
- {"-273.15", 0, false},
- {"+98.6", 98, false},
- {"--123", 0, false},
- {"++123", 0, false},
- {"-+123", 0, false},
- {"+-123", 0, false},
- {"-", 0, false},
- {"-9223372036854775809", 0, false},
- {"-99999999999999999999", 0, false},
- {"999999999999999999999999", size_t_max, false},
- {size_t_max_string, size_t_max, true},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- size_t output = 0;
- EXPECT_EQ(cases[i].success, StringToSizeT(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- string16 utf16_input = UTF8ToUTF16(cases[i].input);
- output = 0;
- EXPECT_EQ(cases[i].success, StringToSizeT(utf16_input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "6\06";
- std::string input_string(input, arraysize(input) - 1);
- size_t output;
- EXPECT_FALSE(StringToSizeT(input_string, &output));
- EXPECT_EQ(6U, output);
- string16 utf16_input = UTF8ToUTF16(input_string);
- output = 0;
- EXPECT_FALSE(StringToSizeT(utf16_input, &output));
- EXPECT_EQ(6U, output);
- }
- TEST(StringNumberConversionsTest, HexStringToInt) {
- static const struct {
- std::string input;
- int64_t output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 66, true},
- {"-42", -66, true},
- {"+42", 66, true},
- {"7fffffff", INT_MAX, true},
- {"-80000000", INT_MIN, true},
- {"80000000", INT_MAX, false}, // Overflow test.
- {"-80000001", INT_MIN, false}, // Underflow test.
- {"0x42", 66, true},
- {"-0x42", -66, true},
- {"+0x42", 66, true},
- {"0x7fffffff", INT_MAX, true},
- {"-0x80000000", INT_MIN, true},
- {"-80000000", INT_MIN, true},
- {"80000000", INT_MAX, false}, // Overflow test.
- {"-80000001", INT_MIN, false}, // Underflow test.
- {"0x0f", 15, true},
- {"0f", 15, true},
- {" 45", 0x45, false},
- {"\t\n\v\f\r 0x45", 0x45, false},
- {" 45", 0x45, false},
- {"45 ", 0x45, false},
- {"45:", 0x45, false},
- {"efgh", 0xef, false},
- {"0xefgh", 0xef, false},
- {"hgfe", 0, false},
- {"-", 0, false},
- {"", 0, false},
- {"0x", 0, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- int output = 0;
- EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "0xc0ffee\09";
- std::string input_string(input, arraysize(input) - 1);
- int output;
- EXPECT_FALSE(HexStringToInt(input_string, &output));
- EXPECT_EQ(0xc0ffee, output);
- }
- TEST(StringNumberConversionsTest, HexStringToUInt) {
- static const struct {
- std::string input;
- uint32_t output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 0x42, true},
- {"-42", 0, false},
- {"+42", 0x42, true},
- {"7fffffff", INT_MAX, true},
- {"-80000000", 0, false},
- {"ffffffff", 0xffffffff, true},
- {"DeadBeef", 0xdeadbeef, true},
- {"0x42", 0x42, true},
- {"-0x42", 0, false},
- {"+0x42", 0x42, true},
- {"0x7fffffff", INT_MAX, true},
- {"-0x80000000", 0, false},
- {"0xffffffff", kuint32max, true},
- {"0XDeadBeef", 0xdeadbeef, true},
- {"0x7fffffffffffffff", kuint32max, false}, // Overflow test.
- {"-0x8000000000000000", 0, false},
- {"0x8000000000000000", kuint32max, false}, // Overflow test.
- {"-0x8000000000000001", 0, false},
- {"0xFFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test.
- {"FFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test.
- {"0x0000000000000000", 0, true},
- {"0000000000000000", 0, true},
- {"1FFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test.
- {"0x0f", 0x0f, true},
- {"0f", 0x0f, true},
- {" 45", 0x45, false},
- {"\t\n\v\f\r 0x45", 0x45, false},
- {" 45", 0x45, false},
- {"45 ", 0x45, false},
- {"45:", 0x45, false},
- {"efgh", 0xef, false},
- {"0xefgh", 0xef, false},
- {"hgfe", 0, false},
- {"-", 0, false},
- {"", 0, false},
- {"0x", 0, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- uint32_t output = 0;
- EXPECT_EQ(cases[i].success, HexStringToUInt(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "0xc0ffee\09";
- std::string input_string(input, arraysize(input) - 1);
- uint32_t output;
- EXPECT_FALSE(HexStringToUInt(input_string, &output));
- EXPECT_EQ(0xc0ffeeU, output);
- }
- TEST(StringNumberConversionsTest, HexStringToInt64) {
- static const struct {
- std::string input;
- int64_t output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 66, true},
- {"-42", -66, true},
- {"+42", 66, true},
- {"40acd88557b", GG_INT64_C(4444444448123), true},
- {"7fffffff", INT_MAX, true},
- {"-80000000", INT_MIN, true},
- {"ffffffff", 0xffffffff, true},
- {"DeadBeef", 0xdeadbeef, true},
- {"0x42", 66, true},
- {"-0x42", -66, true},
- {"+0x42", 66, true},
- {"0x40acd88557b", GG_INT64_C(4444444448123), true},
- {"0x7fffffff", INT_MAX, true},
- {"-0x80000000", INT_MIN, true},
- {"0xffffffff", 0xffffffff, true},
- {"0XDeadBeef", 0xdeadbeef, true},
- {"0x7fffffffffffffff", kint64max, true},
- {"-0x8000000000000000", kint64min, true},
- {"0x8000000000000000", kint64max, false}, // Overflow test.
- {"-0x8000000000000001", kint64min, false}, // Underflow test.
- {"0x0f", 15, true},
- {"0f", 15, true},
- {" 45", 0x45, false},
- {"\t\n\v\f\r 0x45", 0x45, false},
- {" 45", 0x45, false},
- {"45 ", 0x45, false},
- {"45:", 0x45, false},
- {"efgh", 0xef, false},
- {"0xefgh", 0xef, false},
- {"hgfe", 0, false},
- {"-", 0, false},
- {"", 0, false},
- {"0x", 0, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- int64_t output = 0;
- EXPECT_EQ(cases[i].success, HexStringToInt64(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "0xc0ffee\09";
- std::string input_string(input, arraysize(input) - 1);
- int64_t output;
- EXPECT_FALSE(HexStringToInt64(input_string, &output));
- EXPECT_EQ(0xc0ffee, output);
- }
- TEST(StringNumberConversionsTest, HexStringToUInt64) {
- static const struct {
- std::string input;
- uint64_t output;
- bool success;
- } cases[] = {
- {"0", 0, true},
- {"42", 66, true},
- {"-42", 0, false},
- {"+42", 66, true},
- {"40acd88557b", GG_INT64_C(4444444448123), true},
- {"7fffffff", INT_MAX, true},
- {"-80000000", 0, false},
- {"ffffffff", 0xffffffff, true},
- {"DeadBeef", 0xdeadbeef, true},
- {"0x42", 66, true},
- {"-0x42", 0, false},
- {"+0x42", 66, true},
- {"0x40acd88557b", GG_INT64_C(4444444448123), true},
- {"0x7fffffff", INT_MAX, true},
- {"-0x80000000", 0, false},
- {"0xffffffff", 0xffffffff, true},
- {"0XDeadBeef", 0xdeadbeef, true},
- {"0x7fffffffffffffff", kint64max, true},
- {"-0x8000000000000000", 0, false},
- {"0x8000000000000000", GG_UINT64_C(0x8000000000000000), true},
- {"-0x8000000000000001", 0, false},
- {"0xFFFFFFFFFFFFFFFF", kuint64max, true},
- {"FFFFFFFFFFFFFFFF", kuint64max, true},
- {"0x0000000000000000", 0, true},
- {"0000000000000000", 0, true},
- {"1FFFFFFFFFFFFFFFF", kuint64max, false}, // Overflow test.
- {"0x0f", 15, true},
- {"0f", 15, true},
- {" 45", 0x45, false},
- {"\t\n\v\f\r 0x45", 0x45, false},
- {" 45", 0x45, false},
- {"45 ", 0x45, false},
- {"45:", 0x45, false},
- {"efgh", 0xef, false},
- {"0xefgh", 0xef, false},
- {"hgfe", 0, false},
- {"-", 0, false},
- {"", 0, false},
- {"0x", 0, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- uint64_t output = 0;
- EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output));
- EXPECT_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "0xc0ffee\09";
- std::string input_string(input, arraysize(input) - 1);
- uint64_t output;
- EXPECT_FALSE(HexStringToUInt64(input_string, &output));
- EXPECT_EQ(0xc0ffeeU, output);
- }
- TEST(StringNumberConversionsTest, HexStringToBytes) {
- static const struct {
- const std::string input;
- const char* output;
- size_t output_len;
- bool success;
- } cases[] = {
- {"0", "", 0, false}, // odd number of characters fails
- {"00", "\0", 1, true},
- {"42", "\x42", 1, true},
- {"-42", "", 0, false}, // any non-hex value fails
- {"+42", "", 0, false},
- {"7fffffff", "\x7f\xff\xff\xff", 4, true},
- {"80000000", "\x80\0\0\0", 4, true},
- {"deadbeef", "\xde\xad\xbe\xef", 4, true},
- {"DeadBeef", "\xde\xad\xbe\xef", 4, true},
- {"0x42", "", 0, false}, // leading 0x fails (x is not hex)
- {"0f", "\xf", 1, true},
- {"45 ", "\x45", 1, false},
- {"efgh", "\xef", 1, false},
- {"", "", 0, false},
- {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true},
- {"0123456789ABCDEF012345",
- "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- std::vector<uint8_t> output;
- std::vector<uint8_t> compare;
- EXPECT_EQ(cases[i].success, HexStringToBytes(cases[i].input, &output)) <<
- i << ": " << cases[i].input;
- for (size_t j = 0; j < cases[i].output_len; ++j)
- compare.push_back(static_cast<uint8_t>(cases[i].output[j]));
- ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input;
- EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) <<
- i << ": " << cases[i].input;
- }
- }
- TEST(StringNumberConversionsTest, StringToDouble) {
- static const struct {
- std::string input;
- double output;
- bool success;
- } cases[] = {
- {"0", 0.0, true},
- {"42", 42.0, true},
- {"-42", -42.0, true},
- {"123.45", 123.45, true},
- {"-123.45", -123.45, true},
- {"+123.45", 123.45, true},
- {"2.99792458e8", 299792458.0, true},
- {"149597870.691E+3", 149597870691.0, true},
- {"6.", 6.0, true},
- {"9e99999999999999999999", HUGE_VAL, false},
- {"-9e99999999999999999999", -HUGE_VAL, false},
- {"1e-2", 0.01, true},
- {"42 ", 42.0, false},
- {" 1e-2", 0.01, false},
- {"1e-2 ", 0.01, false},
- {"-1E-7", -0.0000001, true},
- {"01e02", 100, true},
- {"2.3e15", 2.3e15, true},
- {"\t\n\v\f\r -123.45e2", -12345.0, false},
- {"+123 e4", 123.0, false},
- {"123e ", 123.0, false},
- {"123e", 123.0, false},
- {" 2.99", 2.99, false},
- {"1e3.4", 1000.0, false},
- {"nothing", 0.0, false},
- {"-", 0.0, false},
- {"+", 0.0, false},
- {"", 0.0, false},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- double output;
- errno = 1;
- EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output));
- if (cases[i].success) {
- EXPECT_EQ(1, errno) << i; // confirm that errno is unchanged.
- }
- EXPECT_DOUBLE_EQ(cases[i].output, output);
- }
- // One additional test to verify that conversion of numbers in strings with
- // embedded NUL characters. The NUL and extra data after it should be
- // interpreted as junk after the number.
- const char input[] = "3.14\0159";
- std::string input_string(input, arraysize(input) - 1);
- double output;
- EXPECT_FALSE(StringToDouble(input_string, &output));
- EXPECT_DOUBLE_EQ(3.14, output);
- }
- TEST(StringNumberConversionsTest, DoubleToString) {
- static const struct {
- double input;
- const char* expected;
- } cases[] = {
- {0.0, "0"},
- {1.25, "1.25"},
- {1.33518e+012, "1.33518e+12"},
- {1.33489e+012, "1.33489e+12"},
- {1.33505e+012, "1.33505e+12"},
- {1.33545e+009, "1335450000"},
- {1.33503e+009, "1335030000"},
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- EXPECT_EQ(cases[i].expected, DoubleToString(cases[i].input));
- }
- // The following two values were seen in crashes in the wild.
- const char input_bytes[8] = {0, 0, 0, 0, '\xee', '\x6d', '\x73', '\x42'};
- double input = 0;
- memcpy(&input, input_bytes, arraysize(input_bytes));
- EXPECT_EQ("1335179083776", DoubleToString(input));
- const char input_bytes2[8] =
- {0, 0, 0, '\xa0', '\xda', '\x6c', '\x73', '\x42'};
- input = 0;
- memcpy(&input, input_bytes2, arraysize(input_bytes2));
- EXPECT_EQ("1334890332160", DoubleToString(input));
- }
- TEST(StringNumberConversionsTest, HexEncode) {
- std::string hex(HexEncode(NULL, 0));
- EXPECT_EQ(hex.length(), 0U);
- unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
- hex = HexEncode(bytes, sizeof(bytes));
- EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
- }
- } // namespace butil
|