2 * Copyright (c) 2019 The Regents of the University of California
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <gtest/gtest.h>
42 #include "base/str.hh"
45 * str.cc has "eat_lead_white", "eat_end_white", and "eat_white" fucntions to
46 * remove leading and trailing whitespace. The following tests verify this
49 TEST(StrTest
, EatLeadWhite
)
51 std::string val
= " hello there ";
53 EXPECT_EQ("hello there ", val
);
56 TEST(StrTest
, EatLeadWhiteNoLeadingWhitespace
)
58 std::string val
= "hello there ";
60 EXPECT_EQ("hello there ", val
);
63 TEST(StrTest
, EatEndWhite
)
65 std::string val
= " hello there ";
67 EXPECT_EQ(" hello there", val
);
70 TEST(StrTest
, EatEndWhiteNoTrailingWhitespace
)
72 std::string val
= " hello there";
74 EXPECT_EQ(" hello there", val
);
77 TEST(StrTest
, EatWhite
)
79 std::string val
= " hello there ";
81 EXPECT_EQ("hello there", val
);
84 TEST(StrTest
, EatWhiteNoWhitespace
)
86 std::string val
= "hello there";
88 EXPECT_EQ("hello there", val
);
92 * This tests checks that str.cc's "to_lower" function converts a string to
95 TEST(StrTest
, ToLower
)
97 std::string val
= "gOoDbYe FOO@barr!";
98 EXPECT_EQ("goodbye foo@barr!", to_lower(val
));
102 * str.cc's "split_first" and "split_last" fucntions split a string on a
103 * character into two parts. "split_first" splits on the first instance of
104 * this character and "split_last" splits on the last instance of this
105 * character. The character itself is not included in either of the output
106 * right-hand side and left-hand side strings. If the character cannot be
107 * found in the string then the left-hand side string is equal to the input
108 * string and the right-hand side string is empty.
110 TEST(StrTest
, SplitFirst
)
112 std::string val
= "abcdefg abcdefg";
116 split_first(val
, lhs
, rhs
, 'f');
117 EXPECT_EQ("abcdefg abcdefg", val
);
118 EXPECT_EQ("abcde", lhs
);
119 EXPECT_EQ("g abcdefg", rhs
);
122 TEST(StrTest
, SplitFirstNoChar
)
124 std::string val
= "abcdefg abcdefg";
128 split_first(val
, lhs
, rhs
, 'h');
129 EXPECT_EQ("abcdefg abcdefg", val
);
130 EXPECT_EQ("abcdefg abcdefg", lhs
);
134 TEST(StrTest
, SplitFirstOnFirstChar
)
136 std::string val
= "abcdefg abcdefg";
140 split_first(val
, lhs
, rhs
, 'a');
141 EXPECT_EQ("abcdefg abcdefg", val
);
143 EXPECT_EQ("bcdefg abcdefg", rhs
);
146 TEST(StrTest
, SplitLast
)
148 std::string val
= "abcdefg abcdefg";
152 split_last(val
, lhs
, rhs
, 'f');
153 EXPECT_EQ("abcdefg abcdefg", val
);
154 EXPECT_EQ("abcdefg abcde", lhs
);
158 TEST(StrTest
, SplitLastNoChar
)
160 std::string val
= "abcdefg abcdefg";
164 split_last(val
, lhs
, rhs
, 'h');
165 EXPECT_EQ("abcdefg abcdefg", val
);
166 EXPECT_EQ("abcdefg abcdefg", lhs
);
170 TEST(StrTest
, SplitLastOnLastChar
)
172 std::string val
= "abcdefg abcdefg";
176 split_last(val
, lhs
, rhs
, 'g');
177 EXPECT_EQ("abcdefg abcdefg", val
);
178 EXPECT_EQ("abcdefg abcdef", lhs
);
184 * str.cc's "tokenize" function splits a string into its constituent tokens.
185 * It splits based on an input character.
187 TEST(StrTest
, TokenizeOnSpace
)
190 * val has a double space between each token with trailing and leading
193 std::string val
= " Hello, this is a sentence. ";
194 std::vector
<std::string
> tokens
;
197 * By default 'ign' is true. This means empty tokens are not included in
200 tokenize(tokens
, val
, ' ');
201 EXPECT_EQ(" Hello, this is a sentence. ", val
);
202 EXPECT_EQ(5, tokens
.size());
203 EXPECT_EQ("Hello,", tokens
[0]);
204 EXPECT_EQ("this", tokens
[1]);
205 EXPECT_EQ("is", tokens
[2]);
206 EXPECT_EQ("a", tokens
[3]);
207 EXPECT_EQ("sentence.", tokens
[4]);
210 TEST(StrTest
, TokenizeOnSpaceIgnFalse
)
213 * val has a double space between each token with trailing and leading
216 std::string val
= " Hello, this is a sentence. ";
217 std::vector
<std::string
> tokens
;
219 tokenize(tokens
, val
, ' ', false);
220 EXPECT_EQ(" Hello, this is a sentence. ", val
);
221 EXPECT_EQ(11, tokens
.size());
222 EXPECT_EQ("", tokens
[0]);
223 EXPECT_EQ("Hello,", tokens
[1]);
224 EXPECT_EQ("", tokens
[2]);
225 EXPECT_EQ("this", tokens
[3]);
226 EXPECT_EQ("", tokens
[4]);
227 EXPECT_EQ("is", tokens
[5]);
228 EXPECT_EQ("", tokens
[6]);
229 EXPECT_EQ("a", tokens
[7]);
230 EXPECT_EQ("", tokens
[8]);
231 EXPECT_EQ("sentence.", tokens
[9]);
232 EXPECT_EQ("", tokens
[10]);
235 TEST(StrTest
, TokenizedTokenDoesNotExist
)
237 std::string val
= "abcdefg";
238 std::vector
<std::string
> tokens
;
240 tokenize(tokens
, val
, 'h');
241 EXPECT_EQ("abcdefg", val
);
242 EXPECT_EQ(1, tokens
.size());
243 EXPECT_EQ("abcdefg", tokens
[0]);
247 * str.cc's "to_number" function converts a string to a number. The function
248 * will return false if this is not possible either because the string
249 * represents a number out-of-range, or because the string cannot be parsed.
251 TEST(StrTest
, ToNumber8BitInt
)
254 std::string input
= "-128";
255 EXPECT_TRUE(to_number(input
, output
));
256 EXPECT_EQ(-128, output
);
259 TEST(StrTest
, ToNumber8BitIntStringOutOfRange
)
262 std::string input
= "-129";
263 EXPECT_FALSE(to_number(input
, output
));
266 TEST(StrTest
, ToNumber8BitIntInvalidString
)
269 std::string input
= "onetwoeight";
270 EXPECT_FALSE(to_number(input
, output
));
273 TEST(StrTest
, ToNumberUnsigned8BitInt
)
276 std::string input
= "255";
277 EXPECT_TRUE(to_number(input
, output
));
278 EXPECT_EQ(255, output
);
281 TEST(StrTest
, ToNumberUnsigned8BitIntNegative
)
284 std::string input
= "-1";
285 EXPECT_FALSE(to_number(input
, output
));
288 TEST(StrTest
, ToNumber64BitInt
)
291 int64_t input_number
= 0xFFFFFFFFFFFFFFFF;
292 std::string input
= std::to_string(input_number
);
293 EXPECT_TRUE(to_number(input
, output
));
294 EXPECT_EQ(input_number
, output
);
297 TEST(StrTest
, ToNumber64BitIntInvalidString
)
300 std::string input
= " ";
301 EXPECT_FALSE(to_number(input
, output
));
304 TEST(StrTest
, ToNumberFloat
)
307 std::string input
= "0.1";
308 float expected_output
= 0.1;
309 EXPECT_TRUE(to_number(input
, output
));
310 EXPECT_EQ(expected_output
, output
);
313 TEST(StrTest
, ToNumberFloatIntegerString
)
316 std::string input
= "10";
317 float expected_output
= 10.0;
318 EXPECT_TRUE(to_number(input
, output
));
319 EXPECT_EQ(expected_output
, output
);
322 TEST(StrTest
, ToNumberFloatNegative
)
325 std::string input
= "-0.1";
326 float expected_output
= -0.1;
327 EXPECT_TRUE(to_number(input
, output
));
328 EXPECT_EQ(expected_output
, output
);
331 TEST(StrTest
, ToNumberDouble
)
334 std::string input
= "0.0001";
335 double expected_output
= 0.0001;
336 EXPECT_TRUE(to_number(input
, output
));
337 EXPECT_EQ(expected_output
, output
);
340 TEST(StrTest
, ToNumberDoubleIntegerString
)
343 std::string input
= "12345";
344 double expected_output
= 12345.0;
345 EXPECT_TRUE(to_number(input
, output
));
346 EXPECT_EQ(expected_output
, output
);
349 TEST(StrTest
, ToNumberDoubleNegative
)
352 std::string input
= "-1.2345";
353 double expected_output
= -1.2345;
354 EXPECT_TRUE(to_number(input
, output
));
355 EXPECT_EQ(expected_output
, output
);
359 * The "to_bool" function takes a string, "true" or "false"
360 * (case-insenstive), and sets the second argument to the bool equivilent.
361 * The function will return false if it cannot parse the string.
363 TEST(StrTest
, ToBoolTrue
)
366 EXPECT_TRUE(to_bool("TrUe", output
));
370 TEST(StrTest
, ToBoolFalse
){
372 EXPECT_TRUE(to_bool("fAlSe", output
));
373 EXPECT_FALSE(output
);
376 TEST(StrTest
, ToBoolInvalidInput
)
379 EXPECT_FALSE(to_bool("falsify", output
));
383 * The "quote" function take a string and returns that string quoted (i.e.,
384 * between double-quotes) if the string contains a space.
386 TEST(StrTest
, QuoteStringNoSpace
)
388 EXPECT_EQ("hello", quote("hello"));
391 TEST(StrTest
, QuoteStringWithSpace
)
393 EXPECT_EQ("\"hello world\"", quote("hello world"));
396 TEST(StrTest
, QuoteQuotedString
)
399 * At present, a quoted string can be quoted again.
401 EXPECT_EQ("\"\"hello world\"\"", quote("\"hello world\""));
404 TEST(StrTest
, QuoteStringWithTab
)
407 * The "quote" function only works with standard space, not any
410 EXPECT_EQ("hello\tworld", quote("hello\tworld"));
414 * str.hh has three implementations of "startswith"; a function that takes
415 * string and a prefix and returns true if the string starts with the prefix.
416 * One implementation takes two strings, another takes two char*, and the
417 * third takes a string and a char* as a prefix.
419 TEST(StrTest
, StartswithDoubleStringDoesStartWith
)
421 std::string s
= "Hello, how are you?";
422 std::string prefix
= "Hello";
423 EXPECT_TRUE(startswith(s
, prefix
));
426 TEST(StrTest
, StartswithDoubleStringDoesNotStartWith
)
428 std::string s
= "Hello, how are you?";
429 std::string prefix
= "ello";
430 EXPECT_FALSE(startswith(s
, prefix
));
433 TEST(StrTest
, StartswithDoubleCharArrayDoesStartWith
)
435 const char* s
= "abcdefg";
436 const char* prefix
= "ab";
437 EXPECT_TRUE(startswith(s
, prefix
));
440 TEST(StrTest
, StartswithDoubleCharArrayDoesNotStartWith
)
442 const char* s
= " abcdefg";
443 const char* prefix
= "a";
444 EXPECT_FALSE(startswith(s
, prefix
));
447 TEST(StrTest
, StartswithStringCharArrayDoesStartWith
)
449 std::string s
= "foobarr";
450 const char* prefix
= "f";
451 EXPECT_TRUE(startswith(s
, prefix
));
454 TEST(StrTest
, StartswithStringCharArrayDoesNotStartWith
)
456 std::string s
= "foobarr";
457 const char* prefix
= "barr";
458 EXPECT_FALSE(startswith(s
, prefix
));