Repository navigation
Expand file tree
/
Copy pathstringref-test.cc
More file actions
122 lines (102 loc) · 2.94 KB
/
Copy pathstringref-test.cc
File metadata and controls
122 lines (102 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <cstring>
#include <string>
#include "gmock/gmock.h"
#include "format.h"
using fmt::StringRef;
TEST(StringRefTest, CtorFromCString) {
const char * const cstr = "abc";
StringRef sref(cstr);
EXPECT_STREQ(cstr, sref.data());
EXPECT_EQ(std::strlen(cstr), sref.size());
}
TEST(StringRefTest, CtorFromStdString) {
const std::string str = "defg";
StringRef sref(str);
EXPECT_STREQ(str.data(), sref.data());
EXPECT_EQ(str.size(), sref.size());
EXPECT_EQ(str.length(), sref.size());
}
TEST(StringRefTest, CtorFromStrbufAndLength) {
const char * const cstr = "123456789";
StringRef sref(&cstr[3], 3);
EXPECT_EQ(3u, sref.size());
EXPECT_EQ(3u, sref.length());
EXPECT_EQ(0, std::memcmp(sref.data(), "456", 3));
}
TEST(StringRefTest, ConvertToString) {
std::string s = StringRef("abc");
EXPECT_EQ("abc", s);
}
TEST(StringRefTest, BeginEnd) {
const char * const cstr = "123456789";
StringRef sref(&cstr[3], 3);
std::string s1;
#if __cplusplus >= 201103L
for (char c : sref) {
s1 += c;
}
#else
for (StringRef::iterator it = sref.begin(); it != sref.end(); ++it) {
s1 += *it;
}
#endif
EXPECT_EQ("456", s1);
std::string s2(sref.cbegin(), sref.cend());
EXPECT_EQ("456", s2);
}
TEST(StringRefTest, RbeginRend) {
const char * const cstr = "987654321";
StringRef sref(&cstr[3], 3);
std::string s(sref.rbegin(), sref.rend());
EXPECT_EQ("456", s);
EXPECT_EQ('4', *sref.crbegin());
EXPECT_EQ('6', *(sref.crend() - 1));
}
TEST(StringRefTest, Subscript) {
const char * const cstr = "987654321";
StringRef sref(&cstr[3], 3);
std::string s;
for (size_t i = 0; i < sref.size(); ++i) {
s += sref[i];
}
EXPECT_EQ("654", s);
}
TEST(StringRefTest, IndexWithAt) {
const char * const cstr = "123456789";
StringRef sref(&cstr[3], 3);
EXPECT_EQ('4', sref.at(0));
EXPECT_EQ('5', sref.at(1));
EXPECT_EQ('6', sref.at(2));
try {
sref.at(3);
FAIL();
} catch (std::out_of_range& e) {
EXPECT_STREQ("BasicStringRef::at()", e.what());
}
}
TEST(StringRefTest, EmptyString) {
StringRef sref("1234", 0);
EXPECT_EQ(0, sref.size());
EXPECT_TRUE(sref.empty());
}
TEST(StringRefTest, CompareLess) {
EXPECT_LT(StringRef("0000").compare("0123"), 0);
EXPECT_LT(StringRef("0123").compare("123"), 0);
EXPECT_LT(StringRef("012").compare("0123"), 0);
EXPECT_LT(StringRef("").compare("0"), 0);
}
TEST(StringRefTest, CompareGreater) {
EXPECT_GT(StringRef("0123").compare("0000"), 0);
EXPECT_GT(StringRef("123").compare("0123"), 0);
EXPECT_GT(StringRef("0123").compare("012"), 0);
EXPECT_GT(StringRef("0").compare(""), 0);
}
TEST(StringRefTest, CompareEqual) {
EXPECT_EQ(StringRef("1234"), StringRef(std::string("1234")));
EXPECT_EQ(StringRef("234"), StringRef(&"12345"[1], 3));
EXPECT_EQ(StringRef(""), "");
EXPECT_EQ(StringRef(""), std::string());
EXPECT_EQ(StringRef(""), StringRef(&"123456"[6], 0));
EXPECT_NE(StringRef("1234"), StringRef("1234", 2));
EXPECT_NE(StringRef("1234"), "");
}