[ADT] Fix a confusing interface spec and some annoying peculiarities
[oota-llvm.git] / unittests / ADT / StringRefTest.cpp
index b240a87ef7544a483dc7d2340f76be45c63e9cda..6cf2e6a0454dcba5802721e5f5a1b72b70be6ebc 100644 (file)
@@ -9,8 +9,10 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Hashing.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/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;
 #include "llvm/Support/raw_ostream.h"
 #include "gtest/gtest.h"
 using namespace llvm;
@@ -223,6 +225,59 @@ 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.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) {
 }
 
 TEST(StringRefTest, Trim) {
@@ -531,22 +586,18 @@ TEST(StringRefTest, joinStrings) {
   EXPECT_TRUE(v2_join3);
 }
 
   EXPECT_TRUE(v2_join3);
 }
 
-static void fn_stringref(StringRef str) {
-  EXPECT_TRUE(str == "hello");
-}
-static void fn_conststringref(ConstStringRef str) {
-  fn_stringref(str);
-}
 
 
-TEST(StringRefTest, constStringRef) {
-  LLVM_CONSTEXPR ConstStringRef csr("hello");
-#if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
-  LLVM_STATIC_ASSERT(csr[0] != csr[1], "");
-  LLVM_STATIC_ASSERT(csr[2] == csr[3], "");
-  LLVM_STATIC_ASSERT(csr.size() == 5, "");
-#endif
-  llvm_expect(csr[2] == csr[3]);
-  fn_conststringref(csr);
+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
 } // end anonymous namespace