util: Pull "usage()" out of the call types in the m5 utility.
[gem5.git] / util / m5 / src / call_type.test.cc
1 /*
2 * Copyright 2020 Google Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <gtest/gtest.h>
29
30 // For EXPECT_THAT and HasSubstr
31 #include <gmock/gmock.h>
32
33 #include "args.hh"
34 #include "call_type.hh"
35
36 // Simple substitute definitions for Args and DispatchTable, with an int so
37 // we can tell instances apart.
38 class DispatchTable { public: int i; };
39
40 class TestCallType : public CallType
41 {
42 protected:
43 bool isDefault() const override { return testIsDefault; }
44 void
45 init() override
46 {
47 testInitHappened = true;
48 CallType::init();
49 }
50
51 void printBrief(std::ostream &os) const override { os << testBrief; }
52 void printDesc(std::ostream &os) const override { os << testDesc; }
53 public:
54 TestCallType(const std::string &_name) : CallType(_name) {}
55
56 static std::map<std::string, CallType &> &testGetMap() { return map(); }
57
58 // Usage strings to return.
59 std::string testBrief;
60 std::string testDesc;
61
62 // Return this dispatch table when requested.
63 DispatchTable testDt = { 0 };
64 const DispatchTable &getDispatch() const override { return testDt; }
65
66 // Whether this call type should be considered default.
67 bool testIsDefault = false;
68
69 // Whether init has been called.
70 bool testInitHappened = false;
71 };
72
73 TEST(CallTypeTest, Constructor)
74 {
75 auto &map = TestCallType::testGetMap();
76
77 // There should be no call types yet.
78 EXPECT_EQ(map.size(), 0);
79
80 // Create one.
81 TestCallType test_ct("test_ct");
82
83 // Set the dispatch table to something we'll recognize.
84 test_ct.testDt.i = 0xaa55;
85
86 // Verify that the list of all call types has one in it now.
87 EXPECT_EQ(map.size(), 1);
88
89 // Verify that that was our call type by verifying that the dispatch table
90 // has our signature.
91 EXPECT_EQ(map.begin()->second.getDispatch().i, 0xaa55);
92 }
93
94 TEST(CallTypeTest, DetectOne)
95 {
96 auto &map = TestCallType::testGetMap();
97
98 // One option selected.
99 TestCallType option1("option1");
100 option1.testIsDefault = true;
101 option1.testDt.i = 1;
102
103 EXPECT_EQ(map.size(), 1);
104
105 Args args1({"--option1"});
106
107 EXPECT_FALSE(option1.testInitHappened);
108
109 auto *ct = CallType::detect(args1);
110
111 // Verify that we selected the only option.
112 EXPECT_TRUE(option1.testInitHappened);
113 EXPECT_EQ(ct, &option1);
114
115 // One option, selecting the default.
116 option1.testInitHappened = false;
117
118 // Args will not match.
119 Args args2({"--option2"});
120
121 auto *def_ct = CallType::detect(args2);
122
123 // Verify that the one option was defaulted to.
124 EXPECT_TRUE(option1.testInitHappened);
125 EXPECT_EQ(def_ct, &option1);
126 }
127
128 TEST(CallTypeTest, DetectTwo)
129 {
130 auto &map = TestCallType::testGetMap();
131
132 // One of two options selected.
133 TestCallType option1("option1");
134 option1.testIsDefault = true;
135 option1.testDt.i = 1;
136
137 TestCallType option2("option2");
138 option2.testIsDefault = false;
139 option2.testDt.i = 2;
140
141 EXPECT_EQ(map.size(), 2);
142
143 // Select the first option.
144 Args args1({"--option1"});
145
146 EXPECT_FALSE(option1.testInitHappened);
147 EXPECT_FALSE(option2.testInitHappened);
148
149 auto *ct1 = CallType::detect(args1);
150
151 // Verify that we selected the first option.
152 EXPECT_TRUE(option1.testInitHappened);
153 EXPECT_FALSE(option2.testInitHappened);
154 EXPECT_EQ(ct1, &option1);
155
156 option1.testInitHappened = false;
157 option2.testInitHappened = false;
158
159 // Select the second option.
160 Args args2({"--option2"});
161
162 auto *ct2 = CallType::detect(args2);
163
164 // Verify that we selected the second option.
165 EXPECT_FALSE(option1.testInitHappened);
166 EXPECT_TRUE(option2.testInitHappened);
167 EXPECT_EQ(ct2, &option2);
168
169 option1.testInitHappened = false;
170 option2.testInitHappened = false;
171
172 // Default to the first option.
173 Args args3({"--option3"});
174
175 auto *def_ct1 = CallType::detect(args3);
176
177 // Verify that we selected the first option.
178 EXPECT_TRUE(option1.testInitHappened);
179 EXPECT_FALSE(option2.testInitHappened);
180 EXPECT_EQ(def_ct1, &option1);
181
182 option1.testInitHappened = false;
183 option2.testInitHappened = false;
184
185 // Default to the second option.
186 option1.testIsDefault = false;
187 option2.testIsDefault = true;
188
189 auto *def_ct2 = CallType::detect(args3);
190
191 // Verify that we selected the second option.
192 EXPECT_FALSE(option1.testInitHappened);
193 EXPECT_TRUE(option2.testInitHappened);
194 EXPECT_EQ(def_ct2, &option2);
195
196 option1.testInitHappened = false;
197 option2.testInitHappened = false;
198 }
199
200 TEST(CallTypeTest, Usage)
201 {
202 auto &map = TestCallType::testGetMap();
203
204 TestCallType ct1("ct1");
205 ct1.testBrief = "brief 1";
206 ct1.testDesc = "A longer description of call type 1, which is long.";
207
208 TestCallType ct2("ct2");
209 ct2.testBrief = "short 2";
210 ct2.testDesc = "Very verbose text saying what call type 2 is, "
211 "and is different from 1.";
212
213 EXPECT_EQ(map.size(), 2);
214
215 auto summary = CallType::usageSummary();
216
217 // For now, just expect that the brief and full descriptive text shows up
218 // in the summary somewhere.
219 //
220 // More strict checks might test that things were placed in the right
221 // order, were on their own lines when appropriate, etc.
222 EXPECT_THAT(summary, testing::HasSubstr(ct1.testBrief));
223 EXPECT_THAT(summary, testing::HasSubstr(ct1.testDesc));
224 EXPECT_THAT(summary, testing::HasSubstr(ct2.testBrief));
225 EXPECT_THAT(summary, testing::HasSubstr(ct2.testDesc));
226 EXPECT_THAT(summary, testing::HasSubstr(ct2.testDesc));
227 }