Index: base/strings/string_util_unittest.cc |
diff --git a/base/strings/string_util_unittest.cc b/base/strings/string_util_unittest.cc |
index 0928c0f9d2237e6339b5489017888093022b9911..16448467901e442c11966d92c3dae421f9bc48c3 100644 |
--- a/base/strings/string_util_unittest.cc |
+++ b/base/strings/string_util_unittest.cc |
@@ -386,6 +386,55 @@ TEST(StringUtilTest, IsStringUTF8) { |
EXPECT_FALSE(IsStringUTF8("embedded\xc0\x80U+0000")); |
} |
+TEST(StringUtilTest, IsStringASCII) { |
+ static char char_ascii[] = |
+ "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; |
+ static char16 char16_ascii[] = { |
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', |
+ 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', |
+ '7', '8', '9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 0 }; |
+ |
+ // Test a variety of the fragment start positions and lengths in order to make |
+ // sure that bit masking in IsStringASCII works correctly. |
+ // Also, test that a non-ASCII character will be detected regardless of its |
+ // position inside the string. |
+ { |
+ const size_t string_length = arraysize(char_ascii) - 1; |
+ for (size_t offset = 0; offset < 8; ++offset) { |
+ for (size_t len = 0, max_len = string_length - offset; len < max_len; |
+ ++len) { |
+ EXPECT_TRUE(IsStringASCII(StringPiece(char_ascii + offset, len))); |
+ for (size_t char_pos = offset; char_pos < len; ++char_pos) { |
+ char_ascii[char_pos] |= '\x80'; |
+ EXPECT_FALSE(IsStringASCII(StringPiece(char_ascii + offset, len))); |
+ char_ascii[char_pos] &= ~'\x80'; |
+ } |
+ } |
+ } |
+ } |
+ |
+ { |
+ const size_t string_length = arraysize(char16_ascii) - 1; |
+ for (size_t offset = 0; offset < 4; ++offset) { |
+ for (size_t len = 0, max_len = string_length - offset; len < max_len; |
+ ++len) { |
+ EXPECT_TRUE(IsStringASCII(StringPiece16(char16_ascii + offset, len))); |
+ for (size_t char_pos = offset; char_pos < len; ++char_pos) { |
+ char16_ascii[char_pos] |= 0x80; |
+ EXPECT_FALSE( |
+ IsStringASCII(StringPiece16(char16_ascii + offset, len))); |
+ char16_ascii[char_pos] &= ~0x80; |
+ // Also test when the upper half is non-zero. |
+ char16_ascii[char_pos] |= 0x100; |
+ EXPECT_FALSE( |
+ IsStringASCII(StringPiece16(char16_ascii + offset, len))); |
+ char16_ascii[char_pos] &= ~0x100; |
+ } |
+ } |
+ } |
+ } |
+} |
+ |
TEST(StringUtilTest, ConvertASCII) { |
static const char* char_cases[] = { |
"Google Video", |