1 /*
2 Copyright (c) DataStax, Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "string_ref.hpp"
20
21 using datastax::StringRef;
22
TEST(StringRefUnitTest,Compare)23 TEST(StringRefUnitTest, Compare) {
24 const char* value = "abc";
25 StringRef s(value);
26
27 // Equals
28 EXPECT_EQ(s.compare(s), 0);
29 EXPECT_EQ(s, s);
30 EXPECT_EQ(s, value);
31
32 // Not equals
33 EXPECT_NE(s, "xyz");
34 EXPECT_NE(s, StringRef("xyz"));
35
36 // Case insensitive
37 EXPECT_TRUE(s.iequals("ABC"));
38 EXPECT_TRUE(iequals(s, "ABC"));
39 }
40
TEST(StringRefUnitTest,Empty)41 TEST(StringRefUnitTest, Empty) {
42 StringRef s;
43
44 EXPECT_TRUE(s.empty());
45 EXPECT_EQ(s, "");
46 EXPECT_NE(s, "abc");
47
48 EXPECT_TRUE(starts_with(s, ""));
49 EXPECT_TRUE(ends_with(s, ""));
50
51 EXPECT_FALSE(starts_with(s, "abc"));
52 EXPECT_FALSE(ends_with(s, "abc"));
53 }
54
TEST(StringRefUnitTest,Substr)55 TEST(StringRefUnitTest, Substr) {
56 StringRef s("abcxyz");
57
58 // Full string
59 EXPECT_EQ(s.substr(0, s.length()), s);
60
61 // Exceeds length
62 EXPECT_EQ(s.substr(0, s.length() + 1), s);
63 EXPECT_EQ(s.substr(0, StringRef::npos), s);
64
65 // More tests in "starts_with" and "ends_with"
66 }
67
TEST(StringRefUnitTest,Find)68 TEST(StringRefUnitTest, Find) {
69 StringRef s("abcxyz");
70
71 EXPECT_EQ(s.find(""), 0u);
72 EXPECT_EQ(s.find("abc"), 0u);
73 EXPECT_EQ(s.find("xyz"), 3u);
74 EXPECT_EQ(s.find("z"), 5u);
75
76 EXPECT_EQ(s.find("invalid"), StringRef::npos);
77 EXPECT_EQ(s.find("abcxyza"), StringRef::npos);
78
79 EXPECT_EQ(s.find(""), 0u);
80 EXPECT_EQ(StringRef("").find(""), 0u);
81 }
82
TEST(StringRefUnitTest,StartsWith)83 TEST(StringRefUnitTest, StartsWith) {
84 StringRef s("abcxyz");
85
86 // Various lengths
87 for (size_t i = 0; i < s.length(); ++i) {
88 EXPECT_TRUE(starts_with(s, s.substr(0, i)));
89 }
90
91 // Does not start with
92 EXPECT_FALSE(starts_with(s, "xyz"));
93
94 // Too long
95 EXPECT_FALSE(starts_with(s, "abcxyzabcxyz"));
96 }
97
TEST(StringRefUnitTest,EndsWith)98 TEST(StringRefUnitTest, EndsWith) {
99 StringRef s("abcxyz");
100
101 // Various lengths
102 for (size_t i = 0; i < s.length(); ++i) {
103 EXPECT_TRUE(ends_with(s, s.substr(i, StringRef::npos)));
104 }
105
106 // Does not end with
107 EXPECT_FALSE(ends_with(s, "abc"));
108
109 // Too long
110 EXPECT_FALSE(ends_with(s, "abcxyzabcxyz"));
111 }
112