gem5  v20.1.0.0
match.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  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include "base/match.hh"
32 
33 TEST(MatchTest, Add)
34 {
35  /*
36  * ObjectMatch.add will add all the expressions from one ObjectMatch to
37  * the other.
38  */
39  ObjectMatch object_match_1("token1.token2");
40  ObjectMatch object_match_2("token3");
41  ObjectMatch object_match_3;
42 
43  object_match_3.add(object_match_1);
44  object_match_3.add(object_match_2);
45 
47  object_match_3.getExpressions();
48 
49  EXPECT_EQ(2, expressions.size());
50  EXPECT_EQ(2, expressions[0].size());
51  EXPECT_EQ(1, expressions[1].size());
52 
53  EXPECT_EQ(expressions[0][0], "token1");
54  EXPECT_EQ(expressions[0][1], "token2");
55  EXPECT_EQ(expressions[1][0], "token3");
56 }
57 
58 TEST(MatchTest, SetExpression)
59 {
60  ObjectMatch object_match;
61  object_match.setExpression("A.B.C.D");
62 
64  object_match.getExpressions();
65 
66  EXPECT_EQ(1, expressions.size());
67  EXPECT_EQ(4, expressions[0].size());
68 
69  EXPECT_EQ("A", expressions[0][0]);
70  EXPECT_EQ("B", expressions[0][1]);
71  EXPECT_EQ("C", expressions[0][2]);
72  EXPECT_EQ("D", expressions[0][3]);
73 }
74 
75 TEST(MatchTest, SetExpressionVector)
76 {
77  ObjectMatch object_match;
78 
80  to_add.push_back("A.B.C.D");
81  to_add.push_back("E.F.G");
82 
83  object_match.setExpression(to_add);
84 
86  object_match.getExpressions();
87 
88  EXPECT_EQ(2, expressions.size());
89  EXPECT_EQ(4, expressions[0].size());
90  EXPECT_EQ(3, expressions[1].size());
91 
92  EXPECT_EQ("A", expressions[0][0]);
93  EXPECT_EQ("B", expressions[0][1]);
94  EXPECT_EQ("C", expressions[0][2]);
95  EXPECT_EQ("D", expressions[0][3]);
96  EXPECT_EQ("E", expressions[1][0]);
97  EXPECT_EQ("F", expressions[1][1]);
98  EXPECT_EQ("G", expressions[1][2]);
99 }
100 
101 TEST(MatchTest, SimpleMatch)
102 {
103  ObjectMatch object_match("this.is.a.perfect.match");
104  EXPECT_TRUE(object_match.match("this.is.a.perfect.match"));
105 }
106 
107 TEST(MatchTest, SimpleMismatch)
108 {
109  ObjectMatch object_match("this.is.a.perfect.match");
110  EXPECT_FALSE(object_match.match("this.is.a.perfect.--"));
111 }
112 
113 TEST(MatchTest, MultipleExpressionsMatch)
114 {
115  ObjectMatch object_match;
116  std::vector<std::string> expressions;
117  expressions.push_back("A.B.C.D");
118  expressions.push_back("E.F.G");
119  object_match.setExpression(expressions);
120 
121  EXPECT_TRUE(object_match.match("A.B.C.D"));
122  EXPECT_TRUE(object_match.match("E.F.G"));
123 }
124 
125 TEST(MatchTest, MultipleExpressionsMismatch)
126 {
127  ObjectMatch object_match;
128  std::vector<std::string> expressions;
129  expressions.push_back("A.B.C.D");
130  expressions.push_back("E.F.G");
131  object_match.setExpression(expressions);
132 
133  EXPECT_FALSE(object_match.match("B.C.D"));
134  EXPECT_FALSE(object_match.match("D.E.F.G"));
135 }
136 
137 TEST(MatchTest, WildCardMatch)
138 {
139  ObjectMatch object_match("this.is.a.*.match");
140 
141  /*
142  * Note: the wildcard token can represent an empty token.
143  */
144  EXPECT_TRUE(object_match.match("this.is.a.match"));
145  EXPECT_TRUE(object_match.match("this.is.a.perfect.match"));
146  EXPECT_TRUE(object_match.match("this.is.a.great.match"));
147 }
148 
149 TEST(MatchTest, WildCardMismatch)
150 {
151  ObjectMatch object_match("this.is.a.*.match");
152 
153  EXPECT_FALSE(object_match.match("this.is.a.bla.bla.match"));
154  EXPECT_FALSE(object_match.match("this.is.a.great.match--"));
155 }
156 
157 TEST(MatchTest, TokensEmptyNoMatch)
158 {
159  ObjectMatch object_match;
160  EXPECT_FALSE(object_match.match("token1"));
161 }
ObjectMatch::match
bool match(const std::string &name) const
Definition: match.hh:66
std::vector
STL vector class.
Definition: stl.hh:37
EXPECT_EQ
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:110
EXPECT_FALSE
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:106
match.hh
EXPECT_TRUE
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:103
ObjectMatch::setExpression
void setExpression(const std::string &expression)
TEST
TEST(MatchTest, Add)
Definition: match.test.cc:33
ObjectMatch::add
void add(const ObjectMatch &other)
Definition: match.cc:46
ObjectMatch
ObjectMatch contains a vector of expressions.
Definition: match.hh:53
ObjectMatch::getExpressions
std::vector< std::vector< std::string > > getExpressions()
Definition: match.cc:106

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