P4C
The P4 Compiler
Loading...
Searching...
No Matches
lib/test_spec.h
1#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_TEST_SPEC_H_
2#define BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_TEST_SPEC_H_
3
4#include <functional>
5#include <map>
6#include <optional>
7#include <vector>
8
9#include "backends/p4tools/common/lib/model.h"
10#include "backends/p4tools/common/lib/trace_event.h"
11#include "ir/ir.h"
12#include "lib/cstring.h"
13
14#include "backends/p4tools/modules/testgen/lib/test_object.h"
15
16namespace P4::P4Tools::P4Testgen {
17
22
23/* =========================================================================================
24 * Packet
25 * ========================================================================================= */
26
27class Packet : public TestObject {
28 private:
30 int port;
31
33 const IR::Expression *payload;
34
36 const IR::Expression *payloadIgnoreMask;
37
38 public:
39 Packet(int port, const IR::Expression *payload, const IR::Expression *payloadIgnoreMask);
40
41 [[nodiscard]] const Packet *evaluate(const Model &model, bool doComplete) const override;
42
43 [[nodiscard]] cstring getObjectName() const override;
44
46 [[nodiscard]] int getPort() const;
47
50 [[nodiscard]] const IR::Constant *getEvaluatedPayload() const;
51
56 [[nodiscard]] const IR::Constant *getEvaluatedPayloadMask() const;
57
58 DECLARE_TYPEINFO(Packet, TestObject);
59};
60
61/* =========================================================================================
62 * Table Test Objects
63 * ========================================================================================= */
64
65class ActionArg : public TestObject {
66 private:
68 const IR::Parameter *param;
69
71 const IR::Expression *value;
72
73 public:
74 ActionArg(const IR::Parameter *param, const IR::Expression *value);
75
76 [[nodiscard]] const ActionArg *evaluate(const Model &model, bool doComplete) const override;
77
78 [[nodiscard]] cstring getObjectName() const override;
79
80 [[nodiscard]] const IR::Parameter *getActionParam() const;
81
83 [[nodiscard]] cstring getActionParamName() const;
84
88 [[nodiscard]] const IR::Constant *getEvaluatedValue() const;
89
90 DECLARE_TYPEINFO(ActionArg, TestObject);
91};
92
93class ActionCall : public TestObject {
94 private:
96 cstring identifier;
97
99 const IR::P4Action *action;
100
102 const std::vector<ActionArg> args;
103
104 public:
105 ActionCall(cstring identifier, const IR::P4Action *action, std::vector<ActionArg> args);
106
107 ActionCall(const IR::P4Action *action, std::vector<ActionArg> args);
108
109 [[nodiscard]] const ActionCall *evaluate(const Model &model, bool doComplete) const override;
110
111 [[nodiscard]] cstring getObjectName() const override;
112
115 [[nodiscard]] cstring getActionName() const;
116
118 [[nodiscard]] const IR::P4Action *getAction() const;
119
121 [[nodiscard]] const std::vector<ActionArg> *getArgs() const;
122
123 DECLARE_TYPEINFO(ActionCall, TestObject);
124};
125
126class TableMatch : public TestObject {
127 private:
129 const IR::KeyElement *key;
130
131 public:
132 explicit TableMatch(const IR::KeyElement *key);
133
135 [[nodiscard]] const IR::KeyElement *getKey() const;
136
137 DECLARE_TYPEINFO(TableMatch, TestObject);
138};
139
140using TableMatchMap = std::map<cstring, const TableMatch *>;
141
142class Ternary : public TableMatch {
143 private:
145 const IR::Expression *value;
146
148 const IR::Expression *mask;
149
150 public:
151 explicit Ternary(const IR::KeyElement *key, const IR::Expression *value,
152 const IR::Expression *mask);
153
154 [[nodiscard]] const Ternary *evaluate(const Model &model, bool doComplete) const override;
155
156 [[nodiscard]] cstring getObjectName() const override;
157
161 [[nodiscard]] const IR::Constant *getEvaluatedValue() const;
162
166 [[nodiscard]] const IR::Constant *getEvaluatedMask() const;
167
168 DECLARE_TYPEINFO(Ternary, TableMatch);
169};
170
171class LPM : public TableMatch {
172 private:
174 const IR::Expression *value;
175
177 const IR::Expression *prefixLength;
178
179 public:
180 explicit LPM(const IR::KeyElement *key, const IR::Expression *value,
181 const IR::Expression *prefixLength);
182
183 [[nodiscard]] const LPM *evaluate(const Model &model, bool doComplete) const override;
184
185 [[nodiscard]] cstring getObjectName() const override;
186
190 [[nodiscard]] const IR::Constant *getEvaluatedValue() const;
191
195 [[nodiscard]] const IR::Constant *getEvaluatedPrefixLength() const;
196
197 DECLARE_TYPEINFO(LPM, TableMatch);
198};
199
200class Exact : public TableMatch {
201 private:
203 const IR::Expression *value;
204
205 public:
206 explicit Exact(const IR::KeyElement *key, const IR::Expression *value);
207
208 [[nodiscard]] const Exact *evaluate(const Model &model, bool doComplete) const override;
209
210 [[nodiscard]] cstring getObjectName() const override;
211
214 [[nodiscard]] const IR::Constant *getEvaluatedValue() const;
215
216 DECLARE_TYPEINFO(Exact, TableMatch);
217};
218
219class TableRule : public TestObject {
220 private:
222 const TableMatchMap matches;
223
225 int priority;
227 const ActionCall action;
229 int ttl;
230
231 public:
232 TableRule(TableMatchMap matches, int priority, ActionCall action, int ttl);
233
234 [[nodiscard]] const TableRule *evaluate(const Model &model, bool doComplete) const override;
235
236 [[nodiscard]] cstring getObjectName() const override;
237
239 [[nodiscard]] const TableMatchMap *getMatches() const;
240
242 [[nodiscard]] int getPriority() const;
243
245 [[nodiscard]] const ActionCall *getActionCall() const;
246
248 [[nodiscard]] int getTTL() const;
249
250 DECLARE_TYPEINFO(TableRule, TestObject);
251};
252
253class TableConfig : public TestObject {
254 private:
256 const IR::P4Table *table;
257
259 const std::vector<TableRule> rules;
260
262 TestObjectMap tableProperties;
263
264 public:
265 explicit TableConfig(const IR::P4Table *table, std::vector<TableRule> rules);
266
267 explicit TableConfig(const IR::P4Table *table, std::vector<TableRule> rules,
268 TestObjectMap tableProperties);
269
270 [[nodiscard]] const IR::P4Table *getTable() const;
271
272 [[nodiscard]] cstring getObjectName() const override;
273
274 [[nodiscard]] const TableConfig *evaluate(const Model &model, bool doComplete) const override;
275
277 [[nodiscard]] const std::vector<TableRule> *getRules() const;
278
280 [[nodiscard]] const TestObjectMap *getProperties() const;
281
284 [[nodiscard]] const TestObject *getProperty(cstring propertyName, bool checked) const;
285
287 void addTableProperty(cstring propertyName, const TestObject *property);
288
289 DECLARE_TYPEINFO(TableConfig, TestObject);
290};
291
292/* =========================================================================================
293 * Test Specification
294 * ========================================================================================= */
295
296class TestSpec {
297 private:
299 const Packet ingressPacket;
300
303 const std::optional<Packet> egressPacket;
304
306 const std::vector<std::reference_wrapper<const TraceEvent>> traces;
307
310 std::map<cstring, TestObjectMap> testObjects;
311
312 public:
313 TestSpec(Packet ingressPacket, std::optional<Packet> egressPacket,
314 std::vector<std::reference_wrapper<const TraceEvent>> traces);
315
319 void addTestObject(cstring category, cstring objectLabel, const TestObject *object);
320
323 [[nodiscard]] const TestObject *getTestObject(cstring category, cstring objectLabel,
324 bool checked) const;
325
330 template <class T>
331 [[nodiscard]] auto *getTestObject(cstring category, cstring objectLabel, bool checked) const {
332 const auto *testObject = getTestObject(category, objectLabel, checked);
333 return testObject->checkedTo<T>();
334 }
335
337 [[nodiscard]] TestObjectMap getTestObjectCategory(cstring category) const;
338
340 [[nodiscard]] const Packet *getIngressPacket() const;
341
343 [[nodiscard]] const std::map<cstring, const TableConfig> *getTables() const;
344
346 [[nodiscard]] std::optional<const Packet *> getEgressPacket() const;
347
349 [[nodiscard]] const std::vector<std::reference_wrapper<const TraceEvent>> *getTraces() const;
350
352 static constexpr int NO_PRIORITY = -1;
353 static constexpr int LOW_PRIORITY = 1;
354 static constexpr int HIGH_PRIORITY = 100;
355 static constexpr int TTL = 0;
356};
357
358} // namespace P4::P4Tools::P4Testgen
359
360#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_TEST_SPEC_H_ */
Definition backends/p4tools/common/lib/model.h:18
Definition lib/test_spec.h:65
cstring getObjectName() const override
Definition lib/test_spec.cpp:79
const IR::Constant * getEvaluatedValue() const
Definition lib/test_spec.cpp:67
cstring getActionParamName() const
Definition lib/test_spec.cpp:65
const ActionArg * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:81
Definition lib/test_spec.h:93
cstring getActionName() const
Definition lib/test_spec.cpp:92
cstring getObjectName() const override
Definition lib/test_spec.cpp:105
const ActionCall * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:96
const IR::P4Action * getAction() const
Definition lib/test_spec.cpp:94
const std::vector< ActionArg > * getArgs() const
Definition lib/test_spec.cpp:107
Definition lib/test_spec.h:200
cstring getObjectName() const override
Definition lib/test_spec.cpp:187
const Exact * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:182
const IR::Constant * getEvaluatedValue() const
Definition lib/test_spec.cpp:173
Definition lib/test_spec.h:171
const IR::Constant * getEvaluatedPrefixLength() const
Definition lib/test_spec.cpp:154
const LPM * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:163
const IR::Constant * getEvaluatedValue() const
Definition lib/test_spec.cpp:145
cstring getObjectName() const override
Definition lib/test_spec.cpp:169
Definition lib/test_spec.h:27
int getPort() const
Definition lib/test_spec.cpp:30
const Packet * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:50
cstring getObjectName() const override
Definition lib/test_spec.cpp:28
const IR::Constant * getEvaluatedPayloadMask() const
Definition lib/test_spec.cpp:32
const IR::Constant * getEvaluatedPayload() const
Definition lib/test_spec.cpp:41
Definition lib/test_spec.h:253
const std::vector< TableRule > * getRules() const
Definition lib/test_spec.cpp:226
const TableConfig * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:245
const TestObject * getProperty(cstring propertyName, bool checked) const
Definition lib/test_spec.cpp:230
cstring getObjectName() const override
Definition lib/test_spec.cpp:217
void addTableProperty(cstring propertyName, const TestObject *property)
Add a table property to the table.
Definition lib/test_spec.cpp:241
const TestObjectMap * getProperties() const
Definition lib/test_spec.cpp:228
Definition lib/test_spec.h:126
const IR::KeyElement * getKey() const
Definition lib/test_spec.cpp:111
Definition lib/test_spec.h:219
const TableRule * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:202
int getTTL() const
Definition lib/test_spec.cpp:198
cstring getObjectName() const override
Definition lib/test_spec.cpp:200
int getPriority() const
Definition lib/test_spec.cpp:194
const ActionCall * getActionCall() const
Definition lib/test_spec.cpp:196
const TableMatchMap * getMatches() const
Definition lib/test_spec.cpp:192
Definition lib/test_spec.h:142
const IR::Constant * getEvaluatedValue() const
Definition lib/test_spec.cpp:116
const Ternary * evaluate(const Model &model, bool doComplete) const override
Definition lib/test_spec.cpp:134
const IR::Constant * getEvaluatedMask() const
Definition lib/test_spec.cpp:125
cstring getObjectName() const override
Definition lib/test_spec.cpp:140
Definition test_object.h:15
Definition lib/test_spec.h:296
std::optional< const Packet * > getEgressPacket() const
Definition lib/test_spec.cpp:272
const std::vector< std::reference_wrapper< const TraceEvent > > * getTraces() const
Definition lib/test_spec.cpp:281
const std::map< cstring, const TableConfig > * getTables() const
const Packet * getIngressPacket() const
Definition lib/test_spec.cpp:279
auto * getTestObject(cstring category, cstring objectLabel, bool checked) const
Definition lib/test_spec.h:331
void addTestObject(cstring category, cstring objectLabel, const TestObject *object)
Definition lib/test_spec.cpp:285
TestObjectMap getTestObjectCategory(cstring category) const
Definition lib/test_spec.cpp:303
static constexpr int NO_PRIORITY
Priority definitions for LPM and ternary entries.
Definition lib/test_spec.h:352
const TestObject * getTestObject(cstring category, cstring objectLabel, bool checked) const
Definition lib/test_spec.cpp:289
Definition cstring.h:85