summaryrefslogtreecommitdiff
path: root/src/base/str.test.cc
diff options
context:
space:
mode:
authorBobby R. Bruce <bbruce@ucdavis.edu>2019-10-22 16:16:10 -0700
committerBobby R. Bruce <bbruce@ucdavis.edu>2019-10-24 16:25:09 +0000
commit69930afa9b63c25baab86ff5fbe632fc02ce5369 (patch)
treeb26df22c0621a2ab274a36a3ce332accdac1441d /src/base/str.test.cc
parentf60383d060ed82400526a47a1ba3182253e9f2f5 (diff)
downloadgem5-69930afa9b63c25baab86ff5fbe632fc02ce5369.tar.xz
tests: Added GTests for base/str.cc
Adding these tests supercedes the unittest/strnumtest.cc and unittest/tokentest.cc tests. They have thereby been removed. Function "to_number" in base/str.hh previously failed to cast negative float/double numbers. This was due to the use of std::numeric_limits<T>::min() instead of std::numeric_limits<T>::lowest() to determine whether a string-to-float/double conversion was "Out of range". Tests "StrTest.ToNumberFloatNegative" and "StrTest.ToNumberDoubleNegative" exposed this bug. It has been fixed. Methods "split_first" and "split_last" in base/str.hh have had their documentation updated to remove abiguity in their functionality. Change-Id: I16e0fe40d884e22dd010db4045857eb6e7f33d4a Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/22084 Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br> Maintainer: Jason Lowe-Power <jason@lowepower.com> Tested-by: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'src/base/str.test.cc')
-rw-r--r--src/base/str.test.cc459
1 files changed, 459 insertions, 0 deletions
diff --git a/src/base/str.test.cc b/src/base/str.test.cc
new file mode 100644
index 000000000..05af5d7f2
--- /dev/null
+++ b/src/base/str.test.cc
@@ -0,0 +1,459 @@
+/*
+ * Copyright (c) 2019 The Regents of the University of California
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder. You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Bobby R. Bruce
+ */
+
+#include <gtest/gtest.h>
+
+#include "base/str.hh"
+
+/*
+ * str.cc has "eat_lead_white", "eat_end_white", and "eat_white" fucntions to
+ * remove leading and trailing whitespace. The following tests verify this
+ * behavior.
+ */
+TEST(StrTest, EatLeadWhite)
+{
+ std::string val = " hello there ";
+ eat_lead_white(val);
+ EXPECT_EQ("hello there ", val);
+}
+
+TEST(StrTest, EatLeadWhiteNoLeadingWhitespace)
+{
+ std::string val = "hello there ";
+ eat_lead_white(val);
+ EXPECT_EQ("hello there ", val);
+}
+
+TEST(StrTest, EatEndWhite)
+{
+ std::string val = " hello there ";
+ eat_end_white(val);
+ EXPECT_EQ(" hello there", val);
+}
+
+TEST(StrTest, EatEndWhiteNoTrailingWhitespace)
+{
+ std::string val = " hello there";
+ eat_end_white(val);
+ EXPECT_EQ(" hello there", val);
+}
+
+TEST(StrTest, EatWhite)
+{
+ std::string val = " hello there ";
+ eat_white(val);
+ EXPECT_EQ("hello there", val);
+}
+
+TEST(StrTest, EatWhiteNoWhitespace)
+{
+ std::string val = "hello there";
+ eat_lead_white(val);
+ EXPECT_EQ("hello there", val);
+}
+
+/*
+ * This tests checks that str.cc's "to_lower" function converts a string to
+ * lowercase.
+ */
+TEST(StrTest, ToLower)
+{
+ std::string val = "gOoDbYe FOO@barr!";
+ to_lower("goodbye foo@bar!");
+}
+
+/*
+ * str.cc's "split_first" and "split_last" fucntions split a string on a
+ * character into two parts. "split_first" splits on the first instance of
+ * this character and "split_last" splits on the last instance of this
+ * character. The character itself is not included in either of the output
+ * right-hand side and left-hand side strings. If the character cannot be
+ * found in the string then the left-hand side string is equal to the input
+ * string and the right-hand side string is empty.
+ */
+TEST(StrTest, SplitFirst)
+{
+ std::string val = "abcdefg abcdefg";
+ std::string lhs;
+ std::string rhs;
+
+ split_first(val , lhs, rhs, 'f');
+ EXPECT_EQ("abcdefg abcdefg", val);
+ EXPECT_EQ("abcde", lhs);
+ EXPECT_EQ("g abcdefg", rhs);
+}
+
+TEST(StrTest, SplitFirstNoChar)
+{
+ std::string val = "abcdefg abcdefg";
+ std::string lhs;
+ std::string rhs;
+
+ split_first(val , lhs, rhs, 'h');
+ EXPECT_EQ("abcdefg abcdefg", val);
+ EXPECT_EQ("abcdefg abcdefg", lhs);
+ EXPECT_EQ("", rhs);
+}
+
+TEST(StrTest, SplitFirstOnFirstChar)
+{
+ std::string val = "abcdefg abcdefg";
+ std::string lhs;
+ std::string rhs;
+
+ split_first(val , lhs, rhs, 'a');
+ EXPECT_EQ("abcdefg abcdefg", val);
+ EXPECT_EQ("", lhs);
+ EXPECT_EQ("bcdefg abcdefg", rhs);
+}
+
+TEST(StrTest, SplitLast)
+{
+ std::string val = "abcdefg abcdefg";
+ std::string lhs;
+ std::string rhs;
+
+ split_last(val , lhs, rhs, 'f');
+ EXPECT_EQ("abcdefg abcdefg", val);
+ EXPECT_EQ("abcdefg abcde", lhs);
+ EXPECT_EQ("g", rhs);
+}
+
+TEST(StrTest, SplitLastNoChar)
+{
+ std::string val = "abcdefg abcdefg";
+ std::string lhs;
+ std::string rhs;
+
+ split_last(val , lhs, rhs, 'h');
+ EXPECT_EQ("abcdefg abcdefg", val);
+ EXPECT_EQ("abcdefg abcdefg", lhs);
+ EXPECT_EQ("", rhs);
+}
+
+TEST(StrTest, SplitLastOnLastChar)
+{
+ std::string val = "abcdefg abcdefg";
+ std::string lhs;
+ std::string rhs;
+
+ split_last(val , lhs, rhs, 'g');
+ EXPECT_EQ("abcdefg abcdefg", val);
+ EXPECT_EQ("abcdefg abcdef", lhs);
+ EXPECT_EQ("", rhs);
+}
+
+
+/*
+ * str.cc's "tokenize" function splits a string into its constituent tokens.
+ * It splits based on an input character.
+ */
+TEST(StrTest, TokenizeOnSpace)
+{
+ /*
+ * val has a double space between each token with trailing and leading
+ * whitespace.
+ */
+ std::string val = " Hello, this is a sentence. ";
+ std::vector<std::string> tokens;
+
+ /*
+ * By default 'ign' is true. This means empty tokens are not included in
+ * the output list.
+ */
+ tokenize(tokens, val, ' ');
+ EXPECT_EQ(" Hello, this is a sentence. ", val);
+ EXPECT_EQ(5, tokens.size());
+ EXPECT_EQ("Hello,", tokens[0]);
+ EXPECT_EQ("this", tokens[1]);
+ EXPECT_EQ("is", tokens[2]);
+ EXPECT_EQ("a", tokens[3]);
+ EXPECT_EQ("sentence.", tokens[4]);
+}
+
+TEST(StrTest, TokenizeOnSpaceIgnFalse)
+{
+ /*
+ * val has a double space between each token with trailing and leading
+ * whitespace.
+ */
+ std::string val = " Hello, this is a sentence. ";
+ std::vector<std::string> tokens;
+
+ tokenize(tokens, val, ' ', false);
+ EXPECT_EQ(" Hello, this is a sentence. ", val);
+ EXPECT_EQ(11, tokens.size());
+ EXPECT_EQ("", tokens[0]);
+ EXPECT_EQ("Hello,", tokens[1]);
+ EXPECT_EQ("", tokens[2]);
+ EXPECT_EQ("this", tokens[3]);
+ EXPECT_EQ("", tokens[4]);
+ EXPECT_EQ("is", tokens[5]);
+ EXPECT_EQ("", tokens[6]);
+ EXPECT_EQ("a", tokens[7]);
+ EXPECT_EQ("", tokens[8]);
+ EXPECT_EQ("sentence.", tokens[9]);
+ EXPECT_EQ("", tokens[10]);
+}
+
+TEST(StrTest, TokenizedTokenDoesNotExist)
+{
+ std::string val = "abcdefg";
+ std::vector<std::string> tokens;
+
+ tokenize(tokens, val, 'h');
+ EXPECT_EQ("abcdefg", val);
+ EXPECT_EQ(1, tokens.size());
+ EXPECT_EQ("abcdefg", tokens[0]);
+}
+
+/*
+ * str.cc's "to_number" function converts a string to a number. The function
+ * will return false if this is not possible either because the string
+ * represents a number out-of-range, or because the string cannot be parsed.
+ */
+TEST(StrTest, ToNumber8BitInt)
+{
+ int8_t output;
+ std::string input = "-128";
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(-128, output);
+}
+
+TEST(StrTest, ToNumber8BitIntStringOutOfRange)
+{
+ int8_t output;
+ std::string input = "-129";
+ EXPECT_FALSE(to_number(input, output));
+}
+
+TEST(StrTest, ToNumber8BitIntInvalidString)
+{
+ int8_t output;
+ std::string input = "onetwoeight";
+ EXPECT_FALSE(to_number(input, output));
+}
+
+TEST(StrTest, ToNumberUnsigned8BitInt)
+{
+ uint8_t output;
+ std::string input = "255";
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(255, output);
+}
+
+TEST(StrTest, ToNumberUnsigned8BitIntNegative)
+{
+ uint8_t output;
+ std::string input = "-1";
+ EXPECT_FALSE(to_number(input, output));
+}
+
+TEST(StrTest, ToNumber64BitInt)
+{
+ int64_t output;
+ int64_t input_number = 0xFFFFFFFFFFFFFFFF;
+ std::string input = std::to_string(input_number);
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(input_number, output);
+}
+
+TEST(StrTest, ToNumber64BitIntInvalidString)
+{
+ int64_t output;
+ std::string input = " ";
+ EXPECT_FALSE(to_number(input, output));
+}
+
+TEST(StrTest, ToNumberFloat)
+{
+ float output;
+ std::string input = "0.1";
+ float expected_output = 0.1;
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(expected_output, output);
+}
+
+TEST(StrTest, ToNumberFloatIntegerString)
+{
+ float output;
+ std::string input = "10";
+ float expected_output = 10.0;
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(expected_output, output);
+}
+
+TEST(StrTest, ToNumberFloatNegative)
+{
+ float output;
+ std::string input = "-0.1";
+ float expected_output = -0.1;
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(expected_output, output);
+}
+
+TEST(StrTest, ToNumberDouble)
+{
+ double output;
+ std::string input = "0.0001";
+ double expected_output = 0.0001;
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(expected_output, output);
+}
+
+TEST(StrTest, ToNumberDoubleIntegerString)
+{
+ double output;
+ std::string input = "12345";
+ double expected_output = 12345.0;
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(expected_output, output);
+}
+
+TEST(StrTest, ToNumberDoubleNegative)
+{
+ double output;
+ std::string input = "-1.2345";
+ double expected_output = -1.2345;
+ EXPECT_TRUE(to_number(input, output));
+ EXPECT_EQ(expected_output, output);
+}
+
+/*
+ * The "to_bool" function takes a string, "true" or "false"
+ * (case-insenstive), and sets the second argument to the bool equivilent.
+ * The function will return false if it cannot parse the string.
+ */
+TEST(StrTest, ToBoolTrue)
+{
+ bool output = false;
+ EXPECT_TRUE(to_bool("TrUe", output));
+ EXPECT_TRUE(output);
+}
+
+TEST(StrTest, ToBoolFalse){
+ bool output = true;
+ EXPECT_TRUE(to_bool("fAlSe", output));
+ EXPECT_FALSE(output);
+}
+
+TEST(StrTest, ToBoolInvalidInput)
+{
+ bool output;
+ EXPECT_FALSE(to_bool("falsify", output));
+}
+
+/*
+ * The "quote" function take a string and returns that string quoted (i.e.,
+ * between double-quotes) if the string contains a space.
+ */
+TEST(StrTest, QuoteStringNoSpace)
+{
+ EXPECT_EQ("hello", quote("hello"));
+}
+
+TEST(StrTest, QuoteStringWithSpace)
+{
+ EXPECT_EQ("\"hello world\"", quote("hello world"));
+}
+
+TEST(StrTest, QuoteQuotedString)
+{
+ /*
+ * At present, a quoted string can be quoted again.
+ */
+ EXPECT_EQ("\"\"hello world\"\"", quote("\"hello world\""));
+}
+
+TEST(StrTest, QuoteStringWithTab)
+{
+ /*
+ * The "quote" function only works with standard space, not any
+ * whitepsace.
+ */
+ EXPECT_EQ("hello\tworld", quote("hello\tworld"));
+}
+
+/*
+ * str.hh has three implementations of "startswith"; a function that takes
+ * string and a prefix and returns true if the string starts with the prefix.
+ * One implementation takes two strings, another takes two char*, and the
+ * third takes a string and a char* as a prefix.
+ */
+TEST(StrTest, StartswithDoubleStringDoesStartWith)
+{
+ std::string s = "Hello, how are you?";
+ std::string prefix = "Hello";
+ EXPECT_TRUE(startswith(s, prefix));
+}
+
+TEST(StrTest, StartswithDoubleStringDoesNotStartWith)
+{
+ std::string s = "Hello, how are you?";
+ std::string prefix = "ello";
+ EXPECT_FALSE(startswith(s, prefix));
+}
+
+TEST(StrTest, StartswithDoubleCharArrayDoesStartWith)
+{
+ const char* s = "abcdefg";
+ const char* prefix = "ab";
+ EXPECT_TRUE(startswith(s, prefix));
+}
+
+TEST(StrTest, StartswithDoubleCharArrayDoesNotStartWith)
+{
+ const char* s = " abcdefg";
+ const char* prefix = "a";
+ EXPECT_FALSE(startswith(s, prefix));
+}
+
+TEST(StrTest, StartswithStringCharArrayDoesStartWith)
+{
+ std::string s = "foobarr";
+ const char* prefix = "f";
+ EXPECT_TRUE(startswith(s, prefix));
+}
+
+TEST(StrTest, StartswithStringCharArrayDoesNotStartWith)
+{
+ std::string s = "foobarr";
+ const char* prefix = "barr";
+ EXPECT_FALSE(startswith(s, prefix));
+} \ No newline at end of file