1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpcpp/support/string_ref.h>
20
21 #include <string.h>
22
23 #include <gtest/gtest.h>
24
25 #include "test/core/util/test_config.h"
26
27 namespace grpc {
28 namespace {
29
30 const char kTestString[] = "blah";
31 const char kTestStringWithEmbeddedNull[] = "blah\0foo";
32 const size_t kTestStringWithEmbeddedNullLength = 8;
33 const char kTestUnrelatedString[] = "foo";
34
35 class StringRefTest : public ::testing::Test {};
36
TEST_F(StringRefTest,Empty)37 TEST_F(StringRefTest, Empty) {
38 string_ref s;
39 EXPECT_EQ(0U, s.length());
40 EXPECT_EQ(nullptr, s.data());
41 }
42
TEST_F(StringRefTest,FromCString)43 TEST_F(StringRefTest, FromCString) {
44 string_ref s(kTestString);
45 EXPECT_EQ(strlen(kTestString), s.length());
46 EXPECT_EQ(kTestString, s.data());
47 }
48
TEST_F(StringRefTest,FromCStringWithLength)49 TEST_F(StringRefTest, FromCStringWithLength) {
50 string_ref s(kTestString, 2);
51 EXPECT_EQ(2U, s.length());
52 EXPECT_EQ(kTestString, s.data());
53 }
54
TEST_F(StringRefTest,FromString)55 TEST_F(StringRefTest, FromString) {
56 string copy(kTestString);
57 string_ref s(copy);
58 EXPECT_EQ(copy.data(), s.data());
59 EXPECT_EQ(copy.length(), s.length());
60 }
61
TEST_F(StringRefTest,CopyConstructor)62 TEST_F(StringRefTest, CopyConstructor) {
63 string_ref s1(kTestString);
64 ;
65 const string_ref& s2(s1);
66 EXPECT_EQ(s1.length(), s2.length());
67 EXPECT_EQ(s1.data(), s2.data());
68 }
69
TEST_F(StringRefTest,FromStringWithEmbeddedNull)70 TEST_F(StringRefTest, FromStringWithEmbeddedNull) {
71 string copy(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
72 string_ref s(copy);
73 EXPECT_EQ(copy.data(), s.data());
74 EXPECT_EQ(copy.length(), s.length());
75 EXPECT_EQ(kTestStringWithEmbeddedNullLength, s.length());
76 }
77
TEST_F(StringRefTest,Assignment)78 TEST_F(StringRefTest, Assignment) {
79 string_ref s1(kTestString);
80 ;
81 string_ref s2;
82 EXPECT_EQ(nullptr, s2.data());
83 s2 = s1;
84 EXPECT_EQ(s1.length(), s2.length());
85 EXPECT_EQ(s1.data(), s2.data());
86 }
87
TEST_F(StringRefTest,Iterator)88 TEST_F(StringRefTest, Iterator) {
89 string_ref s(kTestString);
90 size_t i = 0;
91 for (auto it = s.cbegin(); it != s.cend(); ++it) {
92 auto val = kTestString[i++];
93 EXPECT_EQ(val, *it);
94 }
95 EXPECT_EQ(strlen(kTestString), i);
96 }
97
TEST_F(StringRefTest,ReverseIterator)98 TEST_F(StringRefTest, ReverseIterator) {
99 string_ref s(kTestString);
100 size_t i = strlen(kTestString);
101 for (auto rit = s.crbegin(); rit != s.crend(); ++rit) {
102 auto val = kTestString[--i];
103 EXPECT_EQ(val, *rit);
104 }
105 EXPECT_EQ(0U, i);
106 }
107
TEST_F(StringRefTest,Capacity)108 TEST_F(StringRefTest, Capacity) {
109 string_ref empty;
110 EXPECT_EQ(0U, empty.length());
111 EXPECT_EQ(0U, empty.size());
112 EXPECT_EQ(0U, empty.max_size());
113 EXPECT_TRUE(empty.empty());
114
115 string_ref s(kTestString);
116 EXPECT_EQ(strlen(kTestString), s.length());
117 EXPECT_EQ(s.length(), s.size());
118 EXPECT_EQ(s.max_size(), s.length());
119 EXPECT_FALSE(s.empty());
120 }
121
TEST_F(StringRefTest,Compare)122 TEST_F(StringRefTest, Compare) {
123 string_ref s1(kTestString);
124 string s1_copy(kTestString);
125 string_ref s2(kTestUnrelatedString);
126 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
127 EXPECT_EQ(0, s1.compare(s1_copy));
128 EXPECT_NE(0, s1.compare(s2));
129 EXPECT_NE(0, s1.compare(s3));
130 }
131
TEST_F(StringRefTest,StartsWith)132 TEST_F(StringRefTest, StartsWith) {
133 string_ref s1(kTestString);
134 string_ref s2(kTestUnrelatedString);
135 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
136 EXPECT_TRUE(s1.starts_with(s1));
137 EXPECT_FALSE(s1.starts_with(s2));
138 EXPECT_FALSE(s2.starts_with(s1));
139 EXPECT_FALSE(s1.starts_with(s3));
140 EXPECT_TRUE(s3.starts_with(s1));
141 }
142
TEST_F(StringRefTest,Endswith)143 TEST_F(StringRefTest, Endswith) {
144 string_ref s1(kTestString);
145 string_ref s2(kTestUnrelatedString);
146 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
147 EXPECT_TRUE(s1.ends_with(s1));
148 EXPECT_FALSE(s1.ends_with(s2));
149 EXPECT_FALSE(s2.ends_with(s1));
150 EXPECT_FALSE(s2.ends_with(s3));
151 EXPECT_TRUE(s3.ends_with(s2));
152 }
153
TEST_F(StringRefTest,Find)154 TEST_F(StringRefTest, Find) {
155 string_ref s1(kTestString);
156 string_ref s2(kTestUnrelatedString);
157 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
158 EXPECT_EQ(0U, s1.find(s1));
159 EXPECT_EQ(0U, s2.find(s2));
160 EXPECT_EQ(0U, s3.find(s3));
161 EXPECT_EQ(string_ref::npos, s1.find(s2));
162 EXPECT_EQ(string_ref::npos, s2.find(s1));
163 EXPECT_EQ(string_ref::npos, s1.find(s3));
164 EXPECT_EQ(0U, s3.find(s1));
165 EXPECT_EQ(5U, s3.find(s2));
166 EXPECT_EQ(string_ref::npos, s1.find('z'));
167 EXPECT_EQ(1U, s2.find('o'));
168 }
169
TEST_F(StringRefTest,SubString)170 TEST_F(StringRefTest, SubString) {
171 string_ref s(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
172 string_ref sub1 = s.substr(0, 4);
173 EXPECT_EQ(string_ref(kTestString), sub1);
174 string_ref sub2 = s.substr(5);
175 EXPECT_EQ(string_ref(kTestUnrelatedString), sub2);
176 }
177
TEST_F(StringRefTest,ComparisonOperators)178 TEST_F(StringRefTest, ComparisonOperators) {
179 string_ref s1(kTestString);
180 string_ref s2(kTestUnrelatedString);
181 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
182 EXPECT_EQ(s1, s1);
183 EXPECT_EQ(s2, s2);
184 EXPECT_EQ(s3, s3);
185 EXPECT_GE(s1, s1);
186 EXPECT_GE(s2, s2);
187 EXPECT_GE(s3, s3);
188 EXPECT_LE(s1, s1);
189 EXPECT_LE(s2, s2);
190 EXPECT_LE(s3, s3);
191 EXPECT_NE(s1, s2);
192 EXPECT_NE(s1, s3);
193 EXPECT_NE(s2, s3);
194 EXPECT_GT(s3, s1);
195 EXPECT_LT(s1, s3);
196 }
197
198 } // namespace
199 } // namespace grpc
200
main(int argc,char ** argv)201 int main(int argc, char** argv) {
202 grpc::testing::TestEnvironment env(argc, argv);
203 ::testing::InitGoogleTest(&argc, argv);
204 return RUN_ALL_TESTS();
205 }
206