From: Bobby R. Bruce Date: Fri, 1 Nov 2019 19:44:08 +0000 (-0700) Subject: tests,base: Added GTests for base/match.cc X-Git-Tag: v19.0.0.0~315 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=434047e2c3200b6b249c206fb2a4a306225b473e;p=gem5.git tests,base: Added GTests for base/match.cc 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 Reviewed-by: Jason Lowe-Power Maintainer: Jason Lowe-Power Tested-by: kokoro --- diff --git a/src/base/SConscript b/src/base/SConscript index d6615be13..07a76cbba 100644 --- a/src/base/SConscript +++ b/src/base/SConscript @@ -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') diff --git a/src/base/match.cc b/src/base/match.cc index 03f425f2e..7c2c0cf58 100644 --- a/src/base/match.cc +++ b/src/base/match.cc @@ -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 > +ObjectMatch::getExpressions() +{ + std::vector > to_return; + for (const std::vector expression : tokens) { + std::vector to_add; + to_add.insert(to_add.end(), expression.begin(), expression.end()); + to_return.push_back(to_add); + } + + return to_return; +} + diff --git a/src/base/match.hh b/src/base/match.hh index 3ef4c8192..7f610236d 100644 --- a/src/base/match.hh +++ b/src/base/match.hh @@ -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 @@ -38,6 +40,19 @@ #include #include +/** + * 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 ".."; a + * series of expected tokens separated by a period. The input string takes the + * form "..". In this case, the input string matches + * the expression if == && == + * && == . 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 &expression); + std::vector > 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 index 000000000..0c7935146 --- /dev/null +++ b/src/base/match.test.cc @@ -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 + +#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 > 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 > 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 to_add; + to_add.push_back("A.B.C.D"); + to_add.push_back("E.F.G"); + + object_match.setExpression(to_add); + + std::vector > 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 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 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")); +}