1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/raw_ostream.h"
17 TEST(StringRefTest
, Construction
) {
18 EXPECT_TRUE(StringRef() == "");
19 EXPECT_TRUE(StringRef("hello") == "hello");
20 EXPECT_TRUE(StringRef("hello world", 5) == "hello");
21 EXPECT_TRUE(StringRef(std::string("hello")) == "hello");
24 TEST(StringRefTest
, Iteration
) {
26 const char *p
= "hello";
27 for (const char *it
= S
.begin(), *ie
= S
.end(); it
!= ie
; ++it
, ++p
)
28 EXPECT_TRUE(*p
== *it
);
31 TEST(StringRefTest
, StringOps
) {
32 const char *p
= "hello";
33 EXPECT_EQ(p
, StringRef(p
, 0).data());
34 EXPECT_TRUE(StringRef().empty());
35 EXPECT_EQ((size_t) 5, StringRef("hello").size());
36 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
37 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
38 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
39 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
40 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
43 TEST(StringRefTest
, Operators
) {
44 EXPECT_TRUE(StringRef() == "");
45 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
46 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
47 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
48 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
49 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
50 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
51 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
52 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
53 EXPECT_TRUE(StringRef("aab") == StringRef("aab"));
54 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
55 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
56 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
57 EXPECT_EQ('a', StringRef("aab")[1]);
60 TEST(StringRefTest
, Utilities
) {
61 StringRef
Str("hello");
62 EXPECT_TRUE(Str
.substr(3) == "lo");
63 EXPECT_TRUE(Str
.substr(100) == "");
64 EXPECT_TRUE(Str
.substr(0, 100) == "hello");
65 EXPECT_TRUE(Str
.substr(4, 10) == "o");
67 EXPECT_TRUE(Str
.slice(2, 3) == "l");
68 EXPECT_TRUE(Str
.slice(1, 4) == "ell");
69 EXPECT_TRUE(Str
.slice(2, 100) == "llo");
70 EXPECT_TRUE(Str
.slice(2, 1) == "");
71 EXPECT_TRUE(Str
.slice(10, 20) == "");
73 EXPECT_TRUE(Str
.split('X') == std::make_pair(StringRef("hello"),
75 EXPECT_TRUE(Str
.split('e') == std::make_pair(StringRef("h"),
77 EXPECT_TRUE(Str
.split('h') == std::make_pair(StringRef(""),
79 EXPECT_TRUE(Str
.split('o') == std::make_pair(StringRef("hell"),
82 EXPECT_TRUE(Str
.startswith("he"));
83 EXPECT_FALSE(Str
.startswith("helloworld"));
84 EXPECT_FALSE(Str
.startswith("hi"));
87 raw_string_ostream
OS(Storage
);
88 OS
<< StringRef("hello");
89 EXPECT_EQ("hello", OS
.str());
91 EXPECT_TRUE(Str
.find('l') == 2);
92 EXPECT_TRUE(Str
.find('z') == StringRef::npos
);
93 EXPECT_TRUE(Str
.find("helloworld") == StringRef::npos
);
94 EXPECT_TRUE(Str
.find("hello") == 0);
95 EXPECT_TRUE(Str
.find("ello") == 1);
96 EXPECT_TRUE(Str
.find("zz") == StringRef::npos
);
98 EXPECT_TRUE(Str
.count('l') == 2);
99 EXPECT_TRUE(Str
.count('o') == 1);
100 EXPECT_TRUE(Str
.count('z') == 0);
101 EXPECT_TRUE(Str
.count("helloworld") == 0);
102 EXPECT_TRUE(Str
.count("hello") == 1);
103 EXPECT_TRUE(Str
.count("ello") == 1);
104 EXPECT_TRUE(Str
.count("zz") == 0);
107 } // end anonymous namespace