Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <string>
      6 
      7 #include "base/string_piece.h"
      8 
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 using namespace base;
     12 
     13 TEST(StringPieceTest, CheckComparisonOperators) {
     14 #define CMP_Y(op, x, y)                                               \
     15   ASSERT_TRUE( (StringPiece((x)) op StringPiece((y))));               \
     16   ASSERT_TRUE( (StringPiece((x)).compare(StringPiece((y))) op 0))
     17 
     18 #define CMP_N(op, x, y)                                          \
     19   ASSERT_FALSE(StringPiece((x)) op StringPiece((y)));               \
     20   ASSERT_FALSE(StringPiece((x)).compare(StringPiece((y))) op 0)
     21 
     22   CMP_Y(==, "",   "");
     23   CMP_Y(==, "a",  "a");
     24   CMP_Y(==, "aa", "aa");
     25   CMP_N(==, "a",  "");
     26   CMP_N(==, "",   "a");
     27   CMP_N(==, "a",  "b");
     28   CMP_N(==, "a",  "aa");
     29   CMP_N(==, "aa", "a");
     30 
     31   CMP_N(!=, "",   "");
     32   CMP_N(!=, "a",  "a");
     33   CMP_N(!=, "aa", "aa");
     34   CMP_Y(!=, "a",  "");
     35   CMP_Y(!=, "",   "a");
     36   CMP_Y(!=, "a",  "b");
     37   CMP_Y(!=, "a",  "aa");
     38   CMP_Y(!=, "aa", "a");
     39 
     40   CMP_Y(<, "a",  "b");
     41   CMP_Y(<, "a",  "aa");
     42   CMP_Y(<, "aa", "b");
     43   CMP_Y(<, "aa", "bb");
     44   CMP_N(<, "a",  "a");
     45   CMP_N(<, "b",  "a");
     46   CMP_N(<, "aa", "a");
     47   CMP_N(<, "b",  "aa");
     48   CMP_N(<, "bb", "aa");
     49 
     50   CMP_Y(<=, "a",  "a");
     51   CMP_Y(<=, "a",  "b");
     52   CMP_Y(<=, "a",  "aa");
     53   CMP_Y(<=, "aa", "b");
     54   CMP_Y(<=, "aa", "bb");
     55   CMP_N(<=, "b",  "a");
     56   CMP_N(<=, "aa", "a");
     57   CMP_N(<=, "b",  "aa");
     58   CMP_N(<=, "bb", "aa");
     59 
     60   CMP_N(>=, "a",  "b");
     61   CMP_N(>=, "a",  "aa");
     62   CMP_N(>=, "aa", "b");
     63   CMP_N(>=, "aa", "bb");
     64   CMP_Y(>=, "a",  "a");
     65   CMP_Y(>=, "b",  "a");
     66   CMP_Y(>=, "aa", "a");
     67   CMP_Y(>=, "b",  "aa");
     68   CMP_Y(>=, "bb", "aa");
     69 
     70   CMP_N(>, "a",  "a");
     71   CMP_N(>, "a",  "b");
     72   CMP_N(>, "a",  "aa");
     73   CMP_N(>, "aa", "b");
     74   CMP_N(>, "aa", "bb");
     75   CMP_Y(>, "b",  "a");
     76   CMP_Y(>, "aa", "a");
     77   CMP_Y(>, "b",  "aa");
     78   CMP_Y(>, "bb", "aa");
     79 
     80   std::string x;
     81   for (int i = 0; i < 256; i++) {
     82     x += 'a';
     83     std::string y = x;
     84     CMP_Y(==, x, y);
     85     for (int j = 0; j < i; j++) {
     86       std::string z = x;
     87       z[j] = 'b';       // Differs in position 'j'
     88       CMP_N(==, x, z);
     89     }
     90   }
     91 
     92 #undef CMP_Y
     93 #undef CMP_N
     94 }
     95 
     96 TEST(StringPieceTest, CheckSTL) {
     97   StringPiece a("abcdefghijklmnopqrstuvwxyz");
     98   StringPiece b("abc");
     99   StringPiece c("xyz");
    100   StringPiece d("foobar");
    101   StringPiece e;
    102   std::string temp("123");
    103   temp += '\0';
    104   temp += "456";
    105   StringPiece f(temp);
    106 
    107   ASSERT_EQ(a[6], 'g');
    108   ASSERT_EQ(b[0], 'a');
    109   ASSERT_EQ(c[2], 'z');
    110   ASSERT_EQ(f[3], '\0');
    111   ASSERT_EQ(f[5], '5');
    112 
    113   ASSERT_EQ(*d.data(), 'f');
    114   ASSERT_EQ(d.data()[5], 'r');
    115   ASSERT_TRUE(e.data() == NULL);
    116 
    117   ASSERT_EQ(*a.begin(), 'a');
    118   ASSERT_EQ(*(b.begin() + 2), 'c');
    119   ASSERT_EQ(*(c.end() - 1), 'z');
    120 
    121   ASSERT_EQ(*a.rbegin(), 'z');
    122   ASSERT_EQ(*(b.rbegin() + 2), 'a');
    123   ASSERT_EQ(*(c.rend() - 1), 'x');
    124   ASSERT_TRUE(a.rbegin() + 26 == a.rend());
    125 
    126   ASSERT_EQ(a.size(), 26U);
    127   ASSERT_EQ(b.size(), 3U);
    128   ASSERT_EQ(c.size(), 3U);
    129   ASSERT_EQ(d.size(), 6U);
    130   ASSERT_EQ(e.size(), 0U);
    131   ASSERT_EQ(f.size(), 7U);
    132 
    133   ASSERT_TRUE(!d.empty());
    134   ASSERT_TRUE(d.begin() != d.end());
    135   ASSERT_TRUE(d.begin() + 6 == d.end());
    136 
    137   ASSERT_TRUE(e.empty());
    138   ASSERT_TRUE(e.begin() == e.end());
    139 
    140   d.clear();
    141   ASSERT_EQ(d.size(), 0U);
    142   ASSERT_TRUE(d.empty());
    143   ASSERT_TRUE(d.data() == NULL);
    144   ASSERT_TRUE(d.begin() == d.end());
    145 
    146   ASSERT_GE(a.max_size(), a.capacity());
    147   ASSERT_GE(a.capacity(), a.size());
    148 
    149   char buf[4] = { '%', '%', '%', '%' };
    150   ASSERT_EQ(a.copy(buf, 4), 4U);
    151   ASSERT_EQ(buf[0], a[0]);
    152   ASSERT_EQ(buf[1], a[1]);
    153   ASSERT_EQ(buf[2], a[2]);
    154   ASSERT_EQ(buf[3], a[3]);
    155   ASSERT_EQ(a.copy(buf, 3, 7), 3U);
    156   ASSERT_EQ(buf[0], a[7]);
    157   ASSERT_EQ(buf[1], a[8]);
    158   ASSERT_EQ(buf[2], a[9]);
    159   ASSERT_EQ(buf[3], a[3]);
    160   ASSERT_EQ(c.copy(buf, 99), 3U);
    161   ASSERT_EQ(buf[0], c[0]);
    162   ASSERT_EQ(buf[1], c[1]);
    163   ASSERT_EQ(buf[2], c[2]);
    164   ASSERT_EQ(buf[3], a[3]);
    165 
    166   ASSERT_EQ(StringPiece::npos, std::string::npos);
    167 
    168   ASSERT_EQ(a.find(b), 0U);
    169   ASSERT_EQ(a.find(b, 1), StringPiece::npos);
    170   ASSERT_EQ(a.find(c), 23U);
    171   ASSERT_EQ(a.find(c, 9), 23U);
    172   ASSERT_EQ(a.find(c, StringPiece::npos), StringPiece::npos);
    173   ASSERT_EQ(b.find(c), StringPiece::npos);
    174   ASSERT_EQ(b.find(c, StringPiece::npos), StringPiece::npos);
    175   ASSERT_EQ(a.find(d), 0U);
    176   ASSERT_EQ(a.find(e), 0U);
    177   ASSERT_EQ(a.find(d, 12), 12U);
    178   ASSERT_EQ(a.find(e, 17), 17U);
    179   StringPiece g("xx not found bb");
    180   ASSERT_EQ(a.find(g), StringPiece::npos);
    181   // empty string nonsense
    182   ASSERT_EQ(d.find(b), StringPiece::npos);
    183   ASSERT_EQ(e.find(b), StringPiece::npos);
    184   ASSERT_EQ(d.find(b, 4), StringPiece::npos);
    185   ASSERT_EQ(e.find(b, 7), StringPiece::npos);
    186 
    187   size_t empty_search_pos = std::string().find(std::string());
    188   ASSERT_EQ(d.find(d), empty_search_pos);
    189   ASSERT_EQ(d.find(e), empty_search_pos);
    190   ASSERT_EQ(e.find(d), empty_search_pos);
    191   ASSERT_EQ(e.find(e), empty_search_pos);
    192   ASSERT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
    193   ASSERT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
    194   ASSERT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
    195   ASSERT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
    196 
    197   ASSERT_EQ(a.find('a'), 0U);
    198   ASSERT_EQ(a.find('c'), 2U);
    199   ASSERT_EQ(a.find('z'), 25U);
    200   ASSERT_EQ(a.find('$'), StringPiece::npos);
    201   ASSERT_EQ(a.find('\0'), StringPiece::npos);
    202   ASSERT_EQ(f.find('\0'), 3U);
    203   ASSERT_EQ(f.find('3'), 2U);
    204   ASSERT_EQ(f.find('5'), 5U);
    205   ASSERT_EQ(g.find('o'), 4U);
    206   ASSERT_EQ(g.find('o', 4), 4U);
    207   ASSERT_EQ(g.find('o', 5), 8U);
    208   ASSERT_EQ(a.find('b', 5), StringPiece::npos);
    209   // empty string nonsense
    210   ASSERT_EQ(d.find('\0'), StringPiece::npos);
    211   ASSERT_EQ(e.find('\0'), StringPiece::npos);
    212   ASSERT_EQ(d.find('\0', 4), StringPiece::npos);
    213   ASSERT_EQ(e.find('\0', 7), StringPiece::npos);
    214   ASSERT_EQ(d.find('x'), StringPiece::npos);
    215   ASSERT_EQ(e.find('x'), StringPiece::npos);
    216   ASSERT_EQ(d.find('x', 4), StringPiece::npos);
    217   ASSERT_EQ(e.find('x', 7), StringPiece::npos);
    218 
    219   ASSERT_EQ(a.rfind(b), 0U);
    220   ASSERT_EQ(a.rfind(b, 1), 0U);
    221   ASSERT_EQ(a.rfind(c), 23U);
    222   ASSERT_EQ(a.rfind(c, 22U), StringPiece::npos);
    223   ASSERT_EQ(a.rfind(c, 1U), StringPiece::npos);
    224   ASSERT_EQ(a.rfind(c, 0U), StringPiece::npos);
    225   ASSERT_EQ(b.rfind(c), StringPiece::npos);
    226   ASSERT_EQ(b.rfind(c, 0U), StringPiece::npos);
    227   ASSERT_EQ(a.rfind(d), (size_t) a.as_string().rfind(std::string()));
    228   ASSERT_EQ(a.rfind(e), a.as_string().rfind(std::string()));
    229   ASSERT_EQ(a.rfind(d, 12), 12U);
    230   ASSERT_EQ(a.rfind(e, 17), 17U);
    231   ASSERT_EQ(a.rfind(g), StringPiece::npos);
    232   ASSERT_EQ(d.rfind(b), StringPiece::npos);
    233   ASSERT_EQ(e.rfind(b), StringPiece::npos);
    234   ASSERT_EQ(d.rfind(b, 4), StringPiece::npos);
    235   ASSERT_EQ(e.rfind(b, 7), StringPiece::npos);
    236   // empty string nonsense
    237   ASSERT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
    238   ASSERT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
    239   ASSERT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
    240   ASSERT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
    241   ASSERT_EQ(d.rfind(d), std::string().rfind(std::string()));
    242   ASSERT_EQ(e.rfind(d), std::string().rfind(std::string()));
    243   ASSERT_EQ(d.rfind(e), std::string().rfind(std::string()));
    244   ASSERT_EQ(e.rfind(e), std::string().rfind(std::string()));
    245 
    246   ASSERT_EQ(g.rfind('o'), 8U);
    247   ASSERT_EQ(g.rfind('q'), StringPiece::npos);
    248   ASSERT_EQ(g.rfind('o', 8), 8U);
    249   ASSERT_EQ(g.rfind('o', 7), 4U);
    250   ASSERT_EQ(g.rfind('o', 3), StringPiece::npos);
    251   ASSERT_EQ(f.rfind('\0'), 3U);
    252   ASSERT_EQ(f.rfind('\0', 12), 3U);
    253   ASSERT_EQ(f.rfind('3'), 2U);
    254   ASSERT_EQ(f.rfind('5'), 5U);
    255   // empty string nonsense
    256   ASSERT_EQ(d.rfind('o'), StringPiece::npos);
    257   ASSERT_EQ(e.rfind('o'), StringPiece::npos);
    258   ASSERT_EQ(d.rfind('o', 4), StringPiece::npos);
    259   ASSERT_EQ(e.rfind('o', 7), StringPiece::npos);
    260 
    261   ASSERT_EQ(a.find_first_of(b), 0U);
    262   ASSERT_EQ(a.find_first_of(b, 0), 0U);
    263   ASSERT_EQ(a.find_first_of(b, 1), 1U);
    264   ASSERT_EQ(a.find_first_of(b, 2), 2U);
    265   ASSERT_EQ(a.find_first_of(b, 3), StringPiece::npos);
    266   ASSERT_EQ(a.find_first_of(c), 23U);
    267   ASSERT_EQ(a.find_first_of(c, 23), 23U);
    268   ASSERT_EQ(a.find_first_of(c, 24), 24U);
    269   ASSERT_EQ(a.find_first_of(c, 25), 25U);
    270   ASSERT_EQ(a.find_first_of(c, 26), StringPiece::npos);
    271   ASSERT_EQ(g.find_first_of(b), 13U);
    272   ASSERT_EQ(g.find_first_of(c), 0U);
    273   ASSERT_EQ(a.find_first_of(f), StringPiece::npos);
    274   ASSERT_EQ(f.find_first_of(a), StringPiece::npos);
    275   // empty string nonsense
    276   ASSERT_EQ(a.find_first_of(d), StringPiece::npos);
    277   ASSERT_EQ(a.find_first_of(e), StringPiece::npos);
    278   ASSERT_EQ(d.find_first_of(b), StringPiece::npos);
    279   ASSERT_EQ(e.find_first_of(b), StringPiece::npos);
    280   ASSERT_EQ(d.find_first_of(d), StringPiece::npos);
    281   ASSERT_EQ(e.find_first_of(d), StringPiece::npos);
    282   ASSERT_EQ(d.find_first_of(e), StringPiece::npos);
    283   ASSERT_EQ(e.find_first_of(e), StringPiece::npos);
    284 
    285   ASSERT_EQ(a.find_first_not_of(b), 3U);
    286   ASSERT_EQ(a.find_first_not_of(c), 0U);
    287   ASSERT_EQ(b.find_first_not_of(a), StringPiece::npos);
    288   ASSERT_EQ(c.find_first_not_of(a), StringPiece::npos);
    289   ASSERT_EQ(f.find_first_not_of(a), 0U);
    290   ASSERT_EQ(a.find_first_not_of(f), 0U);
    291   ASSERT_EQ(a.find_first_not_of(d), 0U);
    292   ASSERT_EQ(a.find_first_not_of(e), 0U);
    293   // empty string nonsense
    294   ASSERT_EQ(d.find_first_not_of(a), StringPiece::npos);
    295   ASSERT_EQ(e.find_first_not_of(a), StringPiece::npos);
    296   ASSERT_EQ(d.find_first_not_of(d), StringPiece::npos);
    297   ASSERT_EQ(e.find_first_not_of(d), StringPiece::npos);
    298   ASSERT_EQ(d.find_first_not_of(e), StringPiece::npos);
    299   ASSERT_EQ(e.find_first_not_of(e), StringPiece::npos);
    300 
    301   StringPiece h("====");
    302   ASSERT_EQ(h.find_first_not_of('='), StringPiece::npos);
    303   ASSERT_EQ(h.find_first_not_of('=', 3), StringPiece::npos);
    304   ASSERT_EQ(h.find_first_not_of('\0'), 0U);
    305   ASSERT_EQ(g.find_first_not_of('x'), 2U);
    306   ASSERT_EQ(f.find_first_not_of('\0'), 0U);
    307   ASSERT_EQ(f.find_first_not_of('\0', 3), 4U);
    308   ASSERT_EQ(f.find_first_not_of('\0', 2), 2U);
    309   // empty string nonsense
    310   ASSERT_EQ(d.find_first_not_of('x'), StringPiece::npos);
    311   ASSERT_EQ(e.find_first_not_of('x'), StringPiece::npos);
    312   ASSERT_EQ(d.find_first_not_of('\0'), StringPiece::npos);
    313   ASSERT_EQ(e.find_first_not_of('\0'), StringPiece::npos);
    314 
    315   //  StringPiece g("xx not found bb");
    316   StringPiece i("56");
    317   ASSERT_EQ(h.find_last_of(a), StringPiece::npos);
    318   ASSERT_EQ(g.find_last_of(a), g.size()-1);
    319   ASSERT_EQ(a.find_last_of(b), 2U);
    320   ASSERT_EQ(a.find_last_of(c), a.size()-1);
    321   ASSERT_EQ(f.find_last_of(i), 6U);
    322   ASSERT_EQ(a.find_last_of('a'), 0U);
    323   ASSERT_EQ(a.find_last_of('b'), 1U);
    324   ASSERT_EQ(a.find_last_of('z'), 25U);
    325   ASSERT_EQ(a.find_last_of('a', 5), 0U);
    326   ASSERT_EQ(a.find_last_of('b', 5), 1U);
    327   ASSERT_EQ(a.find_last_of('b', 0), StringPiece::npos);
    328   ASSERT_EQ(a.find_last_of('z', 25), 25U);
    329   ASSERT_EQ(a.find_last_of('z', 24), StringPiece::npos);
    330   ASSERT_EQ(f.find_last_of(i, 5), 5U);
    331   ASSERT_EQ(f.find_last_of(i, 6), 6U);
    332   ASSERT_EQ(f.find_last_of(a, 4), StringPiece::npos);
    333   // empty string nonsense
    334   ASSERT_EQ(f.find_last_of(d), StringPiece::npos);
    335   ASSERT_EQ(f.find_last_of(e), StringPiece::npos);
    336   ASSERT_EQ(f.find_last_of(d, 4), StringPiece::npos);
    337   ASSERT_EQ(f.find_last_of(e, 4), StringPiece::npos);
    338   ASSERT_EQ(d.find_last_of(d), StringPiece::npos);
    339   ASSERT_EQ(d.find_last_of(e), StringPiece::npos);
    340   ASSERT_EQ(e.find_last_of(d), StringPiece::npos);
    341   ASSERT_EQ(e.find_last_of(e), StringPiece::npos);
    342   ASSERT_EQ(d.find_last_of(f), StringPiece::npos);
    343   ASSERT_EQ(e.find_last_of(f), StringPiece::npos);
    344   ASSERT_EQ(d.find_last_of(d, 4), StringPiece::npos);
    345   ASSERT_EQ(d.find_last_of(e, 4), StringPiece::npos);
    346   ASSERT_EQ(e.find_last_of(d, 4), StringPiece::npos);
    347   ASSERT_EQ(e.find_last_of(e, 4), StringPiece::npos);
    348   ASSERT_EQ(d.find_last_of(f, 4), StringPiece::npos);
    349   ASSERT_EQ(e.find_last_of(f, 4), StringPiece::npos);
    350 
    351   ASSERT_EQ(a.find_last_not_of(b), a.size()-1);
    352   ASSERT_EQ(a.find_last_not_of(c), 22U);
    353   ASSERT_EQ(b.find_last_not_of(a), StringPiece::npos);
    354   ASSERT_EQ(b.find_last_not_of(b), StringPiece::npos);
    355   ASSERT_EQ(f.find_last_not_of(i), 4U);
    356   ASSERT_EQ(a.find_last_not_of(c, 24), 22U);
    357   ASSERT_EQ(a.find_last_not_of(b, 3), 3U);
    358   ASSERT_EQ(a.find_last_not_of(b, 2), StringPiece::npos);
    359   // empty string nonsense
    360   ASSERT_EQ(f.find_last_not_of(d), f.size()-1);
    361   ASSERT_EQ(f.find_last_not_of(e), f.size()-1);
    362   ASSERT_EQ(f.find_last_not_of(d, 4), 4U);
    363   ASSERT_EQ(f.find_last_not_of(e, 4), 4U);
    364   ASSERT_EQ(d.find_last_not_of(d), StringPiece::npos);
    365   ASSERT_EQ(d.find_last_not_of(e), StringPiece::npos);
    366   ASSERT_EQ(e.find_last_not_of(d), StringPiece::npos);
    367   ASSERT_EQ(e.find_last_not_of(e), StringPiece::npos);
    368   ASSERT_EQ(d.find_last_not_of(f), StringPiece::npos);
    369   ASSERT_EQ(e.find_last_not_of(f), StringPiece::npos);
    370   ASSERT_EQ(d.find_last_not_of(d, 4), StringPiece::npos);
    371   ASSERT_EQ(d.find_last_not_of(e, 4), StringPiece::npos);
    372   ASSERT_EQ(e.find_last_not_of(d, 4), StringPiece::npos);
    373   ASSERT_EQ(e.find_last_not_of(e, 4), StringPiece::npos);
    374   ASSERT_EQ(d.find_last_not_of(f, 4), StringPiece::npos);
    375   ASSERT_EQ(e.find_last_not_of(f, 4), StringPiece::npos);
    376 
    377   ASSERT_EQ(h.find_last_not_of('x'), h.size() - 1);
    378   ASSERT_EQ(h.find_last_not_of('='), StringPiece::npos);
    379   ASSERT_EQ(b.find_last_not_of('c'), 1U);
    380   ASSERT_EQ(h.find_last_not_of('x', 2), 2U);
    381   ASSERT_EQ(h.find_last_not_of('=', 2), StringPiece::npos);
    382   ASSERT_EQ(b.find_last_not_of('b', 1), 0U);
    383   // empty string nonsense
    384   ASSERT_EQ(d.find_last_not_of('x'), StringPiece::npos);
    385   ASSERT_EQ(e.find_last_not_of('x'), StringPiece::npos);
    386   ASSERT_EQ(d.find_last_not_of('\0'), StringPiece::npos);
    387   ASSERT_EQ(e.find_last_not_of('\0'), StringPiece::npos);
    388 
    389   ASSERT_EQ(a.substr(0, 3), b);
    390   ASSERT_EQ(a.substr(23), c);
    391   ASSERT_EQ(a.substr(23, 3), c);
    392   ASSERT_EQ(a.substr(23, 99), c);
    393   ASSERT_EQ(a.substr(0), a);
    394   ASSERT_EQ(a.substr(3, 2), "de");
    395   // empty string nonsense
    396   ASSERT_EQ(a.substr(99, 2), e);
    397   ASSERT_EQ(d.substr(99), e);
    398   ASSERT_EQ(d.substr(0, 99), e);
    399   ASSERT_EQ(d.substr(99, 99), e);
    400 }
    401 
    402 TEST(StringPieceTest, CheckCustom) {
    403   StringPiece a("foobar");
    404   std::string s1("123");
    405   s1 += '\0';
    406   s1 += "456";
    407   StringPiece b(s1);
    408   StringPiece e;
    409   std::string s2;
    410 
    411   // CopyToString
    412   a.CopyToString(&s2);
    413   ASSERT_EQ(s2.size(), 6U);
    414   ASSERT_EQ(s2, "foobar");
    415   b.CopyToString(&s2);
    416   ASSERT_EQ(s2.size(), 7U);
    417   ASSERT_EQ(s1, s2);
    418   e.CopyToString(&s2);
    419   ASSERT_TRUE(s2.empty());
    420 
    421   // AppendToString
    422   s2.erase();
    423   a.AppendToString(&s2);
    424   ASSERT_EQ(s2.size(), 6U);
    425   ASSERT_EQ(s2, "foobar");
    426   a.AppendToString(&s2);
    427   ASSERT_EQ(s2.size(), 12U);
    428   ASSERT_EQ(s2, "foobarfoobar");
    429 
    430   // starts_with
    431   ASSERT_TRUE(a.starts_with(a));
    432   ASSERT_TRUE(a.starts_with("foo"));
    433   ASSERT_TRUE(a.starts_with(e));
    434   ASSERT_TRUE(b.starts_with(s1));
    435   ASSERT_TRUE(b.starts_with(b));
    436   ASSERT_TRUE(b.starts_with(e));
    437   ASSERT_TRUE(e.starts_with(""));
    438   ASSERT_TRUE(!a.starts_with(b));
    439   ASSERT_TRUE(!b.starts_with(a));
    440   ASSERT_TRUE(!e.starts_with(a));
    441 
    442   // ends with
    443   ASSERT_TRUE(a.ends_with(a));
    444   ASSERT_TRUE(a.ends_with("bar"));
    445   ASSERT_TRUE(a.ends_with(e));
    446   ASSERT_TRUE(b.ends_with(s1));
    447   ASSERT_TRUE(b.ends_with(b));
    448   ASSERT_TRUE(b.ends_with(e));
    449   ASSERT_TRUE(e.ends_with(""));
    450   ASSERT_TRUE(!a.ends_with(b));
    451   ASSERT_TRUE(!b.ends_with(a));
    452   ASSERT_TRUE(!e.ends_with(a));
    453 
    454   // remove_prefix
    455   StringPiece c(a);
    456   c.remove_prefix(3);
    457   ASSERT_EQ(c, "bar");
    458   c = a;
    459   c.remove_prefix(0);
    460   ASSERT_EQ(c, a);
    461   c.remove_prefix(c.size());
    462   ASSERT_EQ(c, e);
    463 
    464   // remove_suffix
    465   c = a;
    466   c.remove_suffix(3);
    467   ASSERT_EQ(c, "foo");
    468   c = a;
    469   c.remove_suffix(0);
    470   ASSERT_EQ(c, a);
    471   c.remove_suffix(c.size());
    472   ASSERT_EQ(c, e);
    473 
    474   // set
    475   c.set("foobar", 6);
    476   ASSERT_EQ(c, a);
    477   c.set("foobar", 0);
    478   ASSERT_EQ(c, e);
    479   c.set("foobar", 7);
    480   ASSERT_NE(c, a);
    481 
    482   c.set("foobar");
    483   ASSERT_EQ(c, a);
    484 
    485   c.set(static_cast<const void*>("foobar"), 6);
    486   ASSERT_EQ(c, a);
    487   c.set(static_cast<const void*>("foobar"), 0);
    488   ASSERT_EQ(c, e);
    489   c.set(static_cast<const void*>("foobar"), 7);
    490   ASSERT_NE(c, a);
    491 
    492   // as_string
    493   std::string s3(a.as_string().c_str(), 7);
    494   ASSERT_EQ(c, s3);
    495   std::string s4(e.as_string());
    496   ASSERT_TRUE(s4.empty());
    497 }
    498 
    499 TEST(StringPieceTest, CheckNULL) {
    500   // we used to crash here, but now we don't.
    501   StringPiece s(NULL);
    502   ASSERT_EQ(s.data(), (const char*)NULL);
    503   ASSERT_EQ(s.size(), 0U);
    504 
    505   s.set(NULL);
    506   ASSERT_EQ(s.data(), (const char*)NULL);
    507   ASSERT_EQ(s.size(), 0U);
    508 }
    509 
    510 TEST(StringPieceTest, CheckComparisons2) {
    511   StringPiece abc("abcdefghijklmnopqrstuvwxyz");
    512 
    513   // check comparison operations on strings longer than 4 bytes.
    514   ASSERT_TRUE(abc == StringPiece("abcdefghijklmnopqrstuvwxyz"));
    515   ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyz")) == 0);
    516 
    517   ASSERT_TRUE(abc < StringPiece("abcdefghijklmnopqrstuvwxzz"));
    518   ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxzz")) < 0);
    519 
    520   ASSERT_TRUE(abc > StringPiece("abcdefghijklmnopqrstuvwxyy"));
    521   ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyy")) > 0);
    522 
    523   // starts_with
    524   ASSERT_TRUE(abc.starts_with(abc));
    525   ASSERT_TRUE(abc.starts_with("abcdefghijklm"));
    526   ASSERT_TRUE(!abc.starts_with("abcdefguvwxyz"));
    527 
    528   // ends_with
    529   ASSERT_TRUE(abc.ends_with(abc));
    530   ASSERT_TRUE(!abc.ends_with("abcdefguvwxyz"));
    531   ASSERT_TRUE(abc.ends_with("nopqrstuvwxyz"));
    532 }
    533 
    534 TEST(StringPieceTest, StringCompareNotAmbiguous) {
    535   ASSERT_TRUE("hello" == std::string("hello"));
    536   ASSERT_TRUE("hello" < std::string("world"));
    537 }
    538 
    539 TEST(StringPieceTest, HeterogenousStringPieceEquals) {
    540   ASSERT_TRUE(StringPiece("hello") == std::string("hello"));
    541   ASSERT_TRUE("hello" == StringPiece("hello"));
    542 }
    543