tests,base: Added GTests for base/match.cc
authorBobby R. Bruce <bbruce@ucdavis.edu>
Fri, 1 Nov 2019 19:44:08 +0000 (12:44 -0700)
committerBobby R. Bruce <bbruce@ucdavis.edu>
Thu, 7 Nov 2019 18:34:01 +0000 (18:34 +0000)
In order to aid testing the method "match.getExpressions()" has been added.

Change-Id: I11acf9bed286ee2809dfa3d05ef573dea85eb786
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/22503
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Tested-by: kokoro <noreply+kokoro@google.com>
src/base/SConscript
src/base/match.cc
src/base/match.hh
src/base/match.test.cc [new file with mode: 0644]

index d6615be1393162c2c58046c050c8c0f2918c9cab..07a76cbba2cd0fe363d446506c3d5c6f1ed20563 100644 (file)
@@ -61,6 +61,7 @@ GTest('inifile.test', 'inifile.test.cc', 'inifile.cc', 'str.cc')
 Source('intmath.cc')
 Source('logging.cc')
 Source('match.cc')
+GTest('match.test', 'match.test.cc', 'match.cc', 'str.cc')
 Source('output.cc')
 Source('pixel.cc')
 GTest('pixel.test', 'pixel.test.cc', 'pixel.cc')
index 03f425f2e504ba982a9b8a49f42b50729e7b7172..7c2c0cf58d08bb3a75db67eb73720b6160bd94eb 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2019 The Regents of the University of California
  * Copyright (c) 2004-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
@@ -26,6 +27,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Authors: Nathan Binkert
+            Bobby R. Bruce
  */
 
 #include "base/match.hh"
@@ -103,3 +105,16 @@ ObjectMatch::domatch(const string &name) const
     return false;
 }
 
+std::vector<std::vector<std::string> >
+ObjectMatch::getExpressions()
+{
+    std::vector<std::vector<std::string> > to_return;
+    for (const std::vector<std::string> expression : tokens) {
+        std::vector<std::string> to_add;
+        to_add.insert(to_add.end(), expression.begin(), expression.end());
+        to_return.push_back(to_add);
+    }
+
+    return to_return;
+}
+
index 3ef4c819214e656d2d219ce04b6bedb8428f9a27..7f610236d2d6bd8feb54f8cb2e16b775bc41053f 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2019 The Regents of the University of California
  * Copyright (c) 2004-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
@@ -26,6 +27,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Authors: Nathan Binkert
+            Bobby R. Bruce
  */
 
 /* @file
 #include <string>
 #include <vector>
 
+/**
+ * ObjectMatch contains a vector of expressions. ObjectMatch can then be
+ * queried, via ObjectMatch.match(std::string), to check if a string matches
+ * any expressions in the vector.
+ *
+ * Expressions in ObjectMatch take the form "<token1>.<token2>.<token3>"; a
+ * series of expected tokens separated by a period. The input string takes the
+ * form "<value1>.<value2>.<value3>". In this case, the input string matches
+ * the expression if <value1> == <token1> && <token2> == <value2>
+ * && <value3> == <token3>.  A token may be a wildcard character, "*", which
+ * will match to any value in that position (inclusive of no value at that
+ * location).
+ */
 class ObjectMatch
 {
   protected:
@@ -50,6 +65,7 @@ class ObjectMatch
     void add(const ObjectMatch &other);
     void setExpression(const std::string &expression);
     void setExpression(const std::vector<std::string> &expression);
+    std::vector<std::vector<std::string> > getExpressions();
     bool match(const std::string &name) const
     {
         return tokens.empty() ? false : domatch(name);
diff --git a/src/base/match.test.cc b/src/base/match.test.cc
new file mode 100644 (file)
index 0000000..0c79351
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2019 The Regents of the University of California
+ * All rights reserved
+ *
+ * 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/match.hh"
+
+TEST(MatchTest, Add)
+{
+    /*
+     * ObjectMatch.add will add all the expressions from one ObjectMatch to
+     * the other.
+     */
+    ObjectMatch object_match_1("token1.token2");
+    ObjectMatch object_match_2("token3");
+    ObjectMatch object_match_3;
+
+    object_match_3.add(object_match_1);
+    object_match_3.add(object_match_2);
+
+    std::vector<std::vector<std::string> > expressions =
+        object_match_3.getExpressions();
+
+    EXPECT_EQ(2, expressions.size());
+    EXPECT_EQ(2, expressions[0].size());
+    EXPECT_EQ(1, expressions[1].size());
+
+    EXPECT_EQ(expressions[0][0], "token1");
+    EXPECT_EQ(expressions[0][1], "token2");
+    EXPECT_EQ(expressions[1][0], "token3");
+}
+
+TEST(MatchTest, SetExpression)
+{
+    ObjectMatch object_match;
+    object_match.setExpression("A.B.C.D");
+
+    std::vector<std::vector<std::string> > expressions =
+        object_match.getExpressions();
+
+    EXPECT_EQ(1, expressions.size());
+    EXPECT_EQ(4, expressions[0].size());
+
+    EXPECT_EQ("A", expressions[0][0]);
+    EXPECT_EQ("B", expressions[0][1]);
+    EXPECT_EQ("C", expressions[0][2]);
+    EXPECT_EQ("D", expressions[0][3]);
+}
+
+TEST(MatchTest, SetExpressionVector)
+{
+    ObjectMatch object_match;
+
+    std::vector<std::string> to_add;
+    to_add.push_back("A.B.C.D");
+    to_add.push_back("E.F.G");
+
+    object_match.setExpression(to_add);
+
+    std::vector<std::vector<std::string> > expressions =
+        object_match.getExpressions();
+
+    EXPECT_EQ(2, expressions.size());
+    EXPECT_EQ(4, expressions[0].size());
+    EXPECT_EQ(3, expressions[1].size());
+
+    EXPECT_EQ("A", expressions[0][0]);
+    EXPECT_EQ("B", expressions[0][1]);
+    EXPECT_EQ("C", expressions[0][2]);
+    EXPECT_EQ("D", expressions[0][3]);
+    EXPECT_EQ("E", expressions[1][0]);
+    EXPECT_EQ("F", expressions[1][1]);
+    EXPECT_EQ("G", expressions[1][2]);
+}
+
+TEST(MatchTest, SimpleMatch)
+{
+    ObjectMatch object_match("this.is.a.perfect.match");
+    EXPECT_TRUE(object_match.match("this.is.a.perfect.match"));
+}
+
+TEST(MatchTest, SimpleMismatch)
+{
+    ObjectMatch object_match("this.is.a.perfect.match");
+    EXPECT_FALSE(object_match.match("this.is.a.perfect.--"));
+}
+
+TEST(MatchTest, MultipleExpressionsMatch)
+{
+    ObjectMatch object_match;
+    std::vector<std::string> expressions;
+    expressions.push_back("A.B.C.D");
+    expressions.push_back("E.F.G");
+    object_match.setExpression(expressions);
+
+    EXPECT_TRUE(object_match.match("A.B.C.D"));
+    EXPECT_TRUE(object_match.match("E.F.G"));
+}
+
+TEST(MatchTest, MultipleExpressionsMismatch)
+{
+    ObjectMatch object_match;
+    std::vector<std::string> expressions;
+    expressions.push_back("A.B.C.D");
+    expressions.push_back("E.F.G");
+    object_match.setExpression(expressions);
+
+    EXPECT_FALSE(object_match.match("B.C.D"));
+    EXPECT_FALSE(object_match.match("D.E.F.G"));
+}
+
+TEST(MatchTest, WildCardMatch)
+{
+    ObjectMatch object_match("this.is.a.*.match");
+
+    /*
+     * Note: the wildcard token can represent an empty token.
+     */
+    EXPECT_TRUE(object_match.match("this.is.a.match"));
+    EXPECT_TRUE(object_match.match("this.is.a.perfect.match"));
+    EXPECT_TRUE(object_match.match("this.is.a.great.match"));
+}
+
+TEST(MatchTest, WildCardMismatch)
+{
+    ObjectMatch object_match("this.is.a.*.match");
+
+    EXPECT_FALSE(object_match.match("this.is.a.bla.bla.match"));
+    EXPECT_FALSE(object_match.match("this.is.a.great.match--"));
+}
+
+TEST(MatchTest, TokensEmptyNoMatch)
+{
+    ObjectMatch object_match;
+    EXPECT_FALSE(object_match.match("token1"));
+}