Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: base/string_util_unittest.cc

Issue 3056029: Move the number conversions from string_util to a new file.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/string_number_conversions_unittest.cc ('k') | base/version.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 615 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 626
627 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { 627 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
628 string16 str = ASCIIToUTF16(cases[i].str); 628 string16 str = ASCIIToUTF16(cases[i].str);
629 ReplaceFirstSubstringAfterOffset(&str, cases[i].start_offset, 629 ReplaceFirstSubstringAfterOffset(&str, cases[i].start_offset,
630 ASCIIToUTF16(cases[i].find_this), 630 ASCIIToUTF16(cases[i].find_this),
631 ASCIIToUTF16(cases[i].replace_with)); 631 ASCIIToUTF16(cases[i].replace_with));
632 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); 632 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str);
633 } 633 }
634 } 634 }
635 635
636 namespace {
637
638 template <typename INT>
639 struct IntToStringTest {
640 INT num;
641 const char* sexpected;
642 const char* uexpected;
643 };
644
645 } // namespace
646
647 TEST(StringUtilTest, IntToString) {
648 static const IntToStringTest<int> int_tests[] = {
649 { 0, "0", "0" },
650 { -1, "-1", "4294967295" },
651 { std::numeric_limits<int>::max(), "2147483647", "2147483647" },
652 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" },
653 };
654 static const IntToStringTest<int64> int64_tests[] = {
655 { 0, "0", "0" },
656 { -1, "-1", "18446744073709551615" },
657 { std::numeric_limits<int64>::max(),
658 "9223372036854775807",
659 "9223372036854775807", },
660 { std::numeric_limits<int64>::min(),
661 "-9223372036854775808",
662 "9223372036854775808" },
663 };
664
665 for (size_t i = 0; i < arraysize(int_tests); ++i) {
666 const IntToStringTest<int>* test = &int_tests[i];
667 EXPECT_EQ(IntToString(test->num), test->sexpected);
668 EXPECT_EQ(IntToWString(test->num), UTF8ToWide(test->sexpected));
669 EXPECT_EQ(UintToString(test->num), test->uexpected);
670 EXPECT_EQ(UintToWString(test->num), UTF8ToWide(test->uexpected));
671 }
672 for (size_t i = 0; i < arraysize(int64_tests); ++i) {
673 const IntToStringTest<int64>* test = &int64_tests[i];
674 EXPECT_EQ(Int64ToString(test->num), test->sexpected);
675 EXPECT_EQ(Int64ToWString(test->num), UTF8ToWide(test->sexpected));
676 EXPECT_EQ(Uint64ToString(test->num), test->uexpected);
677 EXPECT_EQ(Uint64ToWString(test->num), UTF8ToWide(test->uexpected));
678 }
679 }
680
681 TEST(StringUtilTest, Uint64ToString) {
682 static const struct {
683 uint64 input;
684 std::string output;
685 } cases[] = {
686 {0, "0"},
687 {42, "42"},
688 {INT_MAX, "2147483647"},
689 {kuint64max, "18446744073709551615"},
690 };
691
692 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i)
693 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input));
694 }
695
696 TEST(StringUtilTest, StringToInt) {
697 static const struct {
698 std::string input;
699 int output;
700 bool success;
701 } cases[] = {
702 {"0", 0, true},
703 {"42", 42, true},
704 {"-2147483648", INT_MIN, true},
705 {"2147483647", INT_MAX, true},
706 {"", 0, false},
707 {" 42", 42, false},
708 {"42 ", 42, false},
709 {"\t\n\v\f\r 42", 42, false},
710 {"blah42", 0, false},
711 {"42blah", 42, false},
712 {"blah42blah", 0, false},
713 {"-273.15", -273, false},
714 {"+98.6", 98, false},
715 {"--123", 0, false},
716 {"++123", 0, false},
717 {"-+123", 0, false},
718 {"+-123", 0, false},
719 {"-", 0, false},
720 {"-2147483649", INT_MIN, false},
721 {"-99999999999", INT_MIN, false},
722 {"2147483648", INT_MAX, false},
723 {"99999999999", INT_MAX, false},
724 };
725
726 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
727 EXPECT_EQ(cases[i].output, StringToInt(cases[i].input));
728 int output;
729 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output));
730 EXPECT_EQ(cases[i].output, output);
731
732 std::wstring wide_input = ASCIIToWide(cases[i].input);
733 EXPECT_EQ(cases[i].output, StringToInt(WideToUTF16Hack(wide_input)));
734 EXPECT_EQ(cases[i].success, StringToInt(WideToUTF16Hack(wide_input),
735 &output));
736 EXPECT_EQ(cases[i].output, output);
737 }
738
739 // One additional test to verify that conversion of numbers in strings with
740 // embedded NUL characters. The NUL and extra data after it should be
741 // interpreted as junk after the number.
742 const char input[] = "6\06";
743 std::string input_string(input, arraysize(input) - 1);
744 int output;
745 EXPECT_FALSE(StringToInt(input_string, &output));
746 EXPECT_EQ(6, output);
747
748 std::wstring wide_input = ASCIIToWide(input_string);
749 EXPECT_FALSE(StringToInt(WideToUTF16Hack(wide_input), &output));
750 EXPECT_EQ(6, output);
751 }
752
753 TEST(StringUtilTest, StringToInt64) {
754 static const struct {
755 std::string input;
756 int64 output;
757 bool success;
758 } cases[] = {
759 {"0", 0, true},
760 {"42", 42, true},
761 {"-2147483648", INT_MIN, true},
762 {"2147483647", INT_MAX, true},
763 {"-2147483649", GG_INT64_C(-2147483649), true},
764 {"-99999999999", GG_INT64_C(-99999999999), true},
765 {"2147483648", GG_INT64_C(2147483648), true},
766 {"99999999999", GG_INT64_C(99999999999), true},
767 {"9223372036854775807", kint64max, true},
768 {"-9223372036854775808", kint64min, true},
769 {"09", 9, true},
770 {"-09", -9, true},
771 {"", 0, false},
772 {" 42", 42, false},
773 {"42 ", 42, false},
774 {"\t\n\v\f\r 42", 42, false},
775 {"blah42", 0, false},
776 {"42blah", 42, false},
777 {"blah42blah", 0, false},
778 {"-273.15", -273, false},
779 {"+98.6", 98, false},
780 {"--123", 0, false},
781 {"++123", 0, false},
782 {"-+123", 0, false},
783 {"+-123", 0, false},
784 {"-", 0, false},
785 {"-9223372036854775809", kint64min, false},
786 {"-99999999999999999999", kint64min, false},
787 {"9223372036854775808", kint64max, false},
788 {"99999999999999999999", kint64max, false},
789 };
790
791 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
792 EXPECT_EQ(cases[i].output, StringToInt64(cases[i].input));
793 int64 output;
794 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output));
795 EXPECT_EQ(cases[i].output, output);
796
797 std::wstring wide_input = ASCIIToWide(cases[i].input);
798 EXPECT_EQ(cases[i].output, StringToInt64(WideToUTF16Hack(wide_input)));
799 EXPECT_EQ(cases[i].success, StringToInt64(WideToUTF16Hack(wide_input),
800 &output));
801 EXPECT_EQ(cases[i].output, output);
802 }
803
804 // One additional test to verify that conversion of numbers in strings with
805 // embedded NUL characters. The NUL and extra data after it should be
806 // interpreted as junk after the number.
807 const char input[] = "6\06";
808 std::string input_string(input, arraysize(input) - 1);
809 int64 output;
810 EXPECT_FALSE(StringToInt64(input_string, &output));
811 EXPECT_EQ(6, output);
812
813 std::wstring wide_input = ASCIIToWide(input_string);
814 EXPECT_FALSE(StringToInt64(WideToUTF16Hack(wide_input), &output));
815 EXPECT_EQ(6, output);
816 }
817
818 TEST(StringUtilTest, HexStringToInt) {
819 static const struct {
820 std::string input;
821 int output;
822 bool success;
823 } cases[] = {
824 {"0", 0, true},
825 {"42", 66, true},
826 {"-42", -66, true},
827 {"+42", 66, true},
828 {"7fffffff", INT_MAX, true},
829 {"80000000", INT_MIN, true},
830 {"ffffffff", -1, true},
831 {"DeadBeef", 0xdeadbeef, true},
832 {"0x42", 66, true},
833 {"-0x42", -66, true},
834 {"+0x42", 66, true},
835 {"0x7fffffff", INT_MAX, true},
836 {"0x80000000", INT_MIN, true},
837 {"0xffffffff", -1, true},
838 {"0XDeadBeef", 0xdeadbeef, true},
839 {"0x0f", 15, true},
840 {"0f", 15, true},
841 {" 45", 0x45, false},
842 {"\t\n\v\f\r 0x45", 0x45, false},
843 {" 45", 0x45, false},
844 {"45 ", 0x45, false},
845 {"efgh", 0xef, false},
846 {"0xefgh", 0xef, false},
847 {"hgfe", 0, false},
848 {"100000000", -1, false}, // don't care about |output|, just |success|
849 {"-", 0, false},
850 {"", 0, false},
851 };
852
853 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
854 EXPECT_EQ(cases[i].output, HexStringToInt(cases[i].input));
855 int output;
856 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output));
857 EXPECT_EQ(cases[i].output, output);
858
859 std::wstring wide_input = ASCIIToWide(cases[i].input);
860 EXPECT_EQ(cases[i].output, HexStringToInt(WideToUTF16Hack(wide_input)));
861 EXPECT_EQ(cases[i].success, HexStringToInt(WideToUTF16Hack(wide_input),
862 &output));
863 EXPECT_EQ(cases[i].output, output);
864 }
865 // One additional test to verify that conversion of numbers in strings with
866 // embedded NUL characters. The NUL and extra data after it should be
867 // interpreted as junk after the number.
868 const char input[] = "0xc0ffee\09";
869 std::string input_string(input, arraysize(input) - 1);
870 int output;
871 EXPECT_FALSE(HexStringToInt(input_string, &output));
872 EXPECT_EQ(0xc0ffee, output);
873
874 std::wstring wide_input = ASCIIToWide(input_string);
875 EXPECT_FALSE(HexStringToInt(WideToUTF16Hack(wide_input), &output));
876 EXPECT_EQ(0xc0ffee, output);
877 }
878
879 TEST(StringUtilTest, HexStringToBytes) {
880 static const struct {
881 const std::string input;
882 const char* output;
883 size_t output_len;
884 bool success;
885 } cases[] = {
886 {"0", "", 0, false}, // odd number of characters fails
887 {"00", "\0", 1, true},
888 {"42", "\x42", 1, true},
889 {"-42", "", 0, false}, // any non-hex value fails
890 {"+42", "", 0, false},
891 {"7fffffff", "\x7f\xff\xff\xff", 4, true},
892 {"80000000", "\x80\0\0\0", 4, true},
893 {"deadbeef", "\xde\xad\xbe\xef", 4, true},
894 {"DeadBeef", "\xde\xad\xbe\xef", 4, true},
895 {"0x42", "", 0, false}, // leading 0x fails (x is not hex)
896 {"0f", "\xf", 1, true},
897 {"45 ", "\x45", 1, false},
898 {"efgh", "\xef", 1, false},
899 {"", "", 0, false},
900 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true},
901 {"0123456789ABCDEF012345",
902 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true},
903 };
904
905
906 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
907 std::vector<uint8> output;
908 std::vector<uint8> compare;
909 EXPECT_EQ(cases[i].success, HexStringToBytes(cases[i].input, &output)) <<
910 i << ": " << cases[i].input;
911 for (size_t j = 0; j < cases[i].output_len; ++j)
912 compare.push_back(static_cast<uint8>(cases[i].output[j]));
913 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input;
914 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) <<
915 i << ": " << cases[i].input;
916
917 output.clear();
918 compare.clear();
919
920 std::wstring wide_input = ASCIIToWide(cases[i].input);
921 EXPECT_EQ(cases[i].success,
922 HexStringToBytes(WideToUTF16Hack(wide_input), &output)) <<
923 i << ": " << cases[i].input;
924 for (size_t j = 0; j < cases[i].output_len; ++j)
925 compare.push_back(static_cast<uint8>(cases[i].output[j]));
926 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input;
927 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) <<
928 i << ": " << cases[i].input;
929 }
930 }
931
932 TEST(StringUtilTest, StringToDouble) {
933 static const struct {
934 std::string input;
935 double output;
936 bool success;
937 } cases[] = {
938 {"0", 0.0, true},
939 {"42", 42.0, true},
940 {"-42", -42.0, true},
941 {"123.45", 123.45, true},
942 {"-123.45", -123.45, true},
943 {"+123.45", 123.45, true},
944 {"2.99792458e8", 299792458.0, true},
945 {"149597870.691E+3", 149597870691.0, true},
946 {"6.", 6.0, true},
947 {"9e99999999999999999999", HUGE_VAL, false},
948 {"-9e99999999999999999999", -HUGE_VAL, false},
949 {"1e-2", 0.01, true},
950 {" 1e-2", 0.01, false},
951 {"1e-2 ", 0.01, false},
952 {"-1E-7", -0.0000001, true},
953 {"01e02", 100, true},
954 {"2.3e15", 2.3e15, true},
955 {"\t\n\v\f\r -123.45e2", -12345.0, false},
956 {"+123 e4", 123.0, false},
957 {"123e ", 123.0, false},
958 {"123e", 123.0, false},
959 {" 2.99", 2.99, false},
960 {"1e3.4", 1000.0, false},
961 {"nothing", 0.0, false},
962 {"-", 0.0, false},
963 {"+", 0.0, false},
964 {"", 0.0, false},
965 };
966
967 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
968 EXPECT_DOUBLE_EQ(cases[i].output, StringToDouble(cases[i].input));
969 double output;
970 EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output));
971 EXPECT_DOUBLE_EQ(cases[i].output, output);
972
973 std::wstring wide_input = ASCIIToWide(cases[i].input);
974 EXPECT_DOUBLE_EQ(cases[i].output,
975 StringToDouble(WideToUTF16Hack(wide_input)));
976 EXPECT_EQ(cases[i].success, StringToDouble(WideToUTF16Hack(wide_input),
977 &output));
978 EXPECT_DOUBLE_EQ(cases[i].output, output);
979 }
980
981 // One additional test to verify that conversion of numbers in strings with
982 // embedded NUL characters. The NUL and extra data after it should be
983 // interpreted as junk after the number.
984 const char input[] = "3.14\0159";
985 std::string input_string(input, arraysize(input) - 1);
986 double output;
987 EXPECT_FALSE(StringToDouble(input_string, &output));
988 EXPECT_DOUBLE_EQ(3.14, output);
989
990 std::wstring wide_input = ASCIIToWide(input_string);
991 EXPECT_FALSE(StringToDouble(WideToUTF16Hack(wide_input), &output));
992 EXPECT_DOUBLE_EQ(3.14, output);
993 }
994
995 // This checks where we can use the assignment operator for a va_list. We need 636 // This checks where we can use the assignment operator for a va_list. We need
996 // a way to do this since Visual C doesn't support va_copy, but assignment on 637 // a way to do this since Visual C doesn't support va_copy, but assignment on
997 // va_list is not guaranteed to be a copy. See StringAppendVT which uses this 638 // va_list is not guaranteed to be a copy. See StringAppendVT which uses this
998 // capability. 639 // capability.
999 static void VariableArgsFunc(const char* format, ...) { 640 static void VariableArgsFunc(const char* format, ...) {
1000 va_list org; 641 va_list org;
1001 va_start(org, format); 642 va_start(org, format);
1002 643
1003 va_list dup; 644 va_list dup;
1004 GG_VA_COPY(dup, org); 645 GG_VA_COPY(dup, org);
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
1648 { L"Hello, my name is Tom", 100, false, L"Hello, my name is Tom" } 1289 { L"Hello, my name is Tom", 100, false, L"Hello, my name is Tom" }
1649 }; 1290 };
1650 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { 1291 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
1651 std::wstring output; 1292 std::wstring output;
1652 EXPECT_EQ(cases[i].result, 1293 EXPECT_EQ(cases[i].result,
1653 ElideString(cases[i].input, cases[i].max_len, &output)); 1294 ElideString(cases[i].input, cases[i].max_len, &output));
1654 EXPECT_TRUE(output == cases[i].output); 1295 EXPECT_TRUE(output == cases[i].output);
1655 } 1296 }
1656 } 1297 }
1657 1298
1658 TEST(StringUtilTest, HexEncode) {
1659 std::string hex(HexEncode(NULL, 0));
1660 EXPECT_EQ(hex.length(), 0U);
1661 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
1662 hex = HexEncode(bytes, sizeof(bytes));
1663 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
1664 }
1665
1666 TEST(StringUtilTest, RemoveChars) { 1299 TEST(StringUtilTest, RemoveChars) {
1667 const char* kRemoveChars = "-/+*"; 1300 const char* kRemoveChars = "-/+*";
1668 std::string input = "A-+bc/d!*"; 1301 std::string input = "A-+bc/d!*";
1669 EXPECT_TRUE(RemoveChars(input, kRemoveChars, &input)); 1302 EXPECT_TRUE(RemoveChars(input, kRemoveChars, &input));
1670 EXPECT_EQ("Abcd!", input); 1303 EXPECT_EQ("Abcd!", input);
1671 1304
1672 // No characters match kRemoveChars. 1305 // No characters match kRemoveChars.
1673 EXPECT_FALSE(RemoveChars(input, kRemoveChars, &input)); 1306 EXPECT_FALSE(RemoveChars(input, kRemoveChars, &input));
1674 EXPECT_EQ("Abcd!", input); 1307 EXPECT_EQ("Abcd!", input);
1675 1308
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1731 SplitStringUsingSubstr( 1364 SplitStringUsingSubstr(
1732 "unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER", 1365 "unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER",
1733 "DELIMITER", 1366 "DELIMITER",
1734 &results); 1367 &results);
1735 ASSERT_EQ(7u, results.size()); 1368 ASSERT_EQ(7u, results.size());
1736 EXPECT_THAT( 1369 EXPECT_THAT(
1737 results, ElementsAre("un", "deux", "trois", "quatre", "", "", "")); 1370 results, ElementsAre("un", "deux", "trois", "quatre", "", "", ""));
1738 } 1371 }
1739 1372
1740 } // namespace base 1373 } // namespace base
OLDNEW
« no previous file with comments | « base/string_number_conversions_unittest.cc ('k') | base/version.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698