OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_util.h" | 5 #include "base/strings/string_util.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stdarg.h> | 8 #include <stdarg.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 // Convert empty strings. | 487 // Convert empty strings. |
488 string16 empty16; | 488 string16 empty16; |
489 std::string empty; | 489 std::string empty; |
490 EXPECT_EQ(empty, UTF16ToASCII(empty16)); | 490 EXPECT_EQ(empty, UTF16ToASCII(empty16)); |
491 EXPECT_EQ(empty16, ASCIIToUTF16(empty)); | 491 EXPECT_EQ(empty16, ASCIIToUTF16(empty)); |
492 | 492 |
493 // Convert strings with an embedded NUL character. | 493 // Convert strings with an embedded NUL character. |
494 const char chars_with_nul[] = "test\0string"; | 494 const char chars_with_nul[] = "test\0string"; |
495 const int length_with_nul = arraysize(chars_with_nul) - 1; | 495 const int length_with_nul = arraysize(chars_with_nul) - 1; |
496 std::string string_with_nul(chars_with_nul, length_with_nul); | 496 std::string string_with_nul(chars_with_nul, length_with_nul); |
497 base::string16 string16_with_nul = ASCIIToUTF16(string_with_nul); | 497 string16 string16_with_nul = ASCIIToUTF16(string_with_nul); |
498 EXPECT_EQ(static_cast<base::string16::size_type>(length_with_nul), | 498 EXPECT_EQ(static_cast<string16::size_type>(length_with_nul), |
499 string16_with_nul.length()); | 499 string16_with_nul.length()); |
500 std::string narrow_with_nul = UTF16ToASCII(string16_with_nul); | 500 std::string narrow_with_nul = UTF16ToASCII(string16_with_nul); |
501 EXPECT_EQ(static_cast<std::string::size_type>(length_with_nul), | 501 EXPECT_EQ(static_cast<std::string::size_type>(length_with_nul), |
502 narrow_with_nul.length()); | 502 narrow_with_nul.length()); |
503 EXPECT_EQ(0, string_with_nul.compare(narrow_with_nul)); | 503 EXPECT_EQ(0, string_with_nul.compare(narrow_with_nul)); |
504 } | 504 } |
505 | 505 |
506 TEST(StringUtilTest, ToUpperASCII) { | 506 TEST(StringUtilTest, ToUpperASCII) { |
507 EXPECT_EQ('C', ToUpperASCII('C')); | 507 EXPECT_EQ('C', ToUpperASCII('C')); |
508 EXPECT_EQ('C', ToUpperASCII('c')); | 508 EXPECT_EQ('C', ToUpperASCII('c')); |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 EXPECT_EQ(r[0], STR("\ta\t")); | 761 EXPECT_EQ(r[0], STR("\ta\t")); |
762 EXPECT_EQ(r[1], STR("b\tcc")); | 762 EXPECT_EQ(r[1], STR("b\tcc")); |
763 r.clear(); | 763 r.clear(); |
764 } | 764 } |
765 | 765 |
766 TEST(StringUtilTest, TokenizeStdString) { | 766 TEST(StringUtilTest, TokenizeStdString) { |
767 TokenizeTest<std::string>(); | 767 TokenizeTest<std::string>(); |
768 } | 768 } |
769 | 769 |
770 TEST(StringUtilTest, TokenizeStringPiece) { | 770 TEST(StringUtilTest, TokenizeStringPiece) { |
771 TokenizeTest<base::StringPiece>(); | 771 TokenizeTest<StringPiece>(); |
772 } | 772 } |
773 | 773 |
774 // Test for JoinString | 774 // Test for JoinString |
775 TEST(StringUtilTest, JoinString) { | 775 TEST(StringUtilTest, JoinString) { |
776 std::vector<std::string> in; | 776 std::vector<std::string> in; |
777 EXPECT_EQ("", JoinString(in, ',')); | 777 EXPECT_EQ("", JoinString(in, ',')); |
778 | 778 |
779 in.push_back("a"); | 779 in.push_back("a"); |
780 EXPECT_EQ("a", JoinString(in, ',')); | 780 EXPECT_EQ("a", JoinString(in, ',')); |
781 | 781 |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1031 // recursion depth). | 1031 // recursion depth). |
1032 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello"), | 1032 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello"), |
1033 UTF8ToUTF16("He********************************o"))); | 1033 UTF8ToUTF16("He********************************o"))); |
1034 } | 1034 } |
1035 | 1035 |
1036 TEST(StringUtilTest, LcpyTest) { | 1036 TEST(StringUtilTest, LcpyTest) { |
1037 // Test the normal case where we fit in our buffer. | 1037 // Test the normal case where we fit in our buffer. |
1038 { | 1038 { |
1039 char dst[10]; | 1039 char dst[10]; |
1040 wchar_t wdst[10]; | 1040 wchar_t wdst[10]; |
1041 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", arraysize(dst))); | 1041 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst))); |
1042 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); | 1042 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); |
1043 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst))); | 1043 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst))); |
1044 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); | 1044 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); |
1045 } | 1045 } |
1046 | 1046 |
1047 // Test dst_size == 0, nothing should be written to |dst| and we should | 1047 // Test dst_size == 0, nothing should be written to |dst| and we should |
1048 // have the equivalent of strlen(src). | 1048 // have the equivalent of strlen(src). |
1049 { | 1049 { |
1050 char dst[2] = {1, 2}; | 1050 char dst[2] = {1, 2}; |
1051 wchar_t wdst[2] = {1, 2}; | 1051 wchar_t wdst[2] = {1, 2}; |
1052 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", 0)); | 1052 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", 0)); |
1053 EXPECT_EQ(1, dst[0]); | 1053 EXPECT_EQ(1, dst[0]); |
1054 EXPECT_EQ(2, dst[1]); | 1054 EXPECT_EQ(2, dst[1]); |
1055 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", 0)); | 1055 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", 0)); |
1056 EXPECT_EQ(static_cast<wchar_t>(1), wdst[0]); | 1056 EXPECT_EQ(static_cast<wchar_t>(1), wdst[0]); |
1057 EXPECT_EQ(static_cast<wchar_t>(2), wdst[1]); | 1057 EXPECT_EQ(static_cast<wchar_t>(2), wdst[1]); |
1058 } | 1058 } |
1059 | 1059 |
1060 // Test the case were we _just_ competely fit including the null. | 1060 // Test the case were we _just_ competely fit including the null. |
1061 { | 1061 { |
1062 char dst[8]; | 1062 char dst[8]; |
1063 wchar_t wdst[8]; | 1063 wchar_t wdst[8]; |
1064 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", arraysize(dst))); | 1064 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst))); |
1065 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); | 1065 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); |
1066 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst))); | 1066 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst))); |
1067 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); | 1067 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); |
1068 } | 1068 } |
1069 | 1069 |
1070 // Test the case were we we are one smaller, so we can't fit the null. | 1070 // Test the case were we we are one smaller, so we can't fit the null. |
1071 { | 1071 { |
1072 char dst[7]; | 1072 char dst[7]; |
1073 wchar_t wdst[7]; | 1073 wchar_t wdst[7]; |
1074 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", arraysize(dst))); | 1074 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst))); |
1075 EXPECT_EQ(0, memcmp(dst, "abcdef", 7)); | 1075 EXPECT_EQ(0, memcmp(dst, "abcdef", 7)); |
1076 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst))); | 1076 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst))); |
1077 EXPECT_EQ(0, memcmp(wdst, L"abcdef", sizeof(wchar_t) * 7)); | 1077 EXPECT_EQ(0, memcmp(wdst, L"abcdef", sizeof(wchar_t) * 7)); |
1078 } | 1078 } |
1079 | 1079 |
1080 // Test the case were we are just too small. | 1080 // Test the case were we are just too small. |
1081 { | 1081 { |
1082 char dst[3]; | 1082 char dst[3]; |
1083 wchar_t wdst[3]; | 1083 wchar_t wdst[3]; |
1084 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", arraysize(dst))); | 1084 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst))); |
1085 EXPECT_EQ(0, memcmp(dst, "ab", 3)); | 1085 EXPECT_EQ(0, memcmp(dst, "ab", 3)); |
1086 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst))); | 1086 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst))); |
1087 EXPECT_EQ(0, memcmp(wdst, L"ab", sizeof(wchar_t) * 3)); | 1087 EXPECT_EQ(0, memcmp(wdst, L"ab", sizeof(wchar_t) * 3)); |
1088 } | 1088 } |
1089 } | 1089 } |
1090 | 1090 |
1091 TEST(StringUtilTest, WprintfFormatPortabilityTest) { | 1091 TEST(StringUtilTest, WprintfFormatPortabilityTest) { |
1092 static const struct { | 1092 static const struct { |
1093 const wchar_t* input; | 1093 const wchar_t* input; |
1094 bool portable; | 1094 bool portable; |
1095 } cases[] = { | 1095 } cases[] = { |
1096 { L"%ls", true }, | 1096 { L"%ls", true }, |
(...skipping 12 matching lines...) Expand all Loading... |
1109 { L"%f %F", false }, | 1109 { L"%f %F", false }, |
1110 { L"%d %D", false }, | 1110 { L"%d %D", false }, |
1111 { L"%o %O", false }, | 1111 { L"%o %O", false }, |
1112 { L"%u %U", false }, | 1112 { L"%u %U", false }, |
1113 { L"%f %d %o %u", true }, | 1113 { L"%f %d %o %u", true }, |
1114 { L"%-8d (%02.1f%)", true }, | 1114 { L"%-8d (%02.1f%)", true }, |
1115 { L"% 10s", false }, | 1115 { L"% 10s", false }, |
1116 { L"% 10ls", true } | 1116 { L"% 10ls", true } |
1117 }; | 1117 }; |
1118 for (size_t i = 0; i < arraysize(cases); ++i) | 1118 for (size_t i = 0; i < arraysize(cases); ++i) |
1119 EXPECT_EQ(cases[i].portable, base::IsWprintfFormatPortable(cases[i].input)); | 1119 EXPECT_EQ(cases[i].portable, IsWprintfFormatPortable(cases[i].input)); |
1120 } | 1120 } |
1121 | 1121 |
1122 TEST(StringUtilTest, RemoveChars) { | 1122 TEST(StringUtilTest, RemoveChars) { |
1123 const char kRemoveChars[] = "-/+*"; | 1123 const char kRemoveChars[] = "-/+*"; |
1124 std::string input = "A-+bc/d!*"; | 1124 std::string input = "A-+bc/d!*"; |
1125 EXPECT_TRUE(RemoveChars(input, kRemoveChars, &input)); | 1125 EXPECT_TRUE(RemoveChars(input, kRemoveChars, &input)); |
1126 EXPECT_EQ("Abcd!", input); | 1126 EXPECT_EQ("Abcd!", input); |
1127 | 1127 |
1128 // No characters match kRemoveChars. | 1128 // No characters match kRemoveChars. |
1129 EXPECT_FALSE(RemoveChars(input, kRemoveChars, &input)); | 1129 EXPECT_FALSE(RemoveChars(input, kRemoveChars, &input)); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1226 const std::string live = kLive; | 1226 const std::string live = kLive; |
1227 std::string dead = live; | 1227 std::string dead = live; |
1228 strncpy(WriteInto(&dead, 5), kDead, 4); | 1228 strncpy(WriteInto(&dead, 5), kDead, 4); |
1229 EXPECT_EQ(kDead, dead); | 1229 EXPECT_EQ(kDead, dead); |
1230 EXPECT_EQ(4u, dead.size()); | 1230 EXPECT_EQ(4u, dead.size()); |
1231 EXPECT_EQ(kLive, live); | 1231 EXPECT_EQ(kLive, live); |
1232 EXPECT_EQ(4u, live.size()); | 1232 EXPECT_EQ(4u, live.size()); |
1233 } | 1233 } |
1234 | 1234 |
1235 } // namespace base | 1235 } // namespace base |
OLD | NEW |