| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include <stdarg.h> | 6 #include <stdarg.h> |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| (...skipping 810 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 {"99999999999", INT_MAX, false}, | 821 {"99999999999", INT_MAX, false}, |
| 822 }; | 822 }; |
| 823 | 823 |
| 824 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 824 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 825 EXPECT_EQ(cases[i].output, StringToInt(cases[i].input)); | 825 EXPECT_EQ(cases[i].output, StringToInt(cases[i].input)); |
| 826 int output; | 826 int output; |
| 827 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); | 827 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); |
| 828 EXPECT_EQ(cases[i].output, output); | 828 EXPECT_EQ(cases[i].output, output); |
| 829 | 829 |
| 830 std::wstring wide_input = ASCIIToWide(cases[i].input); | 830 std::wstring wide_input = ASCIIToWide(cases[i].input); |
| 831 EXPECT_EQ(cases[i].output, StringToInt(wide_input)); | 831 EXPECT_EQ(cases[i].output, StringToInt(WideToUTF16Hack(wide_input))); |
| 832 EXPECT_EQ(cases[i].success, StringToInt(wide_input, &output)); | 832 EXPECT_EQ(cases[i].success, StringToInt(WideToUTF16Hack(wide_input), |
| 833 &output)); |
| 833 EXPECT_EQ(cases[i].output, output); | 834 EXPECT_EQ(cases[i].output, output); |
| 834 } | 835 } |
| 835 | 836 |
| 836 // One additional test to verify that conversion of numbers in strings with | 837 // One additional test to verify that conversion of numbers in strings with |
| 837 // embedded NUL characters. The NUL and extra data after it should be | 838 // embedded NUL characters. The NUL and extra data after it should be |
| 838 // interpreted as junk after the number. | 839 // interpreted as junk after the number. |
| 839 const char input[] = "6\06"; | 840 const char input[] = "6\06"; |
| 840 std::string input_string(input, arraysize(input) - 1); | 841 std::string input_string(input, arraysize(input) - 1); |
| 841 int output; | 842 int output; |
| 842 EXPECT_FALSE(StringToInt(input_string, &output)); | 843 EXPECT_FALSE(StringToInt(input_string, &output)); |
| 843 EXPECT_EQ(6, output); | 844 EXPECT_EQ(6, output); |
| 844 | 845 |
| 845 std::wstring wide_input = ASCIIToWide(input_string); | 846 std::wstring wide_input = ASCIIToWide(input_string); |
| 846 EXPECT_FALSE(StringToInt(wide_input, &output)); | 847 EXPECT_FALSE(StringToInt(WideToUTF16Hack(wide_input), &output)); |
| 847 EXPECT_EQ(6, output); | 848 EXPECT_EQ(6, output); |
| 848 } | 849 } |
| 849 | 850 |
| 850 TEST(StringUtilTest, StringToInt64) { | 851 TEST(StringUtilTest, StringToInt64) { |
| 851 static const struct { | 852 static const struct { |
| 852 std::string input; | 853 std::string input; |
| 853 int64 output; | 854 int64 output; |
| 854 bool success; | 855 bool success; |
| 855 } cases[] = { | 856 } cases[] = { |
| 856 {"0", 0, true}, | 857 {"0", 0, true}, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 885 {"99999999999999999999", kint64max, false}, | 886 {"99999999999999999999", kint64max, false}, |
| 886 }; | 887 }; |
| 887 | 888 |
| 888 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 889 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 889 EXPECT_EQ(cases[i].output, StringToInt64(cases[i].input)); | 890 EXPECT_EQ(cases[i].output, StringToInt64(cases[i].input)); |
| 890 int64 output; | 891 int64 output; |
| 891 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); | 892 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); |
| 892 EXPECT_EQ(cases[i].output, output); | 893 EXPECT_EQ(cases[i].output, output); |
| 893 | 894 |
| 894 std::wstring wide_input = ASCIIToWide(cases[i].input); | 895 std::wstring wide_input = ASCIIToWide(cases[i].input); |
| 895 EXPECT_EQ(cases[i].output, StringToInt64(wide_input)); | 896 EXPECT_EQ(cases[i].output, StringToInt64(WideToUTF16Hack(wide_input))); |
| 896 EXPECT_EQ(cases[i].success, StringToInt64(wide_input, &output)); | 897 EXPECT_EQ(cases[i].success, StringToInt64(WideToUTF16Hack(wide_input), |
| 898 &output)); |
| 897 EXPECT_EQ(cases[i].output, output); | 899 EXPECT_EQ(cases[i].output, output); |
| 898 } | 900 } |
| 899 | 901 |
| 900 // One additional test to verify that conversion of numbers in strings with | 902 // One additional test to verify that conversion of numbers in strings with |
| 901 // embedded NUL characters. The NUL and extra data after it should be | 903 // embedded NUL characters. The NUL and extra data after it should be |
| 902 // interpreted as junk after the number. | 904 // interpreted as junk after the number. |
| 903 const char input[] = "6\06"; | 905 const char input[] = "6\06"; |
| 904 std::string input_string(input, arraysize(input) - 1); | 906 std::string input_string(input, arraysize(input) - 1); |
| 905 int64 output; | 907 int64 output; |
| 906 EXPECT_FALSE(StringToInt64(input_string, &output)); | 908 EXPECT_FALSE(StringToInt64(input_string, &output)); |
| 907 EXPECT_EQ(6, output); | 909 EXPECT_EQ(6, output); |
| 908 | 910 |
| 909 std::wstring wide_input = ASCIIToWide(input_string); | 911 std::wstring wide_input = ASCIIToWide(input_string); |
| 910 EXPECT_FALSE(StringToInt64(wide_input, &output)); | 912 EXPECT_FALSE(StringToInt64(WideToUTF16Hack(wide_input), &output)); |
| 911 EXPECT_EQ(6, output); | 913 EXPECT_EQ(6, output); |
| 912 } | 914 } |
| 913 | 915 |
| 914 TEST(StringUtilTest, HexStringToInt) { | 916 TEST(StringUtilTest, HexStringToInt) { |
| 915 static const struct { | 917 static const struct { |
| 916 std::string input; | 918 std::string input; |
| 917 int output; | 919 int output; |
| 918 bool success; | 920 bool success; |
| 919 } cases[] = { | 921 } cases[] = { |
| 920 {"0", 0, true}, | 922 {"0", 0, true}, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 946 {"", 0, false}, | 948 {"", 0, false}, |
| 947 }; | 949 }; |
| 948 | 950 |
| 949 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 951 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 950 EXPECT_EQ(cases[i].output, HexStringToInt(cases[i].input)); | 952 EXPECT_EQ(cases[i].output, HexStringToInt(cases[i].input)); |
| 951 int output; | 953 int output; |
| 952 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); | 954 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); |
| 953 EXPECT_EQ(cases[i].output, output); | 955 EXPECT_EQ(cases[i].output, output); |
| 954 | 956 |
| 955 std::wstring wide_input = ASCIIToWide(cases[i].input); | 957 std::wstring wide_input = ASCIIToWide(cases[i].input); |
| 956 EXPECT_EQ(cases[i].output, HexStringToInt(wide_input)); | 958 EXPECT_EQ(cases[i].output, HexStringToInt(WideToUTF16Hack(wide_input))); |
| 957 EXPECT_EQ(cases[i].success, HexStringToInt(wide_input, &output)); | 959 EXPECT_EQ(cases[i].success, HexStringToInt(WideToUTF16Hack(wide_input), |
| 960 &output)); |
| 958 EXPECT_EQ(cases[i].output, output); | 961 EXPECT_EQ(cases[i].output, output); |
| 959 } | 962 } |
| 960 // One additional test to verify that conversion of numbers in strings with | 963 // One additional test to verify that conversion of numbers in strings with |
| 961 // embedded NUL characters. The NUL and extra data after it should be | 964 // embedded NUL characters. The NUL and extra data after it should be |
| 962 // interpreted as junk after the number. | 965 // interpreted as junk after the number. |
| 963 const char input[] = "0xc0ffee\09"; | 966 const char input[] = "0xc0ffee\09"; |
| 964 std::string input_string(input, arraysize(input) - 1); | 967 std::string input_string(input, arraysize(input) - 1); |
| 965 int output; | 968 int output; |
| 966 EXPECT_FALSE(HexStringToInt(input_string, &output)); | 969 EXPECT_FALSE(HexStringToInt(input_string, &output)); |
| 967 EXPECT_EQ(0xc0ffee, output); | 970 EXPECT_EQ(0xc0ffee, output); |
| 968 | 971 |
| 969 std::wstring wide_input = ASCIIToWide(input_string); | 972 std::wstring wide_input = ASCIIToWide(input_string); |
| 970 EXPECT_FALSE(HexStringToInt(wide_input, &output)); | 973 EXPECT_FALSE(HexStringToInt(WideToUTF16Hack(wide_input), &output)); |
| 971 EXPECT_EQ(0xc0ffee, output); | 974 EXPECT_EQ(0xc0ffee, output); |
| 972 } | 975 } |
| 973 | 976 |
| 974 TEST(StringUtilTest, HexStringToBytes) { | 977 TEST(StringUtilTest, HexStringToBytes) { |
| 975 static const struct { | 978 static const struct { |
| 976 const std::string input; | 979 const std::string input; |
| 977 const char* output; | 980 const char* output; |
| 978 size_t output_len; | 981 size_t output_len; |
| 979 bool success; | 982 bool success; |
| 980 } cases[] = { | 983 } cases[] = { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1006 for (size_t j = 0; j < cases[i].output_len; ++j) | 1009 for (size_t j = 0; j < cases[i].output_len; ++j) |
| 1007 compare.push_back(static_cast<uint8>(cases[i].output[j])); | 1010 compare.push_back(static_cast<uint8>(cases[i].output[j])); |
| 1008 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; | 1011 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; |
| 1009 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << | 1012 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << |
| 1010 i << ": " << cases[i].input; | 1013 i << ": " << cases[i].input; |
| 1011 | 1014 |
| 1012 output.clear(); | 1015 output.clear(); |
| 1013 compare.clear(); | 1016 compare.clear(); |
| 1014 | 1017 |
| 1015 std::wstring wide_input = ASCIIToWide(cases[i].input); | 1018 std::wstring wide_input = ASCIIToWide(cases[i].input); |
| 1016 EXPECT_EQ(cases[i].success, HexStringToBytes(wide_input, &output)) << | 1019 EXPECT_EQ(cases[i].success, |
| 1020 HexStringToBytes(WideToUTF16Hack(wide_input), &output)) << |
| 1017 i << ": " << cases[i].input; | 1021 i << ": " << cases[i].input; |
| 1018 for (size_t j = 0; j < cases[i].output_len; ++j) | 1022 for (size_t j = 0; j < cases[i].output_len; ++j) |
| 1019 compare.push_back(static_cast<uint8>(cases[i].output[j])); | 1023 compare.push_back(static_cast<uint8>(cases[i].output[j])); |
| 1020 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; | 1024 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; |
| 1021 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << | 1025 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << |
| 1022 i << ": " << cases[i].input; | 1026 i << ": " << cases[i].input; |
| 1023 } | 1027 } |
| 1024 } | 1028 } |
| 1025 | 1029 |
| 1026 TEST(StringUtilTest, StringToDouble) { | 1030 TEST(StringUtilTest, StringToDouble) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1058 {"", 0.0, false}, | 1062 {"", 0.0, false}, |
| 1059 }; | 1063 }; |
| 1060 | 1064 |
| 1061 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 1065 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 1062 EXPECT_DOUBLE_EQ(cases[i].output, StringToDouble(cases[i].input)); | 1066 EXPECT_DOUBLE_EQ(cases[i].output, StringToDouble(cases[i].input)); |
| 1063 double output; | 1067 double output; |
| 1064 EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output)); | 1068 EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output)); |
| 1065 EXPECT_DOUBLE_EQ(cases[i].output, output); | 1069 EXPECT_DOUBLE_EQ(cases[i].output, output); |
| 1066 | 1070 |
| 1067 std::wstring wide_input = ASCIIToWide(cases[i].input); | 1071 std::wstring wide_input = ASCIIToWide(cases[i].input); |
| 1068 EXPECT_DOUBLE_EQ(cases[i].output, StringToDouble(wide_input)); | 1072 EXPECT_DOUBLE_EQ(cases[i].output, |
| 1069 EXPECT_EQ(cases[i].success, StringToDouble(wide_input, &output)); | 1073 StringToDouble(WideToUTF16Hack(wide_input))); |
| 1074 EXPECT_EQ(cases[i].success, StringToDouble(WideToUTF16Hack(wide_input), |
| 1075 &output)); |
| 1070 EXPECT_DOUBLE_EQ(cases[i].output, output); | 1076 EXPECT_DOUBLE_EQ(cases[i].output, output); |
| 1071 } | 1077 } |
| 1072 | 1078 |
| 1073 // One additional test to verify that conversion of numbers in strings with | 1079 // One additional test to verify that conversion of numbers in strings with |
| 1074 // embedded NUL characters. The NUL and extra data after it should be | 1080 // embedded NUL characters. The NUL and extra data after it should be |
| 1075 // interpreted as junk after the number. | 1081 // interpreted as junk after the number. |
| 1076 const char input[] = "3.14\0159"; | 1082 const char input[] = "3.14\0159"; |
| 1077 std::string input_string(input, arraysize(input) - 1); | 1083 std::string input_string(input, arraysize(input) - 1); |
| 1078 double output; | 1084 double output; |
| 1079 EXPECT_FALSE(StringToDouble(input_string, &output)); | 1085 EXPECT_FALSE(StringToDouble(input_string, &output)); |
| 1080 EXPECT_DOUBLE_EQ(3.14, output); | 1086 EXPECT_DOUBLE_EQ(3.14, output); |
| 1081 | 1087 |
| 1082 std::wstring wide_input = ASCIIToWide(input_string); | 1088 std::wstring wide_input = ASCIIToWide(input_string); |
| 1083 EXPECT_FALSE(StringToDouble(wide_input, &output)); | 1089 EXPECT_FALSE(StringToDouble(WideToUTF16Hack(wide_input), &output)); |
| 1084 EXPECT_DOUBLE_EQ(3.14, output); | 1090 EXPECT_DOUBLE_EQ(3.14, output); |
| 1085 } | 1091 } |
| 1086 | 1092 |
| 1087 // This checks where we can use the assignment operator for a va_list. We need | 1093 // This checks where we can use the assignment operator for a va_list. We need |
| 1088 // a way to do this since Visual C doesn't support va_copy, but assignment on | 1094 // a way to do this since Visual C doesn't support va_copy, but assignment on |
| 1089 // va_list is not guaranteed to be a copy. See StringAppendVT which uses this | 1095 // va_list is not guaranteed to be a copy. See StringAppendVT which uses this |
| 1090 // capability. | 1096 // capability. |
| 1091 static void VariableArgsFunc(const char* format, ...) { | 1097 static void VariableArgsFunc(const char* format, ...) { |
| 1092 va_list org; | 1098 va_list org; |
| 1093 va_start(org, format); | 1099 va_start(org, format); |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1548 } | 1554 } |
| 1549 } | 1555 } |
| 1550 | 1556 |
| 1551 TEST(StringUtilTest, HexEncode) { | 1557 TEST(StringUtilTest, HexEncode) { |
| 1552 std::string hex(HexEncode(NULL, 0)); | 1558 std::string hex(HexEncode(NULL, 0)); |
| 1553 EXPECT_EQ(hex.length(), 0U); | 1559 EXPECT_EQ(hex.length(), 0U); |
| 1554 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 1560 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 1555 hex = HexEncode(bytes, sizeof(bytes)); | 1561 hex = HexEncode(bytes, sizeof(bytes)); |
| 1556 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 1562 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 1557 } | 1563 } |
| OLD | NEW |