1 // Copyright (C) 2019 The Android Open Source Project
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 "utils/string_utils.h"
16 
17 #include <gtest/gtest.h>
18 
19 
20 namespace header_checker {
21 namespace utils {
22 
23 
TEST(StringUtilsTest,Trim)24 TEST(StringUtilsTest, Trim) {
25   EXPECT_EQ("a b", Trim(" a b "));
26   EXPECT_EQ("a b", Trim(" a b"));
27   EXPECT_EQ("a b", Trim("a b "));
28   EXPECT_EQ("a b", Trim("a b"));
29   EXPECT_EQ("a b", Trim("\ta b\n"));
30 }
31 
32 
TEST(StringUtilsTest,StartsWith)33 TEST(StringUtilsTest, StartsWith) {
34   EXPECT_TRUE(StartsWith("abcd", "ab"));
35   EXPECT_TRUE(StartsWith("a", "a"));
36   EXPECT_TRUE(StartsWith("a", ""));
37   EXPECT_TRUE(StartsWith("", ""));
38 
39   EXPECT_FALSE(StartsWith("ab", "abcd"));
40   EXPECT_FALSE(StartsWith("", "ab"));
41 }
42 
43 
TEST(StringUtilsTest,EndsWith)44 TEST(StringUtilsTest, EndsWith) {
45   EXPECT_TRUE(EndsWith("abcd", "cd"));
46   EXPECT_TRUE(EndsWith("d", "d"));
47   EXPECT_TRUE(EndsWith("d", ""));
48   EXPECT_TRUE(EndsWith("", ""));
49 
50   EXPECT_FALSE(EndsWith("cd", "abcd"));
51   EXPECT_FALSE(EndsWith("", "cd"));
52 }
53 
54 
TEST(StringUtilsTest,Split)55 TEST(StringUtilsTest, Split) {
56   std::vector<std::string_view> xs;
57 
58   xs = Split("   a  bb   ccc ", " ");
59   EXPECT_EQ(3, xs.size());
60   EXPECT_EQ("a", xs[0]);
61   EXPECT_EQ("bb", xs[1]);
62   EXPECT_EQ("ccc", xs[2]);
63 
64   xs = Split("a", " ");
65   EXPECT_EQ(1, xs.size());
66   EXPECT_EQ("a", xs[0]);
67 
68   xs = Split("a b", " ");
69   EXPECT_EQ(2, xs.size());
70   EXPECT_EQ("a", xs[0]);
71   EXPECT_EQ("b", xs[1]);
72 
73   xs = Split("a \t \t \tb", " \t");
74   EXPECT_EQ(2, xs.size());
75   EXPECT_EQ("a", xs[0]);
76   EXPECT_EQ("b", xs[1]);
77 }
78 
79 
TEST(StringUtilsTest,ParseInt)80 TEST(StringUtilsTest, ParseInt) {
81   EXPECT_FALSE(ParseInt(""));
82   EXPECT_FALSE(ParseInt("a"));
83   EXPECT_FALSE(ParseInt("0xa"));
84   EXPECT_FALSE(ParseInt("16h"));
85 
86   EXPECT_TRUE(ParseInt("0").hasValue());
87   EXPECT_EQ(0, ParseInt("0").getValue());
88 
89   EXPECT_TRUE(ParseInt("16").hasValue());
90   EXPECT_EQ(16, ParseInt("16").getValue());
91 
92   EXPECT_TRUE(ParseInt("-16").hasValue());
93   EXPECT_EQ(-16, ParseInt("-16").getValue());
94 }
95 
96 
TEST(StringUtilsTest,ParseBool)97 TEST(StringUtilsTest, ParseBool) {
98   EXPECT_FALSE(ParseBool(""));
99   EXPECT_FALSE(ParseBool("false"));
100   EXPECT_FALSE(ParseBool("off"));
101   EXPECT_FALSE(ParseBool("0"));
102 
103   EXPECT_TRUE(ParseBool("TRUE"));
104   EXPECT_TRUE(ParseBool("True"));
105   EXPECT_TRUE(ParseBool("true"));
106   EXPECT_TRUE(ParseBool("ON"));
107   EXPECT_TRUE(ParseBool("1"));
108 }
109 
110 
TEST(StringUtilsTest,IsGlobPattern)111 TEST(StringUtilsTest, IsGlobPattern) {
112   EXPECT_TRUE(IsGlobPattern("*.so"));
113   EXPECT_TRUE(IsGlobPattern("[ab].txt"));
114   EXPECT_TRUE(IsGlobPattern("?.txt"));
115 
116   EXPECT_FALSE(IsGlobPattern("name"));
117   EXPECT_FALSE(IsGlobPattern(".txt"));
118   EXPECT_FALSE(IsGlobPattern(""));
119 }
120 
121 
122 }  // namespace utils
123 }  // namespace header_checker
124