gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Bobby R. Bruce
38  */
39 
40 #include <gtest/gtest.h>
41 
42 #include <cstdint>
43 
44 #include "base/str.hh"
45 
46 /*
47  * str.cc has "eat_lead_white", "eat_end_white", and "eat_white" fucntions to
48  * remove leading and trailing whitespace. The following tests verify this
49  * behavior.
50  */
51 TEST(StrTest, EatLeadWhite)
52 {
53  std::string val = " hello there ";
54  eat_lead_white(val);
55  EXPECT_EQ("hello there ", val);
56 }
57 
58 TEST(StrTest, EatLeadWhiteNoLeadingWhitespace)
59 {
60  std::string val = "hello there ";
61  eat_lead_white(val);
62  EXPECT_EQ("hello there ", val);
63 }
64 
65 TEST(StrTest, EatEndWhite)
66 {
67  std::string val = " hello there ";
68  eat_end_white(val);
69  EXPECT_EQ(" hello there", val);
70 }
71 
72 TEST(StrTest, EatEndWhiteNoTrailingWhitespace)
73 {
74  std::string val = " hello there";
75  eat_end_white(val);
76  EXPECT_EQ(" hello there", val);
77 }
78 
79 TEST(StrTest, EatWhite)
80 {
81  std::string val = " hello there ";
82  eat_white(val);
83  EXPECT_EQ("hello there", val);
84 }
85 
86 TEST(StrTest, EatWhiteNoWhitespace)
87 {
88  std::string val = "hello there";
89  eat_lead_white(val);
90  EXPECT_EQ("hello there", val);
91 }
92 
93 /*
94  * This tests checks that str.cc's "to_lower" function converts a string to
95  * lowercase.
96  */
97 TEST(StrTest, ToLower)
98 {
99  std::string val = "gOoDbYe FOO@barr!";
100  EXPECT_EQ("goodbye foo@barr!", to_lower(val));
101 }
102 
103 /*
104  * str.cc's "split_first" and "split_last" fucntions split a string on a
105  * character into two parts. "split_first" splits on the first instance of
106  * this character and "split_last" splits on the last instance of this
107  * character. The character itself is not included in either of the output
108  * right-hand side and left-hand side strings. If the character cannot be
109  * found in the string then the left-hand side string is equal to the input
110  * string and the right-hand side string is empty.
111  */
112 TEST(StrTest, SplitFirst)
113 {
114  std::string val = "abcdefg abcdefg";
115  std::string lhs;
116  std::string rhs;
117 
118  split_first(val , lhs, rhs, 'f');
119  EXPECT_EQ("abcdefg abcdefg", val);
120  EXPECT_EQ("abcde", lhs);
121  EXPECT_EQ("g abcdefg", rhs);
122 }
123 
124 TEST(StrTest, SplitFirstNoChar)
125 {
126  std::string val = "abcdefg abcdefg";
127  std::string lhs;
128  std::string rhs;
129 
130  split_first(val , lhs, rhs, 'h');
131  EXPECT_EQ("abcdefg abcdefg", val);
132  EXPECT_EQ("abcdefg abcdefg", lhs);
133  EXPECT_EQ("", rhs);
134 }
135 
136 TEST(StrTest, SplitFirstOnFirstChar)
137 {
138  std::string val = "abcdefg abcdefg";
139  std::string lhs;
140  std::string rhs;
141 
142  split_first(val , lhs, rhs, 'a');
143  EXPECT_EQ("abcdefg abcdefg", val);
144  EXPECT_EQ("", lhs);
145  EXPECT_EQ("bcdefg abcdefg", rhs);
146 }
147 
148 TEST(StrTest, SplitLast)
149 {
150  std::string val = "abcdefg abcdefg";
151  std::string lhs;
152  std::string rhs;
153 
154  split_last(val , lhs, rhs, 'f');
155  EXPECT_EQ("abcdefg abcdefg", val);
156  EXPECT_EQ("abcdefg abcde", lhs);
157  EXPECT_EQ("g", rhs);
158 }
159 
160 TEST(StrTest, SplitLastNoChar)
161 {
162  std::string val = "abcdefg abcdefg";
163  std::string lhs;
164  std::string rhs;
165 
166  split_last(val , lhs, rhs, 'h');
167  EXPECT_EQ("abcdefg abcdefg", val);
168  EXPECT_EQ("abcdefg abcdefg", lhs);
169  EXPECT_EQ("", rhs);
170 }
171 
172 TEST(StrTest, SplitLastOnLastChar)
173 {
174  std::string val = "abcdefg abcdefg";
175  std::string lhs;
176  std::string rhs;
177 
178  split_last(val , lhs, rhs, 'g');
179  EXPECT_EQ("abcdefg abcdefg", val);
180  EXPECT_EQ("abcdefg abcdef", lhs);
181  EXPECT_EQ("", rhs);
182 }
183 
184 
185 /*
186  * str.cc's "tokenize" function splits a string into its constituent tokens.
187  * It splits based on an input character.
188  */
189 TEST(StrTest, TokenizeOnSpace)
190 {
191  /*
192  * val has a double space between each token with trailing and leading
193  * whitespace.
194  */
195  std::string val = " Hello, this is a sentence. ";
197 
198  /*
199  * By default 'ign' is true. This means empty tokens are not included in
200  * the output list.
201  */
202  tokenize(tokens, val, ' ');
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]);
210 }
211 
212 TEST(StrTest, TokenizeOnSpaceIgnFalse)
213 {
214  /*
215  * val has a double space between each token with trailing and leading
216  * whitespace.
217  */
218  std::string val = " Hello, this is a sentence. ";
220 
221  tokenize(tokens, val, ' ', false);
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]);
235 }
236 
237 TEST(StrTest, TokenizedTokenDoesNotExist)
238 {
239  std::string val = "abcdefg";
241 
242  tokenize(tokens, val, 'h');
243  EXPECT_EQ("abcdefg", val);
244  EXPECT_EQ(1, tokens.size());
245  EXPECT_EQ("abcdefg", tokens[0]);
246 }
247 
248 /*
249  * str.cc's "to_number" function converts a string to a number. The function
250  * will return false if this is not possible either because the string
251  * represents a number out-of-range, or because the string cannot be parsed.
252  */
253 TEST(StrTest, ToNumber8BitInt)
254 {
255  int8_t output;
256  std::string input = "-128";
257  EXPECT_TRUE(to_number(input, output));
258  EXPECT_EQ(-128, output);
259 }
260 
261 TEST(StrTest, ToNumber8BitIntStringOutOfRange)
262 {
263  int8_t output;
264  std::string input = "-129";
265  EXPECT_FALSE(to_number(input, output));
266 }
267 
268 TEST(StrTest, ToNumber8BitIntInvalidString)
269 {
270  int8_t output;
271  std::string input = "onetwoeight";
272  EXPECT_FALSE(to_number(input, output));
273 }
274 
275 TEST(StrTest, ToNumberUnsigned8BitInt)
276 {
277  uint8_t output;
278  std::string input = "255";
279  EXPECT_TRUE(to_number(input, output));
280  EXPECT_EQ(255, output);
281 }
282 
283 TEST(StrTest, ToNumberUnsigned8BitIntNegative)
284 {
285  uint8_t output;
286  std::string input = "-1";
287  EXPECT_FALSE(to_number(input, output));
288 }
289 
290 TEST(StrTest, ToNumber64BitInt)
291 {
292  int64_t output;
293  int64_t input_number = 0xFFFFFFFFFFFFFFFF;
294  std::string input = std::to_string(input_number);
295  EXPECT_TRUE(to_number(input, output));
296  EXPECT_EQ(input_number, output);
297 }
298 
299 TEST(StrTest, ToNumber64BitIntInvalidString)
300 {
301  int64_t output;
302  std::string input = " ";
303  EXPECT_FALSE(to_number(input, output));
304 }
305 
306 TEST(StrTest, ToNumberFloat)
307 {
308  float output;
309  std::string input = "0.1";
310  float expected_output = 0.1;
311  EXPECT_TRUE(to_number(input, output));
312  EXPECT_EQ(expected_output, output);
313 }
314 
315 TEST(StrTest, ToNumberFloatIntegerString)
316 {
317  float output;
318  std::string input = "10";
319  float expected_output = 10.0;
320  EXPECT_TRUE(to_number(input, output));
321  EXPECT_EQ(expected_output, output);
322 }
323 
324 TEST(StrTest, ToNumberFloatNegative)
325 {
326  float output;
327  std::string input = "-0.1";
328  float expected_output = -0.1;
329  EXPECT_TRUE(to_number(input, output));
330  EXPECT_EQ(expected_output, output);
331 }
332 
333 TEST(StrTest, ToNumberDouble)
334 {
335  double output;
336  std::string input = "0.0001";
337  double expected_output = 0.0001;
338  EXPECT_TRUE(to_number(input, output));
339  EXPECT_EQ(expected_output, output);
340 }
341 
342 TEST(StrTest, ToNumberDoubleIntegerString)
343 {
344  double output;
345  std::string input = "12345";
346  double expected_output = 12345.0;
347  EXPECT_TRUE(to_number(input, output));
348  EXPECT_EQ(expected_output, output);
349 }
350 
351 TEST(StrTest, ToNumberDoubleNegative)
352 {
353  double output;
354  std::string input = "-1.2345";
355  double expected_output = -1.2345;
356  EXPECT_TRUE(to_number(input, output));
357  EXPECT_EQ(expected_output, output);
358 }
359 
360 /*
361  * The "to_bool" function takes a string, "true" or "false"
362  * (case-insenstive), and sets the second argument to the bool equivilent.
363  * The function will return false if it cannot parse the string.
364  */
365 TEST(StrTest, ToBoolTrue)
366 {
367  bool output = false;
368  EXPECT_TRUE(to_bool("TrUe", output));
369  EXPECT_TRUE(output);
370 }
371 
372 TEST(StrTest, ToBoolFalse){
373  bool output = true;
374  EXPECT_TRUE(to_bool("fAlSe", output));
375  EXPECT_FALSE(output);
376 }
377 
378 TEST(StrTest, ToBoolInvalidInput)
379 {
380  bool output;
381  EXPECT_FALSE(to_bool("falsify", output));
382 }
383 
384 /*
385  * The "quote" function take a string and returns that string quoted (i.e.,
386  * between double-quotes) if the string contains a space.
387  */
388 TEST(StrTest, QuoteStringNoSpace)
389 {
390  EXPECT_EQ("hello", quote("hello"));
391 }
392 
393 TEST(StrTest, QuoteStringWithSpace)
394 {
395  EXPECT_EQ("\"hello world\"", quote("hello world"));
396 }
397 
398 TEST(StrTest, QuoteQuotedString)
399 {
400  /*
401  * At present, a quoted string can be quoted again.
402  */
403  EXPECT_EQ("\"\"hello world\"\"", quote("\"hello world\""));
404 }
405 
406 TEST(StrTest, QuoteStringWithTab)
407 {
408  /*
409  * The "quote" function only works with standard space, not any
410  * whitepsace.
411  */
412  EXPECT_EQ("hello\tworld", quote("hello\tworld"));
413 }
414 
415 /*
416  * str.hh has three implementations of "startswith"; a function that takes
417  * string and a prefix and returns true if the string starts with the prefix.
418  * One implementation takes two strings, another takes two char*, and the
419  * third takes a string and a char* as a prefix.
420  */
421 TEST(StrTest, StartswithDoubleStringDoesStartWith)
422 {
423  std::string s = "Hello, how are you?";
424  std::string prefix = "Hello";
425  EXPECT_TRUE(startswith(s, prefix));
426 }
427 
428 TEST(StrTest, StartswithDoubleStringDoesNotStartWith)
429 {
430  std::string s = "Hello, how are you?";
431  std::string prefix = "ello";
432  EXPECT_FALSE(startswith(s, prefix));
433 }
434 
435 TEST(StrTest, StartswithDoubleCharArrayDoesStartWith)
436 {
437  const char* s = "abcdefg";
438  const char* prefix = "ab";
439  EXPECT_TRUE(startswith(s, prefix));
440 }
441 
442 TEST(StrTest, StartswithDoubleCharArrayDoesNotStartWith)
443 {
444  const char* s = " abcdefg";
445  const char* prefix = "a";
446  EXPECT_FALSE(startswith(s, prefix));
447 }
448 
449 TEST(StrTest, StartswithStringCharArrayDoesStartWith)
450 {
451  std::string s = "foobarr";
452  const char* prefix = "f";
453  EXPECT_TRUE(startswith(s, prefix));
454 }
455 
456 TEST(StrTest, StartswithStringCharArrayDoesNotStartWith)
457 {
458  std::string s = "foobarr";
459  const char* prefix = "barr";
460  EXPECT_FALSE(startswith(s, prefix));
461 }
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:189
static void output(const char *filename)
Definition: debug.cc:63
void eat_end_white(std::string &s)
Definition: str.hh:59
bool split_last(const string &s, string &lhs, string &rhs, char c)
Definition: str.cc:54
Bitfield< 63 > val
Definition: misc.hh:771
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:105
TEST(StrTest, EatLeadWhite)
Definition: str.test.cc:51
Bitfield< 4 > s
bool to_number(const std::string &value, VecPredRegContainer< NumBits, Packed > &p)
Helper functions used for serialization/de-serialization.
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:108
bool startswith(const char *s, const char *prefix)
Return true if &#39;s&#39; starts with the prefix string &#39;prefix&#39;.
Definition: str.hh:227
std::string quote(const std::string &s)
Definition: str.hh:206
void eat_white(std::string &s)
Definition: str.hh:67
bool split_first(const string &s, string &lhs, string &rhs, char c)
Definition: str.cc:39
std::string to_lower(const std::string &s)
Definition: str.hh:74
void tokenize(vector< string > &v, const string &s, char token, bool ignore)
Definition: str.cc:69
void eat_lead_white(std::string &s)
Definition: str.hh:49
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:112
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13