1 // Copyright 2017 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "string_piece_util.h"
16
17 #include "test.h"
18
19 using namespace std;
20
TEST(StringPieceUtilTest,SplitStringPiece)21 TEST(StringPieceUtilTest, SplitStringPiece) {
22 {
23 string input("a:b:c");
24 vector<StringPiece> list = SplitStringPiece(input, ':');
25
26 EXPECT_EQ(list.size(), 3);
27
28 EXPECT_EQ(list[0], "a");
29 EXPECT_EQ(list[1], "b");
30 EXPECT_EQ(list[2], "c");
31 }
32
33 {
34 string empty;
35 vector<StringPiece> list = SplitStringPiece(empty, ':');
36
37 EXPECT_EQ(list.size(), 1);
38
39 EXPECT_EQ(list[0], "");
40 }
41
42 {
43 string one("a");
44 vector<StringPiece> list = SplitStringPiece(one, ':');
45
46 EXPECT_EQ(list.size(), 1);
47
48 EXPECT_EQ(list[0], "a");
49 }
50
51 {
52 string sep_only(":");
53 vector<StringPiece> list = SplitStringPiece(sep_only, ':');
54
55 EXPECT_EQ(list.size(), 2);
56
57 EXPECT_EQ(list[0], "");
58 EXPECT_EQ(list[1], "");
59 }
60
61 {
62 string sep(":a:b:c:");
63 vector<StringPiece> list = SplitStringPiece(sep, ':');
64
65 EXPECT_EQ(list.size(), 5);
66
67 EXPECT_EQ(list[0], "");
68 EXPECT_EQ(list[1], "a");
69 EXPECT_EQ(list[2], "b");
70 EXPECT_EQ(list[3], "c");
71 EXPECT_EQ(list[4], "");
72 }
73 }
74
TEST(StringPieceUtilTest,JoinStringPiece)75 TEST(StringPieceUtilTest, JoinStringPiece) {
76 {
77 string input("a:b:c");
78 vector<StringPiece> list = SplitStringPiece(input, ':');
79
80 EXPECT_EQ("a:b:c", JoinStringPiece(list, ':'));
81 EXPECT_EQ("a/b/c", JoinStringPiece(list, '/'));
82 }
83
84 {
85 string empty;
86 vector<StringPiece> list = SplitStringPiece(empty, ':');
87
88 EXPECT_EQ("", JoinStringPiece(list, ':'));
89 }
90
91 {
92 vector<StringPiece> empty_list;
93
94 EXPECT_EQ("", JoinStringPiece(empty_list, ':'));
95 }
96
97 {
98 string one("a");
99 vector<StringPiece> single_list = SplitStringPiece(one, ':');
100
101 EXPECT_EQ("a", JoinStringPiece(single_list, ':'));
102 }
103
104 {
105 string sep(":a:b:c:");
106 vector<StringPiece> list = SplitStringPiece(sep, ':');
107
108 EXPECT_EQ(":a:b:c:", JoinStringPiece(list, ':'));
109 }
110 }
111
TEST(StringPieceUtilTest,ToLowerASCII)112 TEST(StringPieceUtilTest, ToLowerASCII) {
113 EXPECT_EQ('a', ToLowerASCII('A'));
114 EXPECT_EQ('z', ToLowerASCII('Z'));
115 EXPECT_EQ('a', ToLowerASCII('a'));
116 EXPECT_EQ('z', ToLowerASCII('z'));
117 EXPECT_EQ('/', ToLowerASCII('/'));
118 EXPECT_EQ('1', ToLowerASCII('1'));
119 }
120
TEST(StringPieceUtilTest,EqualsCaseInsensitiveASCII)121 TEST(StringPieceUtilTest, EqualsCaseInsensitiveASCII) {
122 EXPECT_TRUE(EqualsCaseInsensitiveASCII("abc", "abc"));
123 EXPECT_TRUE(EqualsCaseInsensitiveASCII("abc", "ABC"));
124 EXPECT_TRUE(EqualsCaseInsensitiveASCII("abc", "aBc"));
125 EXPECT_TRUE(EqualsCaseInsensitiveASCII("AbC", "aBc"));
126 EXPECT_TRUE(EqualsCaseInsensitiveASCII("", ""));
127
128 EXPECT_FALSE(EqualsCaseInsensitiveASCII("a", "ac"));
129 EXPECT_FALSE(EqualsCaseInsensitiveASCII("/", "\\"));
130 EXPECT_FALSE(EqualsCaseInsensitiveASCII("1", "10"));
131 }
132