/Users/deen/code/yugabyte-db/src/yb/gutil/strings/string_util-test.cc
Line  | Count  | Source  | 
1  |  | // Licensed to the Apache Software Foundation (ASF) under one  | 
2  |  | // or more contributor license agreements.  See the NOTICE file  | 
3  |  | // distributed with this work for additional information  | 
4  |  | // regarding copyright ownership.  The ASF licenses this file  | 
5  |  | // to you under the Apache License, Version 2.0 (the  | 
6  |  | // "License"); you may not use this file except in compliance  | 
7  |  | // with the License.  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,  | 
12  |  | // software distributed under the License is distributed on an  | 
13  |  | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  | 
14  |  | // KIND, either express or implied.  See the License for the  | 
15  |  | // specific language governing permissions and limitations  | 
16  |  | // under the License.  | 
17  |  | //  | 
18  |  | // The following only applies to changes made to this file as part of YugaByte development.  | 
19  |  | //  | 
20  |  | // Portions Copyright (c) YugaByte, Inc.  | 
21  |  | //  | 
22  |  | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except  | 
23  |  | // in compliance with the License.  You may obtain a copy of the License at  | 
24  |  | //  | 
25  |  | // http://www.apache.org/licenses/LICENSE-2.0  | 
26  |  | //  | 
27  |  | // Unless required by applicable law or agreed to in writing, software distributed under the License  | 
28  |  | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express  | 
29  |  | // or implied.  See the License for the specific language governing permissions and limitations  | 
30  |  | // under the License.  | 
31  |  | //  | 
32  |  | // Some portions Copyright 2013 The Chromium Authors. All rights reserved.  | 
33  |  |  | 
34  |  | #include "yb/gutil/strings/util.h"  | 
35  |  | #include "yb/util/string_util.h"  | 
36  |  | #include "yb/util/test_util.h"  | 
37  |  |  | 
38  |  | #include <gtest/gtest.h>  | 
39  |  |  | 
40  |  | using std::string;  | 
41  |  |  | 
42  |  | namespace yb { | 
43  |  |  | 
44  |  | class StringUtilTest : public YBTest { | 
45  |  | };  | 
46  |  |  | 
47  | 1  | TEST_F(StringUtilTest, MatchPatternTest) { | 
48  | 1  |   EXPECT_TRUE(MatchPattern("www.google.com", "*.com")); | 
49  | 1  |   EXPECT_TRUE(MatchPattern("www.google.com", "*")); | 
50  | 1  |   EXPECT_FALSE(MatchPattern("www.google.com", "www*.g*.org")); | 
51  | 1  |   EXPECT_TRUE(MatchPattern("Hello", "H?l?o")); | 
52  | 1  |   EXPECT_FALSE(MatchPattern("www.google.com", "http://*)")); | 
53  | 1  |   EXPECT_FALSE(MatchPattern("www.msn.com", "*.COM")); | 
54  | 1  |   EXPECT_TRUE(MatchPattern("Hello*1234", "He??o\\*1*")); | 
55  | 1  |   EXPECT_FALSE(MatchPattern("", "*.*")); | 
56  | 1  |   EXPECT_TRUE(MatchPattern("", "*")); | 
57  | 1  |   EXPECT_TRUE(MatchPattern("", "?")); | 
58  | 1  |   EXPECT_TRUE(MatchPattern("", "")); | 
59  | 1  |   EXPECT_FALSE(MatchPattern("Hello", "")); | 
60  | 1  |   EXPECT_TRUE(MatchPattern("Hello*", "Hello*")); | 
61  |  |   // Stop after a certain recursion depth.  | 
62  | 1  |   EXPECT_FALSE(MatchPattern("123456789012345678", "?????????????????*")); | 
63  |  |  | 
64  |  |   // Test UTF8 matching.  | 
65  | 1  |   EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0", "*\xe2\x99\xa0")); | 
66  | 1  |   EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0.", "heart: ?.")); | 
67  | 1  |   EXPECT_TRUE(MatchPattern("hearts: \xe2\x99\xa0\xe2\x99\xa0", "*")); | 
68  |  |   // Invalid sequences should be handled as a single invalid character.  | 
69  | 1  |   EXPECT_TRUE(MatchPattern("invalid: \xef\xbf\xbe", "invalid: ?")); | 
70  |  |   // If the pattern has invalid characters, it shouldn't match anything.  | 
71  | 1  |   EXPECT_FALSE(MatchPattern("\xf4\x90\x80\x80", "\xf4\x90\x80\x80")); | 
72  |  |  | 
73  |  |   // This test verifies that consecutive wild cards are collapsed into 1  | 
74  |  |   // wildcard (when this doesn't occur, MatchPattern reaches it's maximum  | 
75  |  |   // recursion depth).  | 
76  | 1  |   EXPECT_TRUE(MatchPattern("Hello" , | 
77  | 1  |                            "He********************************o"));  | 
78  | 1  | }  | 
79  |  |  | 
80  | 1  | TEST_F(StringUtilTest, TestIsBigInteger) { | 
81  | 1  |   ASSERT_TRUE(IsBigInteger("0")); | 
82  | 1  |   ASSERT_TRUE(IsBigInteger("1234")); | 
83  | 1  |   ASSERT_TRUE(IsBigInteger("-1234")); | 
84  | 1  |   ASSERT_TRUE(IsBigInteger("+1234")); | 
85  | 1  |   ASSERT_TRUE(IsBigInteger("0000")); | 
86  | 1  |   ASSERT_TRUE(IsBigInteger("00001234")); | 
87  | 1  |   ASSERT_TRUE(IsBigInteger("-00001234")); | 
88  | 1  |   ASSERT_TRUE(IsBigInteger("+00001234")); | 
89  | 1  |   ASSERT_TRUE(IsBigInteger("111222333444555666777888999888777666555444333222111")); | 
90  | 1  |   ASSERT_FALSE(IsBigInteger("")); | 
91  | 1  |   ASSERT_FALSE(IsBigInteger(".")); | 
92  | 1  |   ASSERT_FALSE(IsBigInteger("0.")); | 
93  | 1  |   ASSERT_FALSE(IsBigInteger(".0")); | 
94  | 1  |   ASSERT_FALSE(IsBigInteger("0.0")); | 
95  | 1  |   ASSERT_FALSE(IsBigInteger("0,0")); | 
96  | 1  |   ASSERT_FALSE(IsBigInteger(" 0")); | 
97  | 1  |   ASSERT_FALSE(IsBigInteger("0 ")); | 
98  | 1  | }  | 
99  |  |  | 
100  | 1  | TEST_F(StringUtilTest, TestIsDecimal) { | 
101  |  |   // Integer cases  | 
102  | 1  |   ASSERT_TRUE(IsDecimal("0")); | 
103  | 1  |   ASSERT_TRUE(IsDecimal("1234")); | 
104  | 1  |   ASSERT_TRUE(IsDecimal("-1234")); | 
105  | 1  |   ASSERT_TRUE(IsDecimal("+1234")); | 
106  | 1  |   ASSERT_TRUE(IsDecimal("0000")); | 
107  | 1  |   ASSERT_TRUE(IsDecimal("00001234")); | 
108  | 1  |   ASSERT_TRUE(IsDecimal("-00001234")); | 
109  | 1  |   ASSERT_TRUE(IsDecimal("+00001234")); | 
110  | 1  |   ASSERT_TRUE(IsDecimal("111222333444555666777888999888777666555444333222111")); | 
111  |  |   // Decimal separator - regular  | 
112  | 1  |   ASSERT_TRUE(IsDecimal("0.0")); | 
113  | 1  |   ASSERT_TRUE(IsDecimal("1234.1234")); | 
114  | 1  |   ASSERT_TRUE(IsDecimal("-1234.1234")); | 
115  | 1  |   ASSERT_TRUE(IsDecimal("+1234.1234")); | 
116  | 1  |   ASSERT_TRUE(IsDecimal("0000.0000")); | 
117  | 1  |   ASSERT_TRUE(IsDecimal("00001234.12340000")); | 
118  | 1  |   ASSERT_TRUE(IsDecimal("-00001234.12340000")); | 
119  | 1  |   ASSERT_TRUE(IsDecimal("+00001234.12340000")); | 
120  | 1  |   ASSERT_TRUE(IsDecimal(string("111222333444555666777888999888777666555444333222111") | 
121  | 1  |                             + ".111222333444555666777888999888777666555444333222111"));  | 
122  |  |   // Decimal separator - irregular  | 
123  | 1  |   ASSERT_TRUE(IsDecimal("0.")); | 
124  | 1  |   ASSERT_TRUE(IsDecimal(".0")); | 
125  | 1  |   ASSERT_TRUE(IsDecimal("0.0")); | 
126  |  |   // Exponent  | 
127  | 1  |   ASSERT_TRUE(IsDecimal("0e0")); | 
128  | 1  |   ASSERT_TRUE(IsDecimal("1e2")); | 
129  | 1  |   ASSERT_TRUE(IsDecimal("123E456")); | 
130  | 1  |   ASSERT_TRUE(IsDecimal("-123e-456")); | 
131  | 1  |   ASSERT_TRUE(IsDecimal("+123e+456")); | 
132  | 1  |   ASSERT_TRUE(IsDecimal("000123e000456")); | 
133  | 1  |   ASSERT_TRUE(IsDecimal(string("111222333444555666777888999888777666555444333222111") | 
134  | 1  |                             + "e111222333444555666777888999888777666555444333222111"));  | 
135  |  |   // Both exponent and decimal separator  | 
136  | 1  |   ASSERT_TRUE(IsDecimal("1.2e345")); | 
137  | 1  |   ASSERT_TRUE(IsDecimal(".123e1")); | 
138  | 1  |   ASSERT_TRUE(IsDecimal("-.123e1")); | 
139  | 1  |   ASSERT_TRUE(IsDecimal("+.123e1")); | 
140  | 1  |   ASSERT_TRUE(IsDecimal("123.e1")); | 
141  | 1  |   ASSERT_TRUE(IsDecimal("-123.e1")); | 
142  | 1  |   ASSERT_TRUE(IsDecimal("+123.e1")); | 
143  | 1  |   ASSERT_TRUE(IsDecimal(string("111222333444555666777888999888777666555444333222111") | 
144  | 1  |                             + ".111222333444555666777888999888777666555444333222111"  | 
145  | 1  |                             + "e111222333444555666777888999888777666555444333222111"));  | 
146  |  |   // Non-decimals  | 
147  | 1  |   ASSERT_FALSE(IsDecimal(" 0")); | 
148  | 1  |   ASSERT_FALSE(IsDecimal("0 ")); | 
149  | 1  |   ASSERT_FALSE(IsDecimal("")); | 
150  | 1  |   ASSERT_FALSE(IsDecimal(".")); | 
151  | 1  |   ASSERT_FALSE(IsDecimal("0,0")); | 
152  | 1  |   ASSERT_FALSE(IsDecimal("0e")); | 
153  | 1  |   ASSERT_FALSE(IsDecimal("0e0.0")); | 
154  | 1  |   ASSERT_FALSE(IsDecimal("0e.0")); | 
155  | 1  |   ASSERT_FALSE(IsDecimal("0e0.")); | 
156  | 1  | }  | 
157  |  |  | 
158  | 1  | TEST_F(StringUtilTest, TestIsBoolean) { | 
159  | 1  |   ASSERT_TRUE(IsBoolean("true")); | 
160  | 1  |   ASSERT_TRUE(IsBoolean("TRUE")); | 
161  | 1  |   ASSERT_TRUE(IsBoolean("fAlSe")); | 
162  | 1  |   ASSERT_TRUE(IsBoolean("falsE")); | 
163  | 1  |   ASSERT_FALSE(IsBoolean("")); | 
164  | 1  |   ASSERT_FALSE(IsBoolean("0")); | 
165  | 1  |   ASSERT_FALSE(IsBoolean("1")); | 
166  | 1  |   ASSERT_FALSE(IsBoolean(" true")); | 
167  | 1  |   ASSERT_FALSE(IsBoolean("false ")); | 
168  | 1  | }  | 
169  |  |  | 
170  | 1  | TEST_F(StringUtilTest, TestAppendWithSeparator) { | 
171  | 1  |   string s;  | 
172  | 1  |   AppendWithSeparator("foo", &s); | 
173  | 1  |   ASSERT_EQ(s, "foo");  | 
174  | 1  |   AppendWithSeparator("bar", &s); | 
175  | 1  |   ASSERT_EQ(s, "foo, bar");  | 
176  | 1  |   AppendWithSeparator("foo", &s, " -- "); | 
177  | 1  |   ASSERT_EQ(s, "foo, bar -- foo");  | 
178  |  |  | 
179  | 1  |   s = "";  | 
180  | 1  |   AppendWithSeparator(string("foo"), &s); | 
181  | 1  |   ASSERT_EQ(s, "foo");  | 
182  | 1  |   AppendWithSeparator(string("bar"), &s); | 
183  | 1  |   ASSERT_EQ(s, "foo, bar");  | 
184  | 1  |   AppendWithSeparator(string("foo"), &s, " -- "); | 
185  | 1  |   ASSERT_EQ(s, "foo, bar -- foo");  | 
186  | 1  | }  | 
187  |  |  | 
188  | 1  | TEST_F(StringUtilTest, TestCollectionToString) { | 
189  | 1  |   std::vector<std::string> v{"foo", "123", "bar", ""}; | 
190  | 1  |   ASSERT_EQ("[foo, 123, bar, ]", VectorToString(v)); | 
191  | 1  |   ASSERT_EQ("[foo, 123, bar, ]", RangeToString(v.begin(), v.end())); | 
192  | 1  |   ASSERT_EQ("[]", RangeToString(v.begin(), v.begin())); | 
193  | 1  |   ASSERT_EQ("[foo]", RangeToString(v.begin(), v.begin() + 1)); | 
194  | 1  | }  | 
195  |  |  | 
196  | 1  | TEST_F(StringUtilTest, TestStringStartsWithOrEquals) { | 
197  | 1  |   ASSERT_TRUE(StringStartsWithOrEquals("", "")); | 
198  | 1  |   ASSERT_TRUE(StringStartsWithOrEquals("abc", "")); | 
199  | 1  |   ASSERT_TRUE(StringStartsWithOrEquals("abc", "ab")); | 
200  | 1  |   ASSERT_TRUE(StringStartsWithOrEquals("abc", "abc")); | 
201  | 1  |   ASSERT_FALSE(StringStartsWithOrEquals("abc", "abd")); | 
202  | 1  |   ASSERT_FALSE(StringStartsWithOrEquals("abc", "abcd")); | 
203  | 1  | }  | 
204  |  |  | 
205  | 1  | TEST_F(StringUtilTest, TestSplitAndFlatten) { | 
206  | 1  |   ASSERT_EQ("[foo, bar, baz]", VectorToString(SplitAndFlatten( | 
207  | 1  |       {"foo,bar", "baz"}))); | 
208  | 1  |   ASSERT_EQ("[foo, bar, baz, foo]", VectorToString(SplitAndFlatten( | 
209  | 1  |       {"foo", "bar:baz", "foo"}, ":"))); | 
210  | 1  | }  | 
211  |  |  | 
212  |  | } // namespace yb  |