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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 {"", TRIM_ALL, "", TRIM_NONE}, | 50 {"", TRIM_ALL, "", TRIM_NONE}, |
51 {" ", TRIM_LEADING, "", TRIM_LEADING}, | 51 {" ", TRIM_LEADING, "", TRIM_LEADING}, |
52 {" ", TRIM_TRAILING, "", TRIM_TRAILING}, | 52 {" ", TRIM_TRAILING, "", TRIM_TRAILING}, |
53 {" ", TRIM_ALL, "", TRIM_ALL}, | 53 {" ", TRIM_ALL, "", TRIM_ALL}, |
54 {"\t\rTest String\n", TRIM_ALL, "Test String", TRIM_ALL}, | 54 {"\t\rTest String\n", TRIM_ALL, "Test String", TRIM_ALL}, |
55 }; | 55 }; |
56 | 56 |
57 namespace { | 57 namespace { |
58 | 58 |
59 // Helper used to test TruncateUTF8ToByteSize. | 59 // Helper used to test TruncateUTF8ToByteSize. |
60 bool Truncated(const std::string& input, const size_t byte_size, | 60 bool Truncated(const std::string& input, |
| 61 const size_t byte_size, |
61 std::string* output) { | 62 std::string* output) { |
62 size_t prev = input.length(); | 63 size_t prev = input.length(); |
63 TruncateUTF8ToByteSize(input, byte_size, output); | 64 TruncateUTF8ToByteSize(input, byte_size, output); |
64 return prev != output->length(); | 65 return prev != output->length(); |
65 } | 66 } |
66 | 67 |
67 } // namespace | 68 } // namespace |
68 | 69 |
69 TEST(StringUtilTest, TruncateUTF8ToByteSize) { | 70 TEST(StringUtilTest, TruncateUTF8ToByteSize) { |
70 std::string output; | 71 std::string output; |
71 | 72 |
72 // Empty strings and invalid byte_size arguments | 73 // Empty strings and invalid byte_size arguments |
73 EXPECT_FALSE(Truncated(std::string(), 0, &output)); | 74 EXPECT_FALSE(Truncated(std::string(), 0, &output)); |
74 EXPECT_EQ(output, ""); | 75 EXPECT_EQ(output, ""); |
75 EXPECT_TRUE(Truncated("\xe1\x80\xbf", 0, &output)); | 76 EXPECT_TRUE(Truncated("\xe1\x80\xbf", 0, &output)); |
76 EXPECT_EQ(output, ""); | 77 EXPECT_EQ(output, ""); |
77 EXPECT_FALSE(Truncated("\xe1\x80\xbf", -1, &output)); | 78 EXPECT_FALSE(Truncated("\xe1\x80\xbf", static_cast<size_t>(-1), &output)); |
78 EXPECT_FALSE(Truncated("\xe1\x80\xbf", 4, &output)); | 79 EXPECT_FALSE(Truncated("\xe1\x80\xbf", 4, &output)); |
79 | 80 |
80 // Testing the truncation of valid UTF8 correctly | 81 // Testing the truncation of valid UTF8 correctly |
81 EXPECT_TRUE(Truncated("abc", 2, &output)); | 82 EXPECT_TRUE(Truncated("abc", 2, &output)); |
82 EXPECT_EQ(output, "ab"); | 83 EXPECT_EQ(output, "ab"); |
83 EXPECT_TRUE(Truncated("\xc2\x81\xc2\x81", 2, &output)); | 84 EXPECT_TRUE(Truncated("\xc2\x81\xc2\x81", 2, &output)); |
84 EXPECT_EQ(output.compare("\xc2\x81"), 0); | 85 EXPECT_EQ(output.compare("\xc2\x81"), 0); |
85 EXPECT_TRUE(Truncated("\xc2\x81\xc2\x81", 3, &output)); | 86 EXPECT_TRUE(Truncated("\xc2\x81\xc2\x81", 3, &output)); |
86 EXPECT_EQ(output.compare("\xc2\x81"), 0); | 87 EXPECT_EQ(output.compare("\xc2\x81"), 0); |
87 EXPECT_FALSE(Truncated("\xc2\x81\xc2\x81", 4, &output)); | 88 EXPECT_FALSE(Truncated("\xc2\x81\xc2\x81", 4, &output)); |
(...skipping 1096 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1184 const std::string live = kLive; | 1185 const std::string live = kLive; |
1185 std::string dead = live; | 1186 std::string dead = live; |
1186 strncpy(WriteInto(&dead, 5), kDead, 4); | 1187 strncpy(WriteInto(&dead, 5), kDead, 4); |
1187 EXPECT_EQ(kDead, dead); | 1188 EXPECT_EQ(kDead, dead); |
1188 EXPECT_EQ(4u, dead.size()); | 1189 EXPECT_EQ(4u, dead.size()); |
1189 EXPECT_EQ(kLive, live); | 1190 EXPECT_EQ(kLive, live); |
1190 EXPECT_EQ(4u, live.size()); | 1191 EXPECT_EQ(4u, live.size()); |
1191 } | 1192 } |
1192 | 1193 |
1193 } // namespace base | 1194 } // namespace base |
OLD | NEW |