X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=unittests%2FADT%2FStringRefTest.cpp;h=6cf2e6a0454dcba5802721e5f5a1b72b70be6ebc;hp=b2f6fdcce0382cb2d038dbca2d00e5c6a4dd9a81;hb=f41971f6e7f9e5ba0c590c7e35e2c1af0cd38c69;hpb=528f0bbe19553dfadedca040df13a389daa7593d diff --git a/unittests/ADT/StringRefTest.cpp b/unittests/ADT/StringRefTest.cpp index b2f6fdcce03..6cf2e6a0454 100644 --- a/unittests/ADT/StringRefTest.cpp +++ b/unittests/ADT/StringRefTest.cpp @@ -7,11 +7,14 @@ // //===----------------------------------------------------------------------===// -#include "gtest/gtest.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Hashing.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" using namespace llvm; namespace llvm { @@ -60,6 +63,9 @@ TEST(StringRefTest, StringOps) { EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab")); EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA")); EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb")); + EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb")); + EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB")); + EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB")); EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA")); EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1")); @@ -219,23 +225,122 @@ TEST(StringRefTest, Split2) { expected.push_back("a"); expected.push_back("b"); expected.push_back("c"); StringRef("a,,b,c").split(parts, ",", 3, false); EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + expected.push_back("a"); expected.push_back("b"); expected.push_back("c"); + StringRef("a,,b,c").split(parts, ',', 3, false); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + expected.push_back(""); + StringRef().split(parts, ",", 0, true); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + expected.push_back(StringRef()); + StringRef("").split(parts, ",", 0, true); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + StringRef("").split(parts, ",", 0, false); + EXPECT_TRUE(parts == expected); + StringRef().split(parts, ",", 0, false); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + expected.push_back("a"); + expected.push_back(""); + expected.push_back("b"); + expected.push_back("c,d"); + StringRef("a,,b,c,d").split(parts, ",", 3, true); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + expected.push_back(""); + StringRef().split(parts, ',', 0, true); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + expected.push_back(StringRef()); + StringRef("").split(parts, ',', 0, true); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + StringRef("").split(parts, ',', 0, false); + EXPECT_TRUE(parts == expected); + StringRef().split(parts, ',', 0, false); + EXPECT_TRUE(parts == expected); + + expected.clear(); parts.clear(); + expected.push_back("a"); + expected.push_back(""); + expected.push_back("b"); + expected.push_back("c,d"); + StringRef("a,,b,c,d").split(parts, ',', 3, true); + EXPECT_TRUE(parts == expected); +} + +TEST(StringRefTest, Trim) { + StringRef Str0("hello"); + StringRef Str1(" hello "); + StringRef Str2(" hello "); + + EXPECT_EQ(StringRef("hello"), Str0.rtrim()); + EXPECT_EQ(StringRef(" hello"), Str1.rtrim()); + EXPECT_EQ(StringRef(" hello"), Str2.rtrim()); + EXPECT_EQ(StringRef("hello"), Str0.ltrim()); + EXPECT_EQ(StringRef("hello "), Str1.ltrim()); + EXPECT_EQ(StringRef("hello "), Str2.ltrim()); + EXPECT_EQ(StringRef("hello"), Str0.trim()); + EXPECT_EQ(StringRef("hello"), Str1.trim()); + EXPECT_EQ(StringRef("hello"), Str2.trim()); + + EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh")); + + EXPECT_EQ(StringRef(""), StringRef("").trim()); + EXPECT_EQ(StringRef(""), StringRef(" ").trim()); + EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim()); + EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim()); + EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1))); } TEST(StringRefTest, StartsWith) { StringRef Str("hello"); + EXPECT_TRUE(Str.startswith("")); EXPECT_TRUE(Str.startswith("he")); EXPECT_FALSE(Str.startswith("helloworld")); EXPECT_FALSE(Str.startswith("hi")); } +TEST(StringRefTest, StartsWithLower) { + StringRef Str("heLLo"); + EXPECT_TRUE(Str.startswith_lower("")); + EXPECT_TRUE(Str.startswith_lower("he")); + EXPECT_TRUE(Str.startswith_lower("hell")); + EXPECT_TRUE(Str.startswith_lower("HELlo")); + EXPECT_FALSE(Str.startswith_lower("helloworld")); + EXPECT_FALSE(Str.startswith_lower("hi")); +} + TEST(StringRefTest, EndsWith) { StringRef Str("hello"); + EXPECT_TRUE(Str.endswith("")); EXPECT_TRUE(Str.endswith("lo")); EXPECT_FALSE(Str.endswith("helloworld")); EXPECT_FALSE(Str.endswith("worldhello")); EXPECT_FALSE(Str.endswith("so")); } +TEST(StringRefTest, EndsWithLower) { + StringRef Str("heLLo"); + EXPECT_TRUE(Str.endswith_lower("")); + EXPECT_TRUE(Str.endswith_lower("lo")); + EXPECT_TRUE(Str.endswith_lower("LO")); + EXPECT_TRUE(Str.endswith_lower("ELlo")); + EXPECT_FALSE(Str.endswith_lower("helloworld")); + EXPECT_FALSE(Str.endswith_lower("hi")); +} + TEST(StringRefTest, Find) { StringRef Str("hello"); EXPECT_EQ(2U, Str.find('l')); @@ -267,6 +372,10 @@ TEST(StringRefTest, Find) { EXPECT_EQ(1U, Str.find_first_not_of('h')); EXPECT_EQ(4U, Str.find_first_not_of("hel")); EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello")); + + EXPECT_EQ(3U, Str.find_last_not_of('o')); + EXPECT_EQ(1U, Str.find_last_not_of("lo")); + EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo")); } TEST(StringRefTest, Count) { @@ -310,4 +419,185 @@ TEST(StringRefTest, Hashing) { hash_value(StringRef("hello world").slice(1, -1))); } +struct UnsignedPair { + const char *Str; + uint64_t Expected; +} Unsigned[] = + { {"0", 0} + , {"255", 255} + , {"256", 256} + , {"65535", 65535} + , {"65536", 65536} + , {"4294967295", 4294967295ULL} + , {"4294967296", 4294967296ULL} + , {"18446744073709551615", 18446744073709551615ULL} + , {"042", 34} + , {"0x42", 66} + , {"0b101010", 42} + }; + +struct SignedPair { + const char *Str; + int64_t Expected; +} Signed[] = + { {"0", 0} + , {"-0", 0} + , {"127", 127} + , {"128", 128} + , {"-128", -128} + , {"-129", -129} + , {"32767", 32767} + , {"32768", 32768} + , {"-32768", -32768} + , {"-32769", -32769} + , {"2147483647", 2147483647LL} + , {"2147483648", 2147483648LL} + , {"-2147483648", -2147483648LL} + , {"-2147483649", -2147483649LL} + , {"-9223372036854775808", -(9223372036854775807LL) - 1} + , {"042", 34} + , {"0x42", 66} + , {"0b101010", 42} + , {"-042", -34} + , {"-0x42", -66} + , {"-0b101010", -42} + }; + +TEST(StringRefTest, getAsInteger) { + uint8_t U8; + uint16_t U16; + uint32_t U32; + uint64_t U64; + + for (size_t i = 0; i < array_lengthof(Unsigned); ++i) { + bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8); + if (static_cast(Unsigned[i].Expected) == Unsigned[i].Expected) { + ASSERT_FALSE(U8Success); + EXPECT_EQ(U8, Unsigned[i].Expected); + } else { + ASSERT_TRUE(U8Success); + } + bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16); + if (static_cast(Unsigned[i].Expected) == Unsigned[i].Expected) { + ASSERT_FALSE(U16Success); + EXPECT_EQ(U16, Unsigned[i].Expected); + } else { + ASSERT_TRUE(U16Success); + } + bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32); + if (static_cast(Unsigned[i].Expected) == Unsigned[i].Expected) { + ASSERT_FALSE(U32Success); + EXPECT_EQ(U32, Unsigned[i].Expected); + } else { + ASSERT_TRUE(U32Success); + } + bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64); + if (static_cast(Unsigned[i].Expected) == Unsigned[i].Expected) { + ASSERT_FALSE(U64Success); + EXPECT_EQ(U64, Unsigned[i].Expected); + } else { + ASSERT_TRUE(U64Success); + } + } + + int8_t S8; + int16_t S16; + int32_t S32; + int64_t S64; + + for (size_t i = 0; i < array_lengthof(Signed); ++i) { + bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8); + if (static_cast(Signed[i].Expected) == Signed[i].Expected) { + ASSERT_FALSE(S8Success); + EXPECT_EQ(S8, Signed[i].Expected); + } else { + ASSERT_TRUE(S8Success); + } + bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16); + if (static_cast(Signed[i].Expected) == Signed[i].Expected) { + ASSERT_FALSE(S16Success); + EXPECT_EQ(S16, Signed[i].Expected); + } else { + ASSERT_TRUE(S16Success); + } + bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32); + if (static_cast(Signed[i].Expected) == Signed[i].Expected) { + ASSERT_FALSE(S32Success); + EXPECT_EQ(S32, Signed[i].Expected); + } else { + ASSERT_TRUE(S32Success); + } + bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64); + if (static_cast(Signed[i].Expected) == Signed[i].Expected) { + ASSERT_FALSE(S64Success); + EXPECT_EQ(S64, Signed[i].Expected); + } else { + ASSERT_TRUE(S64Success); + } + } +} + + +static const char* BadStrings[] = { + "18446744073709551617" // value just over max + , "123456789012345678901" // value way too large + , "4t23v" // illegal decimal characters + , "0x123W56" // illegal hex characters + , "0b2" // illegal bin characters + , "08" // illegal oct characters + , "0o8" // illegal oct characters + , "-123" // negative unsigned value +}; + + +TEST(StringRefTest, getAsUnsignedIntegerBadStrings) { + unsigned long long U64; + for (size_t i = 0; i < array_lengthof(BadStrings); ++i) { + bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64); + ASSERT_TRUE(IsBadNumber); + } +} + +static const char *join_input[] = { "a", "b", "c" }; +static const char join_result1[] = "a"; +static const char join_result2[] = "a:b:c"; +static const char join_result3[] = "a::b::c"; + +TEST(StringRefTest, joinStrings) { + std::vector v1; + std::vector v2; + for (size_t i = 0; i < array_lengthof(join_input); ++i) { + v1.push_back(join_input[i]); + v2.push_back(join_input[i]); + } + + bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1; + EXPECT_TRUE(v1_join1); + bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2; + EXPECT_TRUE(v1_join2); + bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3; + EXPECT_TRUE(v1_join3); + + bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1; + EXPECT_TRUE(v2_join1); + bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2; + EXPECT_TRUE(v2_join2); + bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3; + EXPECT_TRUE(v2_join3); +} + + +TEST(StringRefTest, AllocatorCopy) { + BumpPtrAllocator Alloc; + StringRef Str1 = "hello"; + StringRef Str2 = "bye"; + StringRef Str1c = Str1.copy(Alloc); + StringRef Str2c = Str2.copy(Alloc); + EXPECT_TRUE(Str1.equals(Str1c)); + EXPECT_NE(Str1.data(), Str1c.data()); + EXPECT_TRUE(Str2.equals(Str2c)); + EXPECT_NE(Str2.data(), Str2c.data()); +} + + } // end anonymous namespace