Home | History | Annotate | Download | only in ADT
      1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "gtest/gtest.h"
     11 #include "llvm/ADT/StringRef.h"
     12 #include "llvm/ADT/Hashing.h"
     13 #include "llvm/ADT/SmallVector.h"
     14 #include "llvm/Support/raw_ostream.h"
     15 using namespace llvm;
     16 
     17 namespace llvm {
     18 
     19 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
     20   OS << S.str();
     21   return OS;
     22 }
     23 
     24 std::ostream &operator<<(std::ostream &OS,
     25                          const std::pair<StringRef, StringRef> &P) {
     26   OS << "(" << P.first << ", " << P.second << ")";
     27   return OS;
     28 }
     29 
     30 }
     31 
     32 namespace {
     33 TEST(StringRefTest, Construction) {
     34   EXPECT_EQ("", StringRef());
     35   EXPECT_EQ("hello", StringRef("hello"));
     36   EXPECT_EQ("hello", StringRef("hello world", 5));
     37   EXPECT_EQ("hello", StringRef(std::string("hello")));
     38 }
     39 
     40 TEST(StringRefTest, Iteration) {
     41   StringRef S("hello");
     42   const char *p = "hello";
     43   for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
     44     EXPECT_EQ(*it, *p);
     45 }
     46 
     47 TEST(StringRefTest, StringOps) {
     48   const char *p = "hello";
     49   EXPECT_EQ(p, StringRef(p, 0).data());
     50   EXPECT_TRUE(StringRef().empty());
     51   EXPECT_EQ((size_t) 5, StringRef("hello").size());
     52   EXPECT_EQ(-1, StringRef("aab").compare("aad"));
     53   EXPECT_EQ( 0, StringRef("aab").compare("aab"));
     54   EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
     55   EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
     56   EXPECT_EQ( 1, StringRef("aab").compare("aa"));
     57   EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
     58 
     59   EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
     60   EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
     61   EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
     62   EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
     63   EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
     64   EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
     65 
     66   EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
     67   EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
     68   EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
     69   EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
     70   EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
     71   EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
     72   EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
     73   EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
     74   EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
     75   EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
     76   EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
     77   EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
     78   EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
     79   EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
     80   EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
     81   EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
     82   EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
     83 }
     84 
     85 TEST(StringRefTest, Operators) {
     86   EXPECT_EQ("", StringRef());
     87   EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
     88   EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
     89   EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
     90   EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
     91   EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
     92   EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
     93   EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
     94   EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
     95   EXPECT_EQ(StringRef("aab"), StringRef("aab"));
     96   EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
     97   EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
     98   EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
     99   EXPECT_EQ('a', StringRef("aab")[1]);
    100 }
    101 
    102 TEST(StringRefTest, Substr) {
    103   StringRef Str("hello");
    104   EXPECT_EQ("lo", Str.substr(3));
    105   EXPECT_EQ("", Str.substr(100));
    106   EXPECT_EQ("hello", Str.substr(0, 100));
    107   EXPECT_EQ("o", Str.substr(4, 10));
    108 }
    109 
    110 TEST(StringRefTest, Slice) {
    111   StringRef Str("hello");
    112   EXPECT_EQ("l", Str.slice(2, 3));
    113   EXPECT_EQ("ell", Str.slice(1, 4));
    114   EXPECT_EQ("llo", Str.slice(2, 100));
    115   EXPECT_EQ("", Str.slice(2, 1));
    116   EXPECT_EQ("", Str.slice(10, 20));
    117 }
    118 
    119 TEST(StringRefTest, Split) {
    120   StringRef Str("hello");
    121   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
    122             Str.split('X'));
    123   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
    124             Str.split('e'));
    125   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
    126             Str.split('h'));
    127   EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
    128             Str.split('l'));
    129   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
    130             Str.split('o'));
    131 
    132   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
    133             Str.rsplit('X'));
    134   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
    135             Str.rsplit('e'));
    136   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
    137             Str.rsplit('h'));
    138   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
    139             Str.rsplit('l'));
    140   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
    141             Str.rsplit('o'));
    142 }
    143 
    144 TEST(StringRefTest, Split2) {
    145   SmallVector<StringRef, 5> parts;
    146   SmallVector<StringRef, 5> expected;
    147 
    148   expected.push_back("ab"); expected.push_back("c");
    149   StringRef(",ab,,c,").split(parts, ",", -1, false);
    150   EXPECT_TRUE(parts == expected);
    151 
    152   expected.clear(); parts.clear();
    153   expected.push_back(""); expected.push_back("ab"); expected.push_back("");
    154   expected.push_back("c"); expected.push_back("");
    155   StringRef(",ab,,c,").split(parts, ",", -1, true);
    156   EXPECT_TRUE(parts == expected);
    157 
    158   expected.clear(); parts.clear();
    159   expected.push_back("");
    160   StringRef("").split(parts, ",", -1, true);
    161   EXPECT_TRUE(parts == expected);
    162 
    163   expected.clear(); parts.clear();
    164   StringRef("").split(parts, ",", -1, false);
    165   EXPECT_TRUE(parts == expected);
    166 
    167   expected.clear(); parts.clear();
    168   StringRef(",").split(parts, ",", -1, false);
    169   EXPECT_TRUE(parts == expected);
    170 
    171   expected.clear(); parts.clear();
    172   expected.push_back(""); expected.push_back("");
    173   StringRef(",").split(parts, ",", -1, true);
    174   EXPECT_TRUE(parts == expected);
    175 
    176   expected.clear(); parts.clear();
    177   expected.push_back("a"); expected.push_back("b");
    178   StringRef("a,b").split(parts, ",", -1, true);
    179   EXPECT_TRUE(parts == expected);
    180 
    181   // Test MaxSplit
    182   expected.clear(); parts.clear();
    183   expected.push_back("a,,b,c");
    184   StringRef("a,,b,c").split(parts, ",", 0, true);
    185   EXPECT_TRUE(parts == expected);
    186 
    187   expected.clear(); parts.clear();
    188   expected.push_back("a,,b,c");
    189   StringRef("a,,b,c").split(parts, ",", 0, false);
    190   EXPECT_TRUE(parts == expected);
    191 
    192   expected.clear(); parts.clear();
    193   expected.push_back("a"); expected.push_back(",b,c");
    194   StringRef("a,,b,c").split(parts, ",", 1, true);
    195   EXPECT_TRUE(parts == expected);
    196 
    197   expected.clear(); parts.clear();
    198   expected.push_back("a"); expected.push_back(",b,c");
    199   StringRef("a,,b,c").split(parts, ",", 1, false);
    200   EXPECT_TRUE(parts == expected);
    201 
    202   expected.clear(); parts.clear();
    203   expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
    204   StringRef("a,,b,c").split(parts, ",", 2, true);
    205   EXPECT_TRUE(parts == expected);
    206 
    207   expected.clear(); parts.clear();
    208   expected.push_back("a"); expected.push_back("b,c");
    209   StringRef("a,,b,c").split(parts, ",", 2, false);
    210   EXPECT_TRUE(parts == expected);
    211 
    212   expected.clear(); parts.clear();
    213   expected.push_back("a"); expected.push_back(""); expected.push_back("b");
    214   expected.push_back("c");
    215   StringRef("a,,b,c").split(parts, ",", 3, true);
    216   EXPECT_TRUE(parts == expected);
    217 
    218   expected.clear(); parts.clear();
    219   expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
    220   StringRef("a,,b,c").split(parts, ",", 3, false);
    221   EXPECT_TRUE(parts == expected);
    222 }
    223 
    224 TEST(StringRefTest, StartsWith) {
    225   StringRef Str("hello");
    226   EXPECT_TRUE(Str.startswith("he"));
    227   EXPECT_FALSE(Str.startswith("helloworld"));
    228   EXPECT_FALSE(Str.startswith("hi"));
    229 }
    230 
    231 TEST(StringRefTest, EndsWith) {
    232   StringRef Str("hello");
    233   EXPECT_TRUE(Str.endswith("lo"));
    234   EXPECT_FALSE(Str.endswith("helloworld"));
    235   EXPECT_FALSE(Str.endswith("worldhello"));
    236   EXPECT_FALSE(Str.endswith("so"));
    237 }
    238 
    239 TEST(StringRefTest, Find) {
    240   StringRef Str("hello");
    241   EXPECT_EQ(2U, Str.find('l'));
    242   EXPECT_EQ(StringRef::npos, Str.find('z'));
    243   EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
    244   EXPECT_EQ(0U, Str.find("hello"));
    245   EXPECT_EQ(1U, Str.find("ello"));
    246   EXPECT_EQ(StringRef::npos, Str.find("zz"));
    247   EXPECT_EQ(2U, Str.find("ll", 2));
    248   EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
    249   EXPECT_EQ(0U, Str.find(""));
    250   StringRef LongStr("hellx xello hell ello world foo bar hello");
    251   EXPECT_EQ(36U, LongStr.find("hello"));
    252   EXPECT_EQ(28U, LongStr.find("foo"));
    253   EXPECT_EQ(12U, LongStr.find("hell", 2));
    254   EXPECT_EQ(0U, LongStr.find(""));
    255 
    256   EXPECT_EQ(3U, Str.rfind('l'));
    257   EXPECT_EQ(StringRef::npos, Str.rfind('z'));
    258   EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
    259   EXPECT_EQ(0U, Str.rfind("hello"));
    260   EXPECT_EQ(1U, Str.rfind("ello"));
    261   EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
    262 
    263   EXPECT_EQ(2U, Str.find_first_of('l'));
    264   EXPECT_EQ(1U, Str.find_first_of("el"));
    265   EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
    266 
    267   EXPECT_EQ(1U, Str.find_first_not_of('h'));
    268   EXPECT_EQ(4U, Str.find_first_not_of("hel"));
    269   EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
    270 }
    271 
    272 TEST(StringRefTest, Count) {
    273   StringRef Str("hello");
    274   EXPECT_EQ(2U, Str.count('l'));
    275   EXPECT_EQ(1U, Str.count('o'));
    276   EXPECT_EQ(0U, Str.count('z'));
    277   EXPECT_EQ(0U, Str.count("helloworld"));
    278   EXPECT_EQ(1U, Str.count("hello"));
    279   EXPECT_EQ(1U, Str.count("ello"));
    280   EXPECT_EQ(0U, Str.count("zz"));
    281 }
    282 
    283 TEST(StringRefTest, EditDistance) {
    284   StringRef Str("hello");
    285   EXPECT_EQ(2U, Str.edit_distance("hill"));
    286 }
    287 
    288 TEST(StringRefTest, Misc) {
    289   std::string Storage;
    290   raw_string_ostream OS(Storage);
    291   OS << StringRef("hello");
    292   EXPECT_EQ("hello", OS.str());
    293 }
    294 
    295 TEST(StringRefTest, Hashing) {
    296   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
    297   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
    298   std::string S = "hello world";
    299   hash_code H = hash_value(S);
    300   EXPECT_EQ(H, hash_value(StringRef("hello world")));
    301   EXPECT_EQ(H, hash_value(StringRef(S)));
    302   EXPECT_NE(H, hash_value(StringRef("hello worl")));
    303   EXPECT_EQ(hash_value(std::string("hello worl")),
    304             hash_value(StringRef("hello worl")));
    305   EXPECT_NE(H, hash_value(StringRef("hello world ")));
    306   EXPECT_EQ(hash_value(std::string("hello world ")),
    307             hash_value(StringRef("hello world ")));
    308   EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
    309   EXPECT_NE(hash_value(std::string("ello worl")),
    310             hash_value(StringRef("hello world").slice(1, -1)));
    311 }
    312 
    313 struct UnsignedPair {
    314   const char *Str;
    315   uint64_t Expected;
    316 } Unsigned[] =
    317   { {"0", 0}
    318   , {"255", 255}
    319   , {"256", 256}
    320   , {"65535", 65535}
    321   , {"65536", 65536}
    322   , {"4294967295", 4294967295ULL}
    323   , {"4294967296", 4294967296ULL}
    324   , {"18446744073709551615", 18446744073709551615ULL}
    325   , {"042", 34}
    326   , {"0x42", 66}
    327   , {"0b101010", 42}
    328   };
    329 
    330 struct SignedPair {
    331   const char *Str;
    332   int64_t Expected;
    333 } Signed[] =
    334   { {"0", 0}
    335   , {"-0", 0}
    336   , {"127", 127}
    337   , {"128", 128}
    338   , {"-128", -128}
    339   , {"-129", -129}
    340   , {"32767", 32767}
    341   , {"32768", 32768}
    342   , {"-32768", -32768}
    343   , {"-32769", -32769}
    344   , {"2147483647", 2147483647LL}
    345   , {"2147483648", 2147483648LL}
    346   , {"-2147483648", -2147483648LL}
    347   , {"-2147483649", -2147483649LL}
    348   , {"-9223372036854775808", -(9223372036854775807LL) - 1}
    349   , {"042", 34}
    350   , {"0x42", 66}
    351   , {"0b101010", 42}
    352   , {"-042", -34}
    353   , {"-0x42", -66}
    354   , {"-0b101010", -42}
    355   };
    356 
    357 TEST(StringRefTest, getAsInteger) {
    358   uint8_t U8;
    359   uint16_t U16;
    360   uint32_t U32;
    361   uint64_t U64;
    362 
    363   for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
    364     bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
    365     if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
    366       ASSERT_FALSE(U8Success);
    367       EXPECT_EQ(U8, Unsigned[i].Expected);
    368     } else {
    369       ASSERT_TRUE(U8Success);
    370     }
    371     bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
    372     if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
    373       ASSERT_FALSE(U16Success);
    374       EXPECT_EQ(U16, Unsigned[i].Expected);
    375     } else {
    376       ASSERT_TRUE(U16Success);
    377     }
    378     bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
    379     if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
    380       ASSERT_FALSE(U32Success);
    381       EXPECT_EQ(U32, Unsigned[i].Expected);
    382     } else {
    383       ASSERT_TRUE(U32Success);
    384     }
    385     bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
    386     if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
    387       ASSERT_FALSE(U64Success);
    388       EXPECT_EQ(U64, Unsigned[i].Expected);
    389     } else {
    390       ASSERT_TRUE(U64Success);
    391     }
    392   }
    393 
    394   int8_t S8;
    395   int16_t S16;
    396   int32_t S32;
    397   int64_t S64;
    398 
    399   for (size_t i = 0; i < array_lengthof(Signed); ++i) {
    400     bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
    401     if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
    402       ASSERT_FALSE(S8Success);
    403       EXPECT_EQ(S8, Signed[i].Expected);
    404     } else {
    405       ASSERT_TRUE(S8Success);
    406     }
    407     bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
    408     if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
    409       ASSERT_FALSE(S16Success);
    410       EXPECT_EQ(S16, Signed[i].Expected);
    411     } else {
    412       ASSERT_TRUE(S16Success);
    413     }
    414     bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
    415     if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
    416       ASSERT_FALSE(S32Success);
    417       EXPECT_EQ(S32, Signed[i].Expected);
    418     } else {
    419       ASSERT_TRUE(S32Success);
    420     }
    421     bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
    422     if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
    423       ASSERT_FALSE(S64Success);
    424       EXPECT_EQ(S64, Signed[i].Expected);
    425     } else {
    426       ASSERT_TRUE(S64Success);
    427     }
    428   }
    429 }
    430 
    431 } // end anonymous namespace
    432