[email protected] | d41c9c5 | 2012-01-10 18:00:42 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 7 | #include "base/strings/string16.h" |
[email protected] | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 8 | #include "base/strings/string_piece.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
[email protected] | 4f74b78 | 2011-05-10 21:59:24 | [diff] [blame] | 12 | namespace base { |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 13 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 14 | template <typename T> |
| 15 | class CommonStringPieceTest : public ::testing::Test { |
| 16 | public: |
| 17 | static const T as_string(const char* input) { |
| 18 | return T(input); |
| 19 | } |
| 20 | static const T& as_string(const T& input) { |
| 21 | return input; |
| 22 | } |
| 23 | }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 25 | template <> |
| 26 | class CommonStringPieceTest<string16> : public ::testing::Test { |
| 27 | public: |
| 28 | static const string16 as_string(const char* input) { |
| 29 | return ASCIIToUTF16(input); |
| 30 | } |
| 31 | static const string16 as_string(const std::string& input) { |
| 32 | return ASCIIToUTF16(input); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | typedef ::testing::Types<std::string, string16> SupportedStringTypes; |
| 37 | |
| 38 | TYPED_TEST_CASE(CommonStringPieceTest, SupportedStringTypes); |
| 39 | |
| 40 | TYPED_TEST(CommonStringPieceTest, CheckComparisonOperators) { |
| 41 | #define CMP_Y(op, x, y) \ |
| 42 | { \ |
| 43 | TypeParam lhs(TestFixture::as_string(x)); \ |
| 44 | TypeParam rhs(TestFixture::as_string(y)); \ |
| 45 | ASSERT_TRUE( (BasicStringPiece<TypeParam>((lhs.c_str())) op \ |
| 46 | BasicStringPiece<TypeParam>((rhs.c_str())))); \ |
| 47 | ASSERT_TRUE( (BasicStringPiece<TypeParam>((lhs.c_str())).compare( \ |
| 48 | BasicStringPiece<TypeParam>((rhs.c_str()))) op 0)); \ |
| 49 | } |
| 50 | |
| 51 | #define CMP_N(op, x, y) \ |
| 52 | { \ |
| 53 | TypeParam lhs(TestFixture::as_string(x)); \ |
| 54 | TypeParam rhs(TestFixture::as_string(y)); \ |
| 55 | ASSERT_FALSE( (BasicStringPiece<TypeParam>((lhs.c_str())) op \ |
| 56 | BasicStringPiece<TypeParam>((rhs.c_str())))); \ |
| 57 | ASSERT_FALSE( (BasicStringPiece<TypeParam>((lhs.c_str())).compare( \ |
| 58 | BasicStringPiece<TypeParam>((rhs.c_str()))) op 0)); \ |
| 59 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | |
| 61 | CMP_Y(==, "", ""); |
| 62 | CMP_Y(==, "a", "a"); |
| 63 | CMP_Y(==, "aa", "aa"); |
| 64 | CMP_N(==, "a", ""); |
| 65 | CMP_N(==, "", "a"); |
| 66 | CMP_N(==, "a", "b"); |
| 67 | CMP_N(==, "a", "aa"); |
| 68 | CMP_N(==, "aa", "a"); |
| 69 | |
| 70 | CMP_N(!=, "", ""); |
| 71 | CMP_N(!=, "a", "a"); |
| 72 | CMP_N(!=, "aa", "aa"); |
| 73 | CMP_Y(!=, "a", ""); |
| 74 | CMP_Y(!=, "", "a"); |
| 75 | CMP_Y(!=, "a", "b"); |
| 76 | CMP_Y(!=, "a", "aa"); |
| 77 | CMP_Y(!=, "aa", "a"); |
| 78 | |
| 79 | CMP_Y(<, "a", "b"); |
| 80 | CMP_Y(<, "a", "aa"); |
| 81 | CMP_Y(<, "aa", "b"); |
| 82 | CMP_Y(<, "aa", "bb"); |
| 83 | CMP_N(<, "a", "a"); |
| 84 | CMP_N(<, "b", "a"); |
| 85 | CMP_N(<, "aa", "a"); |
| 86 | CMP_N(<, "b", "aa"); |
| 87 | CMP_N(<, "bb", "aa"); |
| 88 | |
| 89 | CMP_Y(<=, "a", "a"); |
| 90 | CMP_Y(<=, "a", "b"); |
| 91 | CMP_Y(<=, "a", "aa"); |
| 92 | CMP_Y(<=, "aa", "b"); |
| 93 | CMP_Y(<=, "aa", "bb"); |
| 94 | CMP_N(<=, "b", "a"); |
| 95 | CMP_N(<=, "aa", "a"); |
| 96 | CMP_N(<=, "b", "aa"); |
| 97 | CMP_N(<=, "bb", "aa"); |
| 98 | |
| 99 | CMP_N(>=, "a", "b"); |
| 100 | CMP_N(>=, "a", "aa"); |
| 101 | CMP_N(>=, "aa", "b"); |
| 102 | CMP_N(>=, "aa", "bb"); |
| 103 | CMP_Y(>=, "a", "a"); |
| 104 | CMP_Y(>=, "b", "a"); |
| 105 | CMP_Y(>=, "aa", "a"); |
| 106 | CMP_Y(>=, "b", "aa"); |
| 107 | CMP_Y(>=, "bb", "aa"); |
| 108 | |
| 109 | CMP_N(>, "a", "a"); |
| 110 | CMP_N(>, "a", "b"); |
| 111 | CMP_N(>, "a", "aa"); |
| 112 | CMP_N(>, "aa", "b"); |
| 113 | CMP_N(>, "aa", "bb"); |
| 114 | CMP_Y(>, "b", "a"); |
| 115 | CMP_Y(>, "aa", "a"); |
| 116 | CMP_Y(>, "b", "aa"); |
| 117 | CMP_Y(>, "bb", "aa"); |
| 118 | |
| 119 | std::string x; |
| 120 | for (int i = 0; i < 256; i++) { |
| 121 | x += 'a'; |
| 122 | std::string y = x; |
| 123 | CMP_Y(==, x, y); |
| 124 | for (int j = 0; j < i; j++) { |
| 125 | std::string z = x; |
| 126 | z[j] = 'b'; // Differs in position 'j' |
| 127 | CMP_N(==, x, z); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #undef CMP_Y |
| 132 | #undef CMP_N |
| 133 | } |
| 134 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 135 | TYPED_TEST(CommonStringPieceTest, CheckSTL) { |
| 136 | TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz")); |
| 137 | TypeParam abc(TestFixture::as_string("abc")); |
| 138 | TypeParam xyz(TestFixture::as_string("xyz")); |
| 139 | TypeParam foobar(TestFixture::as_string("foobar")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 141 | BasicStringPiece<TypeParam> a(alphabet); |
| 142 | BasicStringPiece<TypeParam> b(abc); |
| 143 | BasicStringPiece<TypeParam> c(xyz); |
| 144 | BasicStringPiece<TypeParam> d(foobar); |
| 145 | BasicStringPiece<TypeParam> e; |
| 146 | TypeParam temp(TestFixture::as_string("123")); |
| 147 | temp += static_cast<typename TypeParam::value_type>(0); |
| 148 | temp += TestFixture::as_string("456"); |
| 149 | BasicStringPiece<TypeParam> f(temp); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 151 | ASSERT_EQ(a[6], static_cast<typename TypeParam::value_type>('g')); |
| 152 | ASSERT_EQ(b[0], static_cast<typename TypeParam::value_type>('a')); |
| 153 | ASSERT_EQ(c[2], static_cast<typename TypeParam::value_type>('z')); |
| 154 | ASSERT_EQ(f[3], static_cast<typename TypeParam::value_type>('\0')); |
| 155 | ASSERT_EQ(f[5], static_cast<typename TypeParam::value_type>('5')); |
| 156 | |
| 157 | ASSERT_EQ(*d.data(), static_cast<typename TypeParam::value_type>('f')); |
| 158 | ASSERT_EQ(d.data()[5], static_cast<typename TypeParam::value_type>('r')); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 159 | ASSERT_TRUE(e.data() == NULL); |
| 160 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 161 | ASSERT_EQ(*a.begin(), static_cast<typename TypeParam::value_type>('a')); |
| 162 | ASSERT_EQ(*(b.begin() + 2), static_cast<typename TypeParam::value_type>('c')); |
| 163 | ASSERT_EQ(*(c.end() - 1), static_cast<typename TypeParam::value_type>('z')); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 164 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 165 | ASSERT_EQ(*a.rbegin(), static_cast<typename TypeParam::value_type>('z')); |
| 166 | ASSERT_EQ(*(b.rbegin() + 2), |
| 167 | static_cast<typename TypeParam::value_type>('a')); |
| 168 | ASSERT_EQ(*(c.rend() - 1), static_cast<typename TypeParam::value_type>('x')); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | ASSERT_TRUE(a.rbegin() + 26 == a.rend()); |
| 170 | |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 171 | ASSERT_EQ(a.size(), 26U); |
| 172 | ASSERT_EQ(b.size(), 3U); |
| 173 | ASSERT_EQ(c.size(), 3U); |
| 174 | ASSERT_EQ(d.size(), 6U); |
| 175 | ASSERT_EQ(e.size(), 0U); |
| 176 | ASSERT_EQ(f.size(), 7U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | |
| 178 | ASSERT_TRUE(!d.empty()); |
| 179 | ASSERT_TRUE(d.begin() != d.end()); |
| 180 | ASSERT_TRUE(d.begin() + 6 == d.end()); |
| 181 | |
| 182 | ASSERT_TRUE(e.empty()); |
| 183 | ASSERT_TRUE(e.begin() == e.end()); |
| 184 | |
| 185 | d.clear(); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 186 | ASSERT_EQ(d.size(), 0U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 187 | ASSERT_TRUE(d.empty()); |
| 188 | ASSERT_TRUE(d.data() == NULL); |
| 189 | ASSERT_TRUE(d.begin() == d.end()); |
| 190 | |
| 191 | ASSERT_GE(a.max_size(), a.capacity()); |
| 192 | ASSERT_GE(a.capacity(), a.size()); |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 195 | TYPED_TEST(CommonStringPieceTest, CheckFind) { |
| 196 | typedef BasicStringPiece<TypeParam> Piece; |
[email protected] | 51cb94c | 2014-03-06 17:54:33 | [diff] [blame] | 197 | |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 198 | TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz")); |
| 199 | TypeParam abc(TestFixture::as_string("abc")); |
| 200 | TypeParam xyz(TestFixture::as_string("xyz")); |
| 201 | TypeParam foobar(TestFixture::as_string("foobar")); |
| 202 | |
| 203 | BasicStringPiece<TypeParam> a(alphabet); |
| 204 | BasicStringPiece<TypeParam> b(abc); |
| 205 | BasicStringPiece<TypeParam> c(xyz); |
| 206 | BasicStringPiece<TypeParam> d(foobar); |
| 207 | |
| 208 | d.clear(); |
| 209 | Piece e; |
| 210 | TypeParam temp(TestFixture::as_string("123")); |
| 211 | temp.push_back('\0'); |
| 212 | temp += TestFixture::as_string("456"); |
| 213 | Piece f(temp); |
| 214 | |
| 215 | typename TypeParam::value_type buf[4] = { '%', '%', '%', '%' }; |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 216 | ASSERT_EQ(a.copy(buf, 4), 4U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | ASSERT_EQ(buf[0], a[0]); |
| 218 | ASSERT_EQ(buf[1], a[1]); |
| 219 | ASSERT_EQ(buf[2], a[2]); |
| 220 | ASSERT_EQ(buf[3], a[3]); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 221 | ASSERT_EQ(a.copy(buf, 3, 7), 3U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 222 | ASSERT_EQ(buf[0], a[7]); |
| 223 | ASSERT_EQ(buf[1], a[8]); |
| 224 | ASSERT_EQ(buf[2], a[9]); |
| 225 | ASSERT_EQ(buf[3], a[3]); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 226 | ASSERT_EQ(c.copy(buf, 99), 3U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 227 | ASSERT_EQ(buf[0], c[0]); |
| 228 | ASSERT_EQ(buf[1], c[1]); |
| 229 | ASSERT_EQ(buf[2], c[2]); |
| 230 | ASSERT_EQ(buf[3], a[3]); |
| 231 | |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 232 | ASSERT_EQ(Piece::npos, TypeParam::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 233 | |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 234 | ASSERT_EQ(a.find(b), 0U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 235 | ASSERT_EQ(a.find(b, 1), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 236 | ASSERT_EQ(a.find(c), 23U); |
| 237 | ASSERT_EQ(a.find(c, 9), 23U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 238 | ASSERT_EQ(a.find(c, Piece::npos), Piece::npos); |
| 239 | ASSERT_EQ(b.find(c), Piece::npos); |
| 240 | ASSERT_EQ(b.find(c, Piece::npos), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 241 | ASSERT_EQ(a.find(d), 0U); |
| 242 | ASSERT_EQ(a.find(e), 0U); |
| 243 | ASSERT_EQ(a.find(d, 12), 12U); |
| 244 | ASSERT_EQ(a.find(e, 17), 17U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 245 | TypeParam not_found(TestFixture::as_string("xx not found bb")); |
| 246 | Piece g(not_found); |
| 247 | ASSERT_EQ(a.find(g), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 249 | ASSERT_EQ(d.find(b), Piece::npos); |
| 250 | ASSERT_EQ(e.find(b), Piece::npos); |
| 251 | ASSERT_EQ(d.find(b, 4), Piece::npos); |
| 252 | ASSERT_EQ(e.find(b, 7), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 253 | |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 254 | size_t empty_search_pos = TypeParam().find(TypeParam()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | ASSERT_EQ(d.find(d), empty_search_pos); |
| 256 | ASSERT_EQ(d.find(e), empty_search_pos); |
| 257 | ASSERT_EQ(e.find(d), empty_search_pos); |
| 258 | ASSERT_EQ(e.find(e), empty_search_pos); |
| 259 | ASSERT_EQ(d.find(d, 4), std::string().find(std::string(), 4)); |
| 260 | ASSERT_EQ(d.find(e, 4), std::string().find(std::string(), 4)); |
| 261 | ASSERT_EQ(e.find(d, 4), std::string().find(std::string(), 4)); |
| 262 | ASSERT_EQ(e.find(e, 4), std::string().find(std::string(), 4)); |
| 263 | |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 264 | ASSERT_EQ(a.find('a'), 0U); |
| 265 | ASSERT_EQ(a.find('c'), 2U); |
| 266 | ASSERT_EQ(a.find('z'), 25U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 267 | ASSERT_EQ(a.find('$'), Piece::npos); |
| 268 | ASSERT_EQ(a.find('\0'), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 269 | ASSERT_EQ(f.find('\0'), 3U); |
| 270 | ASSERT_EQ(f.find('3'), 2U); |
| 271 | ASSERT_EQ(f.find('5'), 5U); |
| 272 | ASSERT_EQ(g.find('o'), 4U); |
| 273 | ASSERT_EQ(g.find('o', 4), 4U); |
| 274 | ASSERT_EQ(g.find('o', 5), 8U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 275 | ASSERT_EQ(a.find('b', 5), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 276 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 277 | ASSERT_EQ(d.find('\0'), Piece::npos); |
| 278 | ASSERT_EQ(e.find('\0'), Piece::npos); |
| 279 | ASSERT_EQ(d.find('\0', 4), Piece::npos); |
| 280 | ASSERT_EQ(e.find('\0', 7), Piece::npos); |
| 281 | ASSERT_EQ(d.find('x'), Piece::npos); |
| 282 | ASSERT_EQ(e.find('x'), Piece::npos); |
| 283 | ASSERT_EQ(d.find('x', 4), Piece::npos); |
| 284 | ASSERT_EQ(e.find('x', 7), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 285 | |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 286 | ASSERT_EQ(a.rfind(b), 0U); |
| 287 | ASSERT_EQ(a.rfind(b, 1), 0U); |
| 288 | ASSERT_EQ(a.rfind(c), 23U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 289 | ASSERT_EQ(a.rfind(c, 22U), Piece::npos); |
| 290 | ASSERT_EQ(a.rfind(c, 1U), Piece::npos); |
| 291 | ASSERT_EQ(a.rfind(c, 0U), Piece::npos); |
| 292 | ASSERT_EQ(b.rfind(c), Piece::npos); |
| 293 | ASSERT_EQ(b.rfind(c, 0U), Piece::npos); |
| 294 | ASSERT_EQ(a.rfind(d), static_cast<size_t>(a.as_string().rfind(TypeParam()))); |
| 295 | ASSERT_EQ(a.rfind(e), a.as_string().rfind(TypeParam())); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 296 | ASSERT_EQ(a.rfind(d, 12), 12U); |
| 297 | ASSERT_EQ(a.rfind(e, 17), 17U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 298 | ASSERT_EQ(a.rfind(g), Piece::npos); |
| 299 | ASSERT_EQ(d.rfind(b), Piece::npos); |
| 300 | ASSERT_EQ(e.rfind(b), Piece::npos); |
| 301 | ASSERT_EQ(d.rfind(b, 4), Piece::npos); |
| 302 | ASSERT_EQ(e.rfind(b, 7), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 303 | // empty string nonsense |
| 304 | ASSERT_EQ(d.rfind(d, 4), std::string().rfind(std::string())); |
| 305 | ASSERT_EQ(e.rfind(d, 7), std::string().rfind(std::string())); |
| 306 | ASSERT_EQ(d.rfind(e, 4), std::string().rfind(std::string())); |
| 307 | ASSERT_EQ(e.rfind(e, 7), std::string().rfind(std::string())); |
| 308 | ASSERT_EQ(d.rfind(d), std::string().rfind(std::string())); |
| 309 | ASSERT_EQ(e.rfind(d), std::string().rfind(std::string())); |
| 310 | ASSERT_EQ(d.rfind(e), std::string().rfind(std::string())); |
| 311 | ASSERT_EQ(e.rfind(e), std::string().rfind(std::string())); |
| 312 | |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 313 | ASSERT_EQ(g.rfind('o'), 8U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 314 | ASSERT_EQ(g.rfind('q'), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 315 | ASSERT_EQ(g.rfind('o', 8), 8U); |
| 316 | ASSERT_EQ(g.rfind('o', 7), 4U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 317 | ASSERT_EQ(g.rfind('o', 3), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 318 | ASSERT_EQ(f.rfind('\0'), 3U); |
| 319 | ASSERT_EQ(f.rfind('\0', 12), 3U); |
| 320 | ASSERT_EQ(f.rfind('3'), 2U); |
| 321 | ASSERT_EQ(f.rfind('5'), 5U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 322 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 323 | ASSERT_EQ(d.rfind('o'), Piece::npos); |
| 324 | ASSERT_EQ(e.rfind('o'), Piece::npos); |
| 325 | ASSERT_EQ(d.rfind('o', 4), Piece::npos); |
| 326 | ASSERT_EQ(e.rfind('o', 7), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 327 | |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 328 | TypeParam one_two_three_four(TestFixture::as_string("one,two:three;four")); |
| 329 | TypeParam comma_colon(TestFixture::as_string(",:")); |
| 330 | ASSERT_EQ(3U, Piece(one_two_three_four).find_first_of(comma_colon)); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 331 | ASSERT_EQ(a.find_first_of(b), 0U); |
| 332 | ASSERT_EQ(a.find_first_of(b, 0), 0U); |
| 333 | ASSERT_EQ(a.find_first_of(b, 1), 1U); |
| 334 | ASSERT_EQ(a.find_first_of(b, 2), 2U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 335 | ASSERT_EQ(a.find_first_of(b, 3), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 336 | ASSERT_EQ(a.find_first_of(c), 23U); |
| 337 | ASSERT_EQ(a.find_first_of(c, 23), 23U); |
| 338 | ASSERT_EQ(a.find_first_of(c, 24), 24U); |
| 339 | ASSERT_EQ(a.find_first_of(c, 25), 25U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 340 | ASSERT_EQ(a.find_first_of(c, 26), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 341 | ASSERT_EQ(g.find_first_of(b), 13U); |
| 342 | ASSERT_EQ(g.find_first_of(c), 0U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 343 | ASSERT_EQ(a.find_first_of(f), Piece::npos); |
| 344 | ASSERT_EQ(f.find_first_of(a), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 345 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 346 | ASSERT_EQ(a.find_first_of(d), Piece::npos); |
| 347 | ASSERT_EQ(a.find_first_of(e), Piece::npos); |
| 348 | ASSERT_EQ(d.find_first_of(b), Piece::npos); |
| 349 | ASSERT_EQ(e.find_first_of(b), Piece::npos); |
| 350 | ASSERT_EQ(d.find_first_of(d), Piece::npos); |
| 351 | ASSERT_EQ(e.find_first_of(d), Piece::npos); |
| 352 | ASSERT_EQ(d.find_first_of(e), Piece::npos); |
| 353 | ASSERT_EQ(e.find_first_of(e), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 354 | |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 355 | ASSERT_EQ(a.find_first_not_of(b), 3U); |
| 356 | ASSERT_EQ(a.find_first_not_of(c), 0U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 357 | ASSERT_EQ(b.find_first_not_of(a), Piece::npos); |
| 358 | ASSERT_EQ(c.find_first_not_of(a), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 359 | ASSERT_EQ(f.find_first_not_of(a), 0U); |
| 360 | ASSERT_EQ(a.find_first_not_of(f), 0U); |
| 361 | ASSERT_EQ(a.find_first_not_of(d), 0U); |
| 362 | ASSERT_EQ(a.find_first_not_of(e), 0U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 363 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 364 | ASSERT_EQ(d.find_first_not_of(a), Piece::npos); |
| 365 | ASSERT_EQ(e.find_first_not_of(a), Piece::npos); |
| 366 | ASSERT_EQ(d.find_first_not_of(d), Piece::npos); |
| 367 | ASSERT_EQ(e.find_first_not_of(d), Piece::npos); |
| 368 | ASSERT_EQ(d.find_first_not_of(e), Piece::npos); |
| 369 | ASSERT_EQ(e.find_first_not_of(e), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 370 | |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 371 | TypeParam equals(TestFixture::as_string("====")); |
| 372 | Piece h(equals); |
| 373 | ASSERT_EQ(h.find_first_not_of('='), Piece::npos); |
| 374 | ASSERT_EQ(h.find_first_not_of('=', 3), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 375 | ASSERT_EQ(h.find_first_not_of('\0'), 0U); |
| 376 | ASSERT_EQ(g.find_first_not_of('x'), 2U); |
| 377 | ASSERT_EQ(f.find_first_not_of('\0'), 0U); |
| 378 | ASSERT_EQ(f.find_first_not_of('\0', 3), 4U); |
| 379 | ASSERT_EQ(f.find_first_not_of('\0', 2), 2U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 380 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 381 | ASSERT_EQ(d.find_first_not_of('x'), Piece::npos); |
| 382 | ASSERT_EQ(e.find_first_not_of('x'), Piece::npos); |
| 383 | ASSERT_EQ(d.find_first_not_of('\0'), Piece::npos); |
| 384 | ASSERT_EQ(e.find_first_not_of('\0'), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 385 | |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 386 | // Piece g("xx not found bb"); |
| 387 | TypeParam fifty_six(TestFixture::as_string("56")); |
| 388 | Piece i(fifty_six); |
| 389 | ASSERT_EQ(h.find_last_of(a), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | ASSERT_EQ(g.find_last_of(a), g.size()-1); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 391 | ASSERT_EQ(a.find_last_of(b), 2U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 392 | ASSERT_EQ(a.find_last_of(c), a.size()-1); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 393 | ASSERT_EQ(f.find_last_of(i), 6U); |
| 394 | ASSERT_EQ(a.find_last_of('a'), 0U); |
| 395 | ASSERT_EQ(a.find_last_of('b'), 1U); |
| 396 | ASSERT_EQ(a.find_last_of('z'), 25U); |
| 397 | ASSERT_EQ(a.find_last_of('a', 5), 0U); |
| 398 | ASSERT_EQ(a.find_last_of('b', 5), 1U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 399 | ASSERT_EQ(a.find_last_of('b', 0), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 400 | ASSERT_EQ(a.find_last_of('z', 25), 25U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 401 | ASSERT_EQ(a.find_last_of('z', 24), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 402 | ASSERT_EQ(f.find_last_of(i, 5), 5U); |
| 403 | ASSERT_EQ(f.find_last_of(i, 6), 6U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 404 | ASSERT_EQ(f.find_last_of(a, 4), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 405 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 406 | ASSERT_EQ(f.find_last_of(d), Piece::npos); |
| 407 | ASSERT_EQ(f.find_last_of(e), Piece::npos); |
| 408 | ASSERT_EQ(f.find_last_of(d, 4), Piece::npos); |
| 409 | ASSERT_EQ(f.find_last_of(e, 4), Piece::npos); |
| 410 | ASSERT_EQ(d.find_last_of(d), Piece::npos); |
| 411 | ASSERT_EQ(d.find_last_of(e), Piece::npos); |
| 412 | ASSERT_EQ(e.find_last_of(d), Piece::npos); |
| 413 | ASSERT_EQ(e.find_last_of(e), Piece::npos); |
| 414 | ASSERT_EQ(d.find_last_of(f), Piece::npos); |
| 415 | ASSERT_EQ(e.find_last_of(f), Piece::npos); |
| 416 | ASSERT_EQ(d.find_last_of(d, 4), Piece::npos); |
| 417 | ASSERT_EQ(d.find_last_of(e, 4), Piece::npos); |
| 418 | ASSERT_EQ(e.find_last_of(d, 4), Piece::npos); |
| 419 | ASSERT_EQ(e.find_last_of(e, 4), Piece::npos); |
| 420 | ASSERT_EQ(d.find_last_of(f, 4), Piece::npos); |
| 421 | ASSERT_EQ(e.find_last_of(f, 4), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 422 | |
| 423 | ASSERT_EQ(a.find_last_not_of(b), a.size()-1); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 424 | ASSERT_EQ(a.find_last_not_of(c), 22U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 425 | ASSERT_EQ(b.find_last_not_of(a), Piece::npos); |
| 426 | ASSERT_EQ(b.find_last_not_of(b), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 427 | ASSERT_EQ(f.find_last_not_of(i), 4U); |
| 428 | ASSERT_EQ(a.find_last_not_of(c, 24), 22U); |
| 429 | ASSERT_EQ(a.find_last_not_of(b, 3), 3U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 430 | ASSERT_EQ(a.find_last_not_of(b, 2), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 431 | // empty string nonsense |
| 432 | ASSERT_EQ(f.find_last_not_of(d), f.size()-1); |
| 433 | ASSERT_EQ(f.find_last_not_of(e), f.size()-1); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 434 | ASSERT_EQ(f.find_last_not_of(d, 4), 4U); |
| 435 | ASSERT_EQ(f.find_last_not_of(e, 4), 4U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 436 | ASSERT_EQ(d.find_last_not_of(d), Piece::npos); |
| 437 | ASSERT_EQ(d.find_last_not_of(e), Piece::npos); |
| 438 | ASSERT_EQ(e.find_last_not_of(d), Piece::npos); |
| 439 | ASSERT_EQ(e.find_last_not_of(e), Piece::npos); |
| 440 | ASSERT_EQ(d.find_last_not_of(f), Piece::npos); |
| 441 | ASSERT_EQ(e.find_last_not_of(f), Piece::npos); |
| 442 | ASSERT_EQ(d.find_last_not_of(d, 4), Piece::npos); |
| 443 | ASSERT_EQ(d.find_last_not_of(e, 4), Piece::npos); |
| 444 | ASSERT_EQ(e.find_last_not_of(d, 4), Piece::npos); |
| 445 | ASSERT_EQ(e.find_last_not_of(e, 4), Piece::npos); |
| 446 | ASSERT_EQ(d.find_last_not_of(f, 4), Piece::npos); |
| 447 | ASSERT_EQ(e.find_last_not_of(f, 4), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 448 | |
| 449 | ASSERT_EQ(h.find_last_not_of('x'), h.size() - 1); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 450 | ASSERT_EQ(h.find_last_not_of('='), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 451 | ASSERT_EQ(b.find_last_not_of('c'), 1U); |
| 452 | ASSERT_EQ(h.find_last_not_of('x', 2), 2U); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 453 | ASSERT_EQ(h.find_last_not_of('=', 2), Piece::npos); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 454 | ASSERT_EQ(b.find_last_not_of('b', 1), 0U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 455 | // empty string nonsense |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 456 | ASSERT_EQ(d.find_last_not_of('x'), Piece::npos); |
| 457 | ASSERT_EQ(e.find_last_not_of('x'), Piece::npos); |
| 458 | ASSERT_EQ(d.find_last_not_of('\0'), Piece::npos); |
| 459 | ASSERT_EQ(e.find_last_not_of('\0'), Piece::npos); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 460 | |
| 461 | ASSERT_EQ(a.substr(0, 3), b); |
| 462 | ASSERT_EQ(a.substr(23), c); |
| 463 | ASSERT_EQ(a.substr(23, 3), c); |
| 464 | ASSERT_EQ(a.substr(23, 99), c); |
| 465 | ASSERT_EQ(a.substr(0), a); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 466 | ASSERT_EQ(a.substr(3, 2), TestFixture::as_string("de")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 467 | // empty string nonsense |
| 468 | ASSERT_EQ(a.substr(99, 2), e); |
| 469 | ASSERT_EQ(d.substr(99), e); |
| 470 | ASSERT_EQ(d.substr(0, 99), e); |
| 471 | ASSERT_EQ(d.substr(99, 99), e); |
| 472 | } |
| 473 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 474 | TYPED_TEST(CommonStringPieceTest, CheckCustom) { |
| 475 | TypeParam foobar(TestFixture::as_string("foobar")); |
| 476 | BasicStringPiece<TypeParam> a(foobar); |
| 477 | TypeParam s1(TestFixture::as_string("123")); |
| 478 | s1 += static_cast<typename TypeParam::value_type>('\0'); |
| 479 | s1 += TestFixture::as_string("456"); |
| 480 | BasicStringPiece<TypeParam> b(s1); |
| 481 | BasicStringPiece<TypeParam> e; |
| 482 | TypeParam s2; |
| 483 | |
| 484 | // remove_prefix |
| 485 | BasicStringPiece<TypeParam> c(a); |
| 486 | c.remove_prefix(3); |
| 487 | ASSERT_EQ(c, TestFixture::as_string("bar")); |
| 488 | c = a; |
| 489 | c.remove_prefix(0); |
| 490 | ASSERT_EQ(c, a); |
| 491 | c.remove_prefix(c.size()); |
| 492 | ASSERT_EQ(c, e); |
| 493 | |
| 494 | // remove_suffix |
| 495 | c = a; |
| 496 | c.remove_suffix(3); |
| 497 | ASSERT_EQ(c, TestFixture::as_string("foo")); |
| 498 | c = a; |
| 499 | c.remove_suffix(0); |
| 500 | ASSERT_EQ(c, a); |
| 501 | c.remove_suffix(c.size()); |
| 502 | ASSERT_EQ(c, e); |
| 503 | |
| 504 | // set |
| 505 | c.set(foobar.c_str()); |
| 506 | ASSERT_EQ(c, a); |
| 507 | c.set(foobar.c_str(), 6); |
| 508 | ASSERT_EQ(c, a); |
| 509 | c.set(foobar.c_str(), 0); |
| 510 | ASSERT_EQ(c, e); |
| 511 | c.set(foobar.c_str(), 7); // Note, has an embedded NULL |
| 512 | ASSERT_NE(c, a); |
| 513 | |
| 514 | // as_string |
| 515 | TypeParam s3(a.as_string().c_str(), 7); // Note, has an embedded NULL |
| 516 | ASSERT_TRUE(c == s3); |
| 517 | TypeParam s4(e.as_string()); |
| 518 | ASSERT_TRUE(s4.empty()); |
| 519 | } |
| 520 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 521 | TEST(StringPieceTest, CheckCustom) { |
| 522 | StringPiece a("foobar"); |
| 523 | std::string s1("123"); |
| 524 | s1 += '\0'; |
| 525 | s1 += "456"; |
| 526 | StringPiece b(s1); |
| 527 | StringPiece e; |
| 528 | std::string s2; |
| 529 | |
| 530 | // CopyToString |
| 531 | a.CopyToString(&s2); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 532 | ASSERT_EQ(s2.size(), 6U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 533 | ASSERT_EQ(s2, "foobar"); |
| 534 | b.CopyToString(&s2); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 535 | ASSERT_EQ(s2.size(), 7U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 536 | ASSERT_EQ(s1, s2); |
| 537 | e.CopyToString(&s2); |
| 538 | ASSERT_TRUE(s2.empty()); |
| 539 | |
| 540 | // AppendToString |
| 541 | s2.erase(); |
| 542 | a.AppendToString(&s2); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 543 | ASSERT_EQ(s2.size(), 6U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 544 | ASSERT_EQ(s2, "foobar"); |
| 545 | a.AppendToString(&s2); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 546 | ASSERT_EQ(s2.size(), 12U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 547 | ASSERT_EQ(s2, "foobarfoobar"); |
| 548 | |
| 549 | // starts_with |
| 550 | ASSERT_TRUE(a.starts_with(a)); |
| 551 | ASSERT_TRUE(a.starts_with("foo")); |
| 552 | ASSERT_TRUE(a.starts_with(e)); |
| 553 | ASSERT_TRUE(b.starts_with(s1)); |
| 554 | ASSERT_TRUE(b.starts_with(b)); |
| 555 | ASSERT_TRUE(b.starts_with(e)); |
| 556 | ASSERT_TRUE(e.starts_with("")); |
| 557 | ASSERT_TRUE(!a.starts_with(b)); |
| 558 | ASSERT_TRUE(!b.starts_with(a)); |
| 559 | ASSERT_TRUE(!e.starts_with(a)); |
| 560 | |
| 561 | // ends with |
| 562 | ASSERT_TRUE(a.ends_with(a)); |
| 563 | ASSERT_TRUE(a.ends_with("bar")); |
| 564 | ASSERT_TRUE(a.ends_with(e)); |
| 565 | ASSERT_TRUE(b.ends_with(s1)); |
| 566 | ASSERT_TRUE(b.ends_with(b)); |
| 567 | ASSERT_TRUE(b.ends_with(e)); |
| 568 | ASSERT_TRUE(e.ends_with("")); |
| 569 | ASSERT_TRUE(!a.ends_with(b)); |
| 570 | ASSERT_TRUE(!b.ends_with(a)); |
| 571 | ASSERT_TRUE(!e.ends_with(a)); |
| 572 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 573 | StringPiece c; |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 574 | c.set("foobar", 6); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 575 | ASSERT_EQ(c, a); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 576 | c.set("foobar", 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 577 | ASSERT_EQ(c, e); |
[email protected] | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 578 | c.set("foobar", 7); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 579 | ASSERT_NE(c, a); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 580 | } |
| 581 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 582 | TYPED_TEST(CommonStringPieceTest, CheckNULL) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 583 | // we used to crash here, but now we don't. |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 584 | BasicStringPiece<TypeParam> s(NULL); |
| 585 | ASSERT_EQ(s.data(), (const typename TypeParam::value_type*)NULL); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 586 | ASSERT_EQ(s.size(), 0U); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 587 | |
| 588 | s.set(NULL); |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 589 | ASSERT_EQ(s.data(), (const typename TypeParam::value_type*)NULL); |
[email protected] | d6b0667 | 2008-08-19 00:31:24 | [diff] [blame] | 590 | ASSERT_EQ(s.size(), 0U); |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 591 | |
| 592 | TypeParam str = s.as_string(); |
| 593 | ASSERT_EQ(str.length(), 0U); |
| 594 | ASSERT_EQ(str, TypeParam()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 595 | } |
| 596 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 597 | TYPED_TEST(CommonStringPieceTest, CheckComparisons2) { |
| 598 | TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz")); |
| 599 | TypeParam alphabet_z(TestFixture::as_string("abcdefghijklmnopqrstuvwxyzz")); |
| 600 | TypeParam alphabet_y(TestFixture::as_string("abcdefghijklmnopqrstuvwxyy")); |
| 601 | BasicStringPiece<TypeParam> abc(alphabet); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 602 | |
[email protected] | a9f4b986 | 2011-12-13 20:05:58 | [diff] [blame] | 603 | // check comparison operations on strings longer than 4 bytes. |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 604 | ASSERT_TRUE(abc == BasicStringPiece<TypeParam>(alphabet)); |
| 605 | ASSERT_TRUE(abc.compare(BasicStringPiece<TypeParam>(alphabet)) == 0); |
[email protected] | a9f4b986 | 2011-12-13 20:05:58 | [diff] [blame] | 606 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 607 | ASSERT_TRUE(abc < BasicStringPiece<TypeParam>(alphabet_z)); |
| 608 | ASSERT_TRUE(abc.compare(BasicStringPiece<TypeParam>(alphabet_z)) < 0); |
[email protected] | a9f4b986 | 2011-12-13 20:05:58 | [diff] [blame] | 609 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 610 | ASSERT_TRUE(abc > BasicStringPiece<TypeParam>(alphabet_y)); |
| 611 | ASSERT_TRUE(abc.compare(BasicStringPiece<TypeParam>(alphabet_y)) > 0); |
| 612 | } |
| 613 | |
| 614 | // Test operations only supported by std::string version. |
| 615 | TEST(StringPieceTest, CheckComparisons2) { |
| 616 | StringPiece abc("abcdefghijklmnopqrstuvwxyz"); |
[email protected] | a9f4b986 | 2011-12-13 20:05:58 | [diff] [blame] | 617 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 618 | // starts_with |
| 619 | ASSERT_TRUE(abc.starts_with(abc)); |
| 620 | ASSERT_TRUE(abc.starts_with("abcdefghijklm")); |
| 621 | ASSERT_TRUE(!abc.starts_with("abcdefguvwxyz")); |
| 622 | |
| 623 | // ends_with |
| 624 | ASSERT_TRUE(abc.ends_with(abc)); |
| 625 | ASSERT_TRUE(!abc.ends_with("abcdefguvwxyz")); |
| 626 | ASSERT_TRUE(abc.ends_with("nopqrstuvwxyz")); |
| 627 | } |
| 628 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 629 | TYPED_TEST(CommonStringPieceTest, StringCompareNotAmbiguous) { |
| 630 | ASSERT_TRUE(TestFixture::as_string("hello").c_str() == |
| 631 | TestFixture::as_string("hello")); |
| 632 | ASSERT_TRUE(TestFixture::as_string("hello").c_str() < |
| 633 | TestFixture::as_string("world")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 634 | } |
| 635 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 636 | TYPED_TEST(CommonStringPieceTest, HeterogenousStringPieceEquals) { |
| 637 | TypeParam hello(TestFixture::as_string("hello")); |
| 638 | |
| 639 | ASSERT_TRUE(BasicStringPiece<TypeParam>(hello) == hello); |
| 640 | ASSERT_TRUE(hello.c_str() == BasicStringPiece<TypeParam>(hello)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 641 | } |
[email protected] | 4f74b78 | 2011-05-10 21:59:24 | [diff] [blame] | 642 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 643 | // string16-specific stuff |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 644 | TEST(StringPiece16Test, CheckSTL) { |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 645 | // Check some non-ascii characters. |
| 646 | string16 fifth(ASCIIToUTF16("123")); |
| 647 | fifth.push_back(0x0000); |
| 648 | fifth.push_back(0xd8c5); |
| 649 | fifth.push_back(0xdffe); |
| 650 | StringPiece16 f(fifth); |
| 651 | |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 652 | ASSERT_EQ(f[3], '\0'); |
| 653 | ASSERT_EQ(f[5], static_cast<char16>(0xdffe)); |
| 654 | |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 655 | ASSERT_EQ(f.size(), 6U); |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 656 | } |
| 657 | |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 658 | |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 659 | |
| 660 | TEST(StringPiece16Test, CheckConversion) { |
| 661 | // Make sure that we can convert from UTF8 to UTF16 and back. We use a two |
| 662 | // byte character (G clef) to test this. |
| 663 | ASSERT_EQ( |
| 664 | UTF16ToUTF8( |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 665 | StringPiece16(UTF8ToUTF16("\xf0\x9d\x84\x9e")).as_string()), |
[email protected] | 5cf71c5 | 2011-08-09 23:30:44 | [diff] [blame] | 666 | "\xf0\x9d\x84\x9e"); |
| 667 | } |
| 668 | |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 669 | TYPED_TEST(CommonStringPieceTest, CheckConstructors) { |
| 670 | TypeParam str(TestFixture::as_string("hello world")); |
| 671 | TypeParam empty; |
| 672 | |
| 673 | ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str)); |
| 674 | ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str.c_str())); |
| 675 | ASSERT_TRUE(TestFixture::as_string("hello") == |
| 676 | BasicStringPiece<TypeParam>(str.c_str(), 5)); |
[email protected] | fa1e7c2 | 2014-03-19 12:00:47 | [diff] [blame^] | 677 | ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(str.c_str(), |
| 678 | static_cast<typename BasicStringPiece<TypeParam>::size_type>(0))); |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 679 | ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(NULL)); |
[email protected] | fa1e7c2 | 2014-03-19 12:00:47 | [diff] [blame^] | 680 | ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(NULL, |
| 681 | static_cast<typename BasicStringPiece<TypeParam>::size_type>(0))); |
[email protected] | b1ecd2fc | 2011-12-22 21:54:49 | [diff] [blame] | 682 | ASSERT_TRUE(empty == BasicStringPiece<TypeParam>()); |
| 683 | ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str.begin(), str.end())); |
| 684 | ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(str.begin(), str.begin())); |
| 685 | ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(empty)); |
| 686 | ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(empty.begin(), empty.end())); |
| 687 | } |
| 688 | |
[email protected] | 4f74b78 | 2011-05-10 21:59:24 | [diff] [blame] | 689 | } // namespace base |