P4C
The P4 Compiler
Loading...
Searching...
No Matches
tofino/test_backend/ptf.h
1/*******************************************************************************
2 * Copyright (C) 2024 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing,
11 * software distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions
14 * and limitations under the License.
15 *
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 ******************************************************************************/
19
20#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_TOFINO_TEST_BACKEND_PTF_H_
21#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_TOFINO_TEST_BACKEND_PTF_H_
22
23#include <cstddef>
24#include <fstream>
25#include <optional>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include <inja/inja.hpp>
31
32#include "ir/ir.h"
33#include "lib/cstring.h"
34
35#include "backends/p4tools/modules/testgen/lib/test_framework.h"
36
37namespace P4::P4Tools::P4Testgen::Tofino {
38
40class PTF : public TestFramework {
42 bool preambleEmitted = false;
43
45 std::ofstream ptfFileStream;
46
47 public:
48 ~PTF() override = default;
49
50 PTF(const PTF &) = delete;
51
52 PTF(PTF &&) = delete;
53
54 PTF &operator=(const PTF &) = delete;
55
56 PTF &operator=(PTF &&) = delete;
57
58 explicit PTF(const TestBackendConfiguration &testBackendConfiguration);
59
61 void writeTestToFile(const TestSpec *spec, cstring selectedBranches, size_t testIdx,
62 float currentCoverage) override;
63
64 private:
67 void emitPreamble();
68
74 void emitTestcase(const TestSpec *testSpec, cstring selectedBranches, size_t testIdx,
75 const std::string &testCase, float currentCoverage);
76
78 static std::string getTestCaseTemplate();
79
81 static inja::json getControlPlane(const TestSpec *testSpec);
82
84 static inja::json getSend(const TestSpec *testSpec);
85
87 static inja::json getVerify(const TestSpec *testSpec);
88
91 static std::vector<std::pair<size_t, size_t>> getIgnoreMasks(const IR::Constant *mask);
92
94 static inja::json getControlPlaneForTable(const TableMatchMap &matches,
95 const std::vector<ActionArg> &args);
96};
97
98} // namespace P4::P4Tools::P4Testgen::Tofino
99
100#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_TOFINO_TEST_BACKEND_PTF_H_ */
TestFramework(const TestBackendConfiguration &testBackendConfiguration)
Creates a generic test framework.
Definition test_framework.cpp:7
Definition lib/test_spec.h:296
void writeTestToFile(const TestSpec *spec, cstring selectedBranches, size_t testIdx, float currentCoverage) override
Produce a PTF test.
Definition tofino/test_backend/ptf.cpp:310
Definition cstring.h:85
Definition test_backend_configuration.h:16