38#include <gtest/gtest.h>
53 std::string
val =
" hello there ";
55 EXPECT_EQ(
"hello there ",
val);
58TEST(StrTest, EatLeadWhiteNoLeadingWhitespace)
60 std::string
val =
"hello there ";
62 EXPECT_EQ(
"hello there ",
val);
67 std::string
val =
" hello there ";
69 EXPECT_EQ(
" hello there",
val);
72TEST(StrTest, EatEndWhiteNoTrailingWhitespace)
74 std::string
val =
" hello there";
76 EXPECT_EQ(
" hello there",
val);
81 std::string
val =
" hello there ";
83 EXPECT_EQ(
"hello there",
val);
86TEST(StrTest, EatWhiteNoWhitespace)
88 std::string
val =
"hello there";
90 EXPECT_EQ(
"hello there",
val);
99 std::string
val =
"gOoDbYe FOO@barr!";
114 std::string
val =
"abcdefg abcdefg";
119 EXPECT_EQ(
"abcdefg abcdefg",
val);
120 EXPECT_EQ(
"abcde", lhs);
121 EXPECT_EQ(
"g abcdefg", rhs);
126 std::string
val =
"abcdefg abcdefg";
131 EXPECT_EQ(
"abcdefg abcdefg",
val);
132 EXPECT_EQ(
"abcdefg abcdefg", lhs);
136TEST(StrTest, SplitFirstOnFirstChar)
138 std::string
val =
"abcdefg abcdefg";
143 EXPECT_EQ(
"abcdefg abcdefg",
val);
145 EXPECT_EQ(
"bcdefg abcdefg", rhs);
150 std::string
val =
"abcdefg abcdefg";
155 EXPECT_EQ(
"abcdefg abcdefg",
val);
156 EXPECT_EQ(
"abcdefg abcde", lhs);
162 std::string
val =
"abcdefg abcdefg";
167 EXPECT_EQ(
"abcdefg abcdefg",
val);
168 EXPECT_EQ(
"abcdefg abcdefg", lhs);
172TEST(StrTest, SplitLastOnLastChar)
174 std::string
val =
"abcdefg abcdefg";
179 EXPECT_EQ(
"abcdefg abcdefg",
val);
180 EXPECT_EQ(
"abcdefg abcdef", lhs);
195 std::string
val =
" Hello, this is a sentence. ";
203 EXPECT_EQ(
" Hello, this is a sentence. ",
val);
204 EXPECT_EQ(5, tokens.size());
205 EXPECT_EQ(
"Hello,", tokens[0]);
206 EXPECT_EQ(
"this", tokens[1]);
207 EXPECT_EQ(
"is", tokens[2]);
208 EXPECT_EQ(
"a", tokens[3]);
209 EXPECT_EQ(
"sentence.", tokens[4]);
212TEST(StrTest, TokenizeOnSpaceIgnFalse)
218 std::string
val =
" Hello, this is a sentence. ";
222 EXPECT_EQ(
" Hello, this is a sentence. ",
val);
223 EXPECT_EQ(11, tokens.size());
224 EXPECT_EQ(
"", tokens[0]);
225 EXPECT_EQ(
"Hello,", tokens[1]);
226 EXPECT_EQ(
"", tokens[2]);
227 EXPECT_EQ(
"this", tokens[3]);
228 EXPECT_EQ(
"", tokens[4]);
229 EXPECT_EQ(
"is", tokens[5]);
230 EXPECT_EQ(
"", tokens[6]);
231 EXPECT_EQ(
"a", tokens[7]);
232 EXPECT_EQ(
"", tokens[8]);
233 EXPECT_EQ(
"sentence.", tokens[9]);
234 EXPECT_EQ(
"", tokens[10]);
237TEST(StrTest, TokenizedTokenDoesNotExist)
239 std::string
val =
"abcdefg";
243 EXPECT_EQ(
"abcdefg",
val);
244 EXPECT_EQ(1, tokens.size());
245 EXPECT_EQ(
"abcdefg", tokens[0]);
256 std::string input =
"-128";
261TEST(StrTest, ToNumber8BitIntStringOutOfRange)
264 std::string input =
"-129";
268TEST(StrTest, ToNumber8BitIntInvalidString)
271 std::string input =
"onetwoeight";
275TEST(StrTest, ToNumberUnsigned8BitInt)
278 std::string input =
"255";
283TEST(StrTest, ToNumberUnsigned8BitIntNegative)
286 std::string input =
"-1";
291TEST(StrTest, ToNumberUnsigned8BitIntRoundDown)
294 std::string input_1 =
"2.99";
298 std::string input_2 =
"3.99";
307TEST(StrTest, ToNumber8BitUnsignedLimit)
310 std::string input =
"255.99";
319TEST(StrTest, ToNumber8BitUnsignedOutOfRange)
322 std::string input =
"256.99";
327TEST(StrTest, ToNumberUnsignedScientific)
330 std::string input =
"8.234e+08";
335TEST(StrTest, ToNumberIntScientificNegative)
338 std::string input =
"-8.234e+08";
345 int64_t input_number = 0xFFFFFFFFFFFFFFFF;
346 std::string input = std::to_string(input_number);
348 EXPECT_EQ(input_number,
output);
351TEST(StrTest, ToNumber64BitIntInvalidString)
354 std::string input =
" ";
365 std::string input =
"2";
371TEST(StrTest, DISABLED_ToNumberEnumInvalid)
378 std::string input =
"3";
385 std::string input =
"0.1";
386 float expected_output = 0.1;
388 EXPECT_EQ(expected_output,
output);
391TEST(StrTest, ToNumberFloatIntegerString)
394 std::string input =
"10";
395 float expected_output = 10.0;
397 EXPECT_EQ(expected_output,
output);
400TEST(StrTest, ToNumberFloatNegative)
403 std::string input =
"-0.1";
404 float expected_output = -0.1;
406 EXPECT_EQ(expected_output,
output);
412 std::string input =
"0.0001";
413 double expected_output = 0.0001;
415 EXPECT_EQ(expected_output,
output);
418TEST(StrTest, ToNumberDoubleIntegerString)
421 std::string input =
"12345";
422 double expected_output = 12345.0;
424 EXPECT_EQ(expected_output,
output);
427TEST(StrTest, ToNumberDoubleNegative)
430 std::string input =
"-1.2345";
431 double expected_output = -1.2345;
433 EXPECT_EQ(expected_output,
output);
437TEST(StrTest, ToNumberScientific)
440 std::string input =
"8.234e+08";
441 double expected_output = 823400000;
443 EXPECT_EQ(expected_output,
output);
464TEST(StrTest, ToBoolInvalidInput)
474TEST(StrTest, QuoteStringNoSpace)
476 EXPECT_EQ(
"hello",
quote(
"hello"));
479TEST(StrTest, QuoteStringWithSpace)
481 EXPECT_EQ(
"\"hello world\"",
quote(
"hello world"));
484TEST(StrTest, QuoteQuotedString)
489 EXPECT_EQ(
"\"\"hello world\"\"",
quote(
"\"hello world\""));
492TEST(StrTest, QuoteStringWithTab)
498 EXPECT_EQ(
"hello\tworld",
quote(
"hello\tworld"));
507TEST(StrTest, StartswithDoubleStringDoesStartWith)
509 std::string
s =
"Hello, how are you?";
510 std::string prefix =
"Hello";
514TEST(StrTest, StartswithDoubleStringDoesNotStartWith)
516 std::string
s =
"Hello, how are you?";
517 std::string prefix =
"ello";
521TEST(StrTest, StartswithDoubleCharArrayDoesStartWith)
523 const char*
s =
"abcdefg";
524 const char* prefix =
"ab";
528TEST(StrTest, StartswithDoubleCharArrayDoesNotStartWith)
530 const char*
s =
" abcdefg";
531 const char* prefix =
"a";
535TEST(StrTest, StartswithStringCharArrayDoesStartWith)
537 std::string
s =
"foobarr";
538 const char* prefix =
"f";
542TEST(StrTest, StartswithStringCharArrayDoesNotStartWith)
544 std::string
s =
"foobarr";
545 const char* prefix =
"barr";
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
std::string to_lower(const std::string &s)
bool to_number(const std::string &value, Pixel &retval)
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
std::string quote(const std::string &s)
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
static void output(const char *filename)
void eat_end_white(std::string &s)
void eat_white(std::string &s)
void eat_lead_white(std::string &s)
bool split_first(const std::string &s, std::string &lhs, std::string &rhs, char c)
TEST(StrTest, EatLeadWhite)