gem5  v20.1.0.0
str.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 The Regents of the University of California
3  * All rights reserved
4  *
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.
13  *
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.
24  *
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.
36  */
37 
38 #include <gtest/gtest.h>
39 
40 #include <cstdint>
41 
42 #include "base/str.hh"
43 
44 /*
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
47  * behavior.
48  */
49 TEST(StrTest, EatLeadWhite)
50 {
51  std::string val = " hello there ";
53  EXPECT_EQ("hello there ", val);
54 }
55 
56 TEST(StrTest, EatLeadWhiteNoLeadingWhitespace)
57 {
58  std::string val = "hello there ";
60  EXPECT_EQ("hello there ", val);
61 }
62 
63 TEST(StrTest, EatEndWhite)
64 {
65  std::string val = " hello there ";
67  EXPECT_EQ(" hello there", val);
68 }
69 
70 TEST(StrTest, EatEndWhiteNoTrailingWhitespace)
71 {
72  std::string val = " hello there";
74  EXPECT_EQ(" hello there", val);
75 }
76 
77 TEST(StrTest, EatWhite)
78 {
79  std::string val = " hello there ";
80  eat_white(val);
81  EXPECT_EQ("hello there", val);
82 }
83 
84 TEST(StrTest, EatWhiteNoWhitespace)
85 {
86  std::string val = "hello there";
88  EXPECT_EQ("hello there", val);
89 }
90 
91 /*
92  * This tests checks that str.cc's "to_lower" function converts a string to
93  * lowercase.
94  */
95 TEST(StrTest, ToLower)
96 {
97  std::string val = "gOoDbYe FOO@barr!";
98  EXPECT_EQ("goodbye foo@barr!", to_lower(val));
99 }
100 
101 /*
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.
109  */
110 TEST(StrTest, SplitFirst)
111 {
112  std::string val = "abcdefg abcdefg";
113  std::string lhs;
114  std::string rhs;
115 
116  split_first(val , lhs, rhs, 'f');
117  EXPECT_EQ("abcdefg abcdefg", val);
118  EXPECT_EQ("abcde", lhs);
119  EXPECT_EQ("g abcdefg", rhs);
120 }
121 
122 TEST(StrTest, SplitFirstNoChar)
123 {
124  std::string val = "abcdefg abcdefg";
125  std::string lhs;
126  std::string rhs;
127 
128  split_first(val , lhs, rhs, 'h');
129  EXPECT_EQ("abcdefg abcdefg", val);
130  EXPECT_EQ("abcdefg abcdefg", lhs);
131  EXPECT_EQ("", rhs);
132 }
133 
134 TEST(StrTest, SplitFirstOnFirstChar)
135 {
136  std::string val = "abcdefg abcdefg";
137  std::string lhs;
138  std::string rhs;
139 
140  split_first(val , lhs, rhs, 'a');
141  EXPECT_EQ("abcdefg abcdefg", val);
142  EXPECT_EQ("", lhs);
143  EXPECT_EQ("bcdefg abcdefg", rhs);
144 }
145 
146 TEST(StrTest, SplitLast)
147 {
148  std::string val = "abcdefg abcdefg";
149  std::string lhs;
150  std::string rhs;
151 
152  split_last(val , lhs, rhs, 'f');
153  EXPECT_EQ("abcdefg abcdefg", val);
154  EXPECT_EQ("abcdefg abcde", lhs);
155  EXPECT_EQ("g", rhs);
156 }
157 
158 TEST(StrTest, SplitLastNoChar)
159 {
160  std::string val = "abcdefg abcdefg";
161  std::string lhs;
162  std::string rhs;
163 
164  split_last(val , lhs, rhs, 'h');
165  EXPECT_EQ("abcdefg abcdefg", val);
166  EXPECT_EQ("abcdefg abcdefg", lhs);
167  EXPECT_EQ("", rhs);
168 }
169 
170 TEST(StrTest, SplitLastOnLastChar)
171 {
172  std::string val = "abcdefg abcdefg";
173  std::string lhs;
174  std::string rhs;
175 
176  split_last(val , lhs, rhs, 'g');
177  EXPECT_EQ("abcdefg abcdefg", val);
178  EXPECT_EQ("abcdefg abcdef", lhs);
179  EXPECT_EQ("", rhs);
180 }
181 
182 
183 /*
184  * str.cc's "tokenize" function splits a string into its constituent tokens.
185  * It splits based on an input character.
186  */
187 TEST(StrTest, TokenizeOnSpace)
188 {
189  /*
190  * val has a double space between each token with trailing and leading
191  * whitespace.
192  */
193  std::string val = " Hello, this is a sentence. ";
195 
196  /*
197  * By default 'ign' is true. This means empty tokens are not included in
198  * the output list.
199  */
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]);
208 }
209 
210 TEST(StrTest, TokenizeOnSpaceIgnFalse)
211 {
212  /*
213  * val has a double space between each token with trailing and leading
214  * whitespace.
215  */
216  std::string val = " Hello, this is a sentence. ";
218 
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]);
233 }
234 
235 TEST(StrTest, TokenizedTokenDoesNotExist)
236 {
237  std::string val = "abcdefg";
239 
240  tokenize(tokens, val, 'h');
241  EXPECT_EQ("abcdefg", val);
242  EXPECT_EQ(1, tokens.size());
243  EXPECT_EQ("abcdefg", tokens[0]);
244 }
245 
246 /*
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.
250  */
251 TEST(StrTest, ToNumber8BitInt)
252 {
253  int8_t output;
254  std::string input = "-128";
255  EXPECT_TRUE(to_number(input, output));
256  EXPECT_EQ(-128, output);
257 }
258 
259 TEST(StrTest, ToNumber8BitIntStringOutOfRange)
260 {
261  int8_t output;
262  std::string input = "-129";
263  EXPECT_FALSE(to_number(input, output));
264 }
265 
266 TEST(StrTest, ToNumber8BitIntInvalidString)
267 {
268  int8_t output;
269  std::string input = "onetwoeight";
270  EXPECT_FALSE(to_number(input, output));
271 }
272 
273 TEST(StrTest, ToNumberUnsigned8BitInt)
274 {
275  uint8_t output;
276  std::string input = "255";
277  EXPECT_TRUE(to_number(input, output));
278  EXPECT_EQ(255, output);
279 }
280 
281 TEST(StrTest, ToNumberUnsigned8BitIntNegative)
282 {
283  uint8_t output;
284  std::string input = "-1";
285  EXPECT_FALSE(to_number(input, output));
286 }
287 
288 TEST(StrTest, ToNumber64BitInt)
289 {
290  int64_t output;
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);
295 }
296 
297 TEST(StrTest, ToNumber64BitIntInvalidString)
298 {
299  int64_t output;
300  std::string input = " ";
301  EXPECT_FALSE(to_number(input, output));
302 }
303 
304 TEST(StrTest, ToNumberFloat)
305 {
306  float output;
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);
311 }
312 
313 TEST(StrTest, ToNumberFloatIntegerString)
314 {
315  float output;
316  std::string input = "10";
317  float expected_output = 10.0;
318  EXPECT_TRUE(to_number(input, output));
319  EXPECT_EQ(expected_output, output);
320 }
321 
322 TEST(StrTest, ToNumberFloatNegative)
323 {
324  float output;
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);
329 }
330 
331 TEST(StrTest, ToNumberDouble)
332 {
333  double output;
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);
338 }
339 
340 TEST(StrTest, ToNumberDoubleIntegerString)
341 {
342  double output;
343  std::string input = "12345";
344  double expected_output = 12345.0;
345  EXPECT_TRUE(to_number(input, output));
346  EXPECT_EQ(expected_output, output);
347 }
348 
349 TEST(StrTest, ToNumberDoubleNegative)
350 {
351  double output;
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);
356 }
357 
358 /*
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.
362  */
363 TEST(StrTest, ToBoolTrue)
364 {
365  bool output = false;
366  EXPECT_TRUE(to_bool("TrUe", output));
368 }
369 
370 TEST(StrTest, ToBoolFalse){
371  bool output = true;
372  EXPECT_TRUE(to_bool("fAlSe", output));
374 }
375 
376 TEST(StrTest, ToBoolInvalidInput)
377 {
378  bool output;
379  EXPECT_FALSE(to_bool("falsify", output));
380 }
381 
382 /*
383  * The "quote" function take a string and returns that string quoted (i.e.,
384  * between double-quotes) if the string contains a space.
385  */
386 TEST(StrTest, QuoteStringNoSpace)
387 {
388  EXPECT_EQ("hello", quote("hello"));
389 }
390 
391 TEST(StrTest, QuoteStringWithSpace)
392 {
393  EXPECT_EQ("\"hello world\"", quote("hello world"));
394 }
395 
396 TEST(StrTest, QuoteQuotedString)
397 {
398  /*
399  * At present, a quoted string can be quoted again.
400  */
401  EXPECT_EQ("\"\"hello world\"\"", quote("\"hello world\""));
402 }
403 
404 TEST(StrTest, QuoteStringWithTab)
405 {
406  /*
407  * The "quote" function only works with standard space, not any
408  * whitepsace.
409  */
410  EXPECT_EQ("hello\tworld", quote("hello\tworld"));
411 }
412 
413 /*
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.
418  */
419 TEST(StrTest, StartswithDoubleStringDoesStartWith)
420 {
421  std::string s = "Hello, how are you?";
422  std::string prefix = "Hello";
423  EXPECT_TRUE(startswith(s, prefix));
424 }
425 
426 TEST(StrTest, StartswithDoubleStringDoesNotStartWith)
427 {
428  std::string s = "Hello, how are you?";
429  std::string prefix = "ello";
430  EXPECT_FALSE(startswith(s, prefix));
431 }
432 
433 TEST(StrTest, StartswithDoubleCharArrayDoesStartWith)
434 {
435  const char* s = "abcdefg";
436  const char* prefix = "ab";
437  EXPECT_TRUE(startswith(s, prefix));
438 }
439 
440 TEST(StrTest, StartswithDoubleCharArrayDoesNotStartWith)
441 {
442  const char* s = " abcdefg";
443  const char* prefix = "a";
444  EXPECT_FALSE(startswith(s, prefix));
445 }
446 
447 TEST(StrTest, StartswithStringCharArrayDoesStartWith)
448 {
449  std::string s = "foobarr";
450  const char* prefix = "f";
451  EXPECT_TRUE(startswith(s, prefix));
452 }
453 
454 TEST(StrTest, StartswithStringCharArrayDoesNotStartWith)
455 {
456  std::string s = "foobarr";
457  const char* prefix = "barr";
458  EXPECT_FALSE(startswith(s, prefix));
459 }
tokenize
void tokenize(vector< string > &v, const string &s, char token, bool ignore)
Definition: str.cc:67
output
static void output(const char *filename)
Definition: debug.cc:60
to_bool
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:186
eat_end_white
void eat_end_white(std::string &s)
Definition: str.hh:56
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
std::vector< std::string >
EXPECT_EQ
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:110
TEST
TEST(StrTest, EatLeadWhite)
Definition: str.test.cc:49
str.hh
EXPECT_FALSE
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:106
to_number
bool to_number(const std::string &value, VecPredRegContainer< NumBits, Packed > &p)
Helper functions used for serialization/de-serialization.
Definition: vec_pred_reg.hh:379
EXPECT_TRUE
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:103
split_last
bool split_last(const string &s, string &lhs, string &rhs, char c)
Definition: str.cc:52
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
eat_white
void eat_white(std::string &s)
Definition: str.hh:64
quote
std::string quote(const std::string &s)
Definition: str.hh:203
to_lower
std::string to_lower(const std::string &s)
Definition: str.hh:71
startswith
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
Definition: str.hh:224
eat_lead_white
void eat_lead_white(std::string &s)
Definition: str.hh:46
split_first
bool split_first(const string &s, string &lhs, string &rhs, char c)
Definition: str.cc:37
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17