Home | History | Annotate | Download | only in ADT
      1 //===- llvm/unittest/ADT/SmallStringTest.cpp ------------------------------===//
      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 // SmallString unit tests.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/ADT/SmallString.h"
     15 #include "gtest/gtest.h"
     16 #include <climits>
     17 #include <cstring>
     18 #include <stdarg.h>
     19 
     20 using namespace llvm;
     21 
     22 namespace {
     23 
     24 // Test fixture class
     25 class SmallStringTest : public testing::Test {
     26 protected:
     27   typedef SmallString<40> StringType;
     28 
     29   StringType theString;
     30 
     31   void assertEmpty(StringType & v) {
     32     // Size tests
     33     EXPECT_EQ(0u, v.size());
     34     EXPECT_TRUE(v.empty());
     35     // Iterator tests
     36     EXPECT_TRUE(v.begin() == v.end());
     37   }
     38 };
     39 
     40 // New string test.
     41 TEST_F(SmallStringTest, EmptyStringTest) {
     42   SCOPED_TRACE("EmptyStringTest");
     43   assertEmpty(theString);
     44   EXPECT_TRUE(theString.rbegin() == theString.rend());
     45 }
     46 
     47 TEST_F(SmallStringTest, AssignRepeated) {
     48   theString.assign(3, 'a');
     49   EXPECT_EQ(3u, theString.size());
     50   EXPECT_STREQ("aaa", theString.c_str());
     51 }
     52 
     53 TEST_F(SmallStringTest, AssignIterPair) {
     54   StringRef abc = "abc";
     55   theString.assign(abc.begin(), abc.end());
     56   EXPECT_EQ(3u, theString.size());
     57   EXPECT_STREQ("abc", theString.c_str());
     58 }
     59 
     60 TEST_F(SmallStringTest, AssignStringRef) {
     61   StringRef abc = "abc";
     62   theString.assign(abc);
     63   EXPECT_EQ(3u, theString.size());
     64   EXPECT_STREQ("abc", theString.c_str());
     65 }
     66 
     67 TEST_F(SmallStringTest, AssignSmallVector) {
     68   StringRef abc = "abc";
     69   SmallVector<char, 10> abcVec(abc.begin(), abc.end());
     70   theString.assign(abcVec);
     71   EXPECT_EQ(3u, theString.size());
     72   EXPECT_STREQ("abc", theString.c_str());
     73 }
     74 
     75 TEST_F(SmallStringTest, AppendIterPair) {
     76   StringRef abc = "abc";
     77   theString.append(abc.begin(), abc.end());
     78   theString.append(abc.begin(), abc.end());
     79   EXPECT_EQ(6u, theString.size());
     80   EXPECT_STREQ("abcabc", theString.c_str());
     81 }
     82 
     83 TEST_F(SmallStringTest, AppendStringRef) {
     84   StringRef abc = "abc";
     85   theString.append(abc);
     86   theString.append(abc);
     87   EXPECT_EQ(6u, theString.size());
     88   EXPECT_STREQ("abcabc", theString.c_str());
     89 }
     90 
     91 TEST_F(SmallStringTest, AppendSmallVector) {
     92   StringRef abc = "abc";
     93   SmallVector<char, 10> abcVec(abc.begin(), abc.end());
     94   theString.append(abcVec);
     95   theString.append(abcVec);
     96   EXPECT_EQ(6u, theString.size());
     97   EXPECT_STREQ("abcabc", theString.c_str());
     98 }
     99 
    100 TEST_F(SmallStringTest, Substr) {
    101   theString = "hello";
    102   EXPECT_EQ("lo", theString.substr(3));
    103   EXPECT_EQ("", theString.substr(100));
    104   EXPECT_EQ("hello", theString.substr(0, 100));
    105   EXPECT_EQ("o", theString.substr(4, 10));
    106 }
    107 
    108 TEST_F(SmallStringTest, Slice) {
    109   theString = "hello";
    110   EXPECT_EQ("l", theString.slice(2, 3));
    111   EXPECT_EQ("ell", theString.slice(1, 4));
    112   EXPECT_EQ("llo", theString.slice(2, 100));
    113   EXPECT_EQ("", theString.slice(2, 1));
    114   EXPECT_EQ("", theString.slice(10, 20));
    115 }
    116 
    117 TEST_F(SmallStringTest, Find) {
    118   theString = "hello";
    119   EXPECT_EQ(2U, theString.find('l'));
    120   EXPECT_EQ(StringRef::npos, theString.find('z'));
    121   EXPECT_EQ(StringRef::npos, theString.find("helloworld"));
    122   EXPECT_EQ(0U, theString.find("hello"));
    123   EXPECT_EQ(1U, theString.find("ello"));
    124   EXPECT_EQ(StringRef::npos, theString.find("zz"));
    125   EXPECT_EQ(2U, theString.find("ll", 2));
    126   EXPECT_EQ(StringRef::npos, theString.find("ll", 3));
    127   EXPECT_EQ(0U, theString.find(""));
    128 
    129   EXPECT_EQ(3U, theString.rfind('l'));
    130   EXPECT_EQ(StringRef::npos, theString.rfind('z'));
    131   EXPECT_EQ(StringRef::npos, theString.rfind("helloworld"));
    132   EXPECT_EQ(0U, theString.rfind("hello"));
    133   EXPECT_EQ(1U, theString.rfind("ello"));
    134   EXPECT_EQ(StringRef::npos, theString.rfind("zz"));
    135 
    136   EXPECT_EQ(2U, theString.find_first_of('l'));
    137   EXPECT_EQ(1U, theString.find_first_of("el"));
    138   EXPECT_EQ(StringRef::npos, theString.find_first_of("xyz"));
    139 
    140   EXPECT_EQ(1U, theString.find_first_not_of('h'));
    141   EXPECT_EQ(4U, theString.find_first_not_of("hel"));
    142   EXPECT_EQ(StringRef::npos, theString.find_first_not_of("hello"));
    143 
    144   theString = "hellx xello hell ello world foo bar hello";
    145   EXPECT_EQ(36U, theString.find("hello"));
    146   EXPECT_EQ(28U, theString.find("foo"));
    147   EXPECT_EQ(12U, theString.find("hell", 2));
    148   EXPECT_EQ(0U, theString.find(""));
    149 }
    150 
    151 TEST_F(SmallStringTest, Count) {
    152   theString = "hello";
    153   EXPECT_EQ(2U, theString.count('l'));
    154   EXPECT_EQ(1U, theString.count('o'));
    155   EXPECT_EQ(0U, theString.count('z'));
    156   EXPECT_EQ(0U, theString.count("helloworld"));
    157   EXPECT_EQ(1U, theString.count("hello"));
    158   EXPECT_EQ(1U, theString.count("ello"));
    159   EXPECT_EQ(0U, theString.count("zz"));
    160 }
    161 
    162 TEST_F(SmallStringTest, Realloc) {
    163   theString = "abcd";
    164   theString.reserve(100);
    165   EXPECT_EQ("abcd", theString);
    166   unsigned const N = 100000;
    167   theString.reserve(N);
    168   for (unsigned i = 0; i < N - 4; ++i)
    169     theString.push_back('y');
    170   EXPECT_EQ("abcdyyy", theString.slice(0, 7));
    171 }
    172 
    173 TEST(StringRefTest, Comparisons) {
    174   EXPECT_EQ(-1, SmallString<10>("aab").compare("aad"));
    175   EXPECT_EQ( 0, SmallString<10>("aab").compare("aab"));
    176   EXPECT_EQ( 1, SmallString<10>("aab").compare("aaa"));
    177   EXPECT_EQ(-1, SmallString<10>("aab").compare("aabb"));
    178   EXPECT_EQ( 1, SmallString<10>("aab").compare("aa"));
    179   EXPECT_EQ( 1, SmallString<10>("\xFF").compare("\1"));
    180 
    181   EXPECT_EQ(-1, SmallString<10>("AaB").compare_lower("aAd"));
    182   EXPECT_EQ( 0, SmallString<10>("AaB").compare_lower("aab"));
    183   EXPECT_EQ( 1, SmallString<10>("AaB").compare_lower("AAA"));
    184   EXPECT_EQ(-1, SmallString<10>("AaB").compare_lower("aaBb"));
    185   EXPECT_EQ( 1, SmallString<10>("AaB").compare_lower("aA"));
    186   EXPECT_EQ( 1, SmallString<10>("\xFF").compare_lower("\1"));
    187 
    188   EXPECT_EQ(-1, SmallString<10>("aab").compare_numeric("aad"));
    189   EXPECT_EQ( 0, SmallString<10>("aab").compare_numeric("aab"));
    190   EXPECT_EQ( 1, SmallString<10>("aab").compare_numeric("aaa"));
    191   EXPECT_EQ(-1, SmallString<10>("aab").compare_numeric("aabb"));
    192   EXPECT_EQ( 1, SmallString<10>("aab").compare_numeric("aa"));
    193   EXPECT_EQ(-1, SmallString<10>("1").compare_numeric("10"));
    194   EXPECT_EQ( 0, SmallString<10>("10").compare_numeric("10"));
    195   EXPECT_EQ( 0, SmallString<10>("10a").compare_numeric("10a"));
    196   EXPECT_EQ( 1, SmallString<10>("2").compare_numeric("1"));
    197   EXPECT_EQ( 0, SmallString<10>("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
    198   EXPECT_EQ( 1, SmallString<10>("\xFF").compare_numeric("\1"));
    199   EXPECT_EQ( 1, SmallString<10>("V16").compare_numeric("V1_q0"));
    200   EXPECT_EQ(-1, SmallString<10>("V1_q0").compare_numeric("V16"));
    201   EXPECT_EQ(-1, SmallString<10>("V8_q0").compare_numeric("V16"));
    202   EXPECT_EQ( 1, SmallString<10>("V16").compare_numeric("V8_q0"));
    203   EXPECT_EQ(-1, SmallString<10>("V1_q0").compare_numeric("V8_q0"));
    204   EXPECT_EQ( 1, SmallString<10>("V8_q0").compare_numeric("V1_q0"));
    205 }
    206 
    207 }
    208