P4C
The P4 Compiler
Loading...
Searching...
No Matches
test/gtest_utils.h
1/*
2 * SPDX-FileCopyrightText: 2022 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_
8#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_
9
10#include <gtest/gtest.h>
11
12#include <memory>
13#include <optional>
14#include <string>
15
16#include "frontends/common/options.h"
17#include "ir/ir.h"
18#include "lib/compile_context.h"
19
20#include "backends/p4tools/modules/testgen/core/target.h"
21#include "backends/p4tools/modules/testgen/register.h"
22#include "backends/p4tools/modules/testgen/toolname.h"
23
24namespace P4::P4Tools::Test {
25
27class P4ToolsTestCase {
28 public:
30 static std::optional<const P4ToolsTestCase> create(std::string deviceName, std::string archName,
31 CompilerOptions::FrontendVersion langVersion,
32 const std::string &source);
33
35 static std::optional<const P4ToolsTestCase> create_14(std::string deviceName,
36 std::string archName,
37 const std::string &source);
38
40 static std::optional<const P4ToolsTestCase> create_16(std::string deviceName,
41 std::string archName,
42 const std::string &source);
43
44 explicit P4ToolsTestCase(const P4Tools::CompilerResult &compilerResults);
45
47 [[nodiscard]] const IR::P4Program &getProgram() const;
48
51 [[nodiscard]] const P4Tools::CompilerResult &getCompilerResult() const;
52
53 private:
55 std::reference_wrapper<const P4Tools::CompilerResult> compilerResults;
56
58 static void ensureInit();
59};
60
61class P4TestgenTest : public testing::Test {
62 public:
63 [[nodiscard]] static std::unique_ptr<AutoCompileContext> SetUp(std::string_view target,
64 std::string_view archName) {
65 P4Tools::P4Testgen::registerTestgenTargets();
68 auto ctxOpt = P4Testgen::TestgenTarget::initializeTarget(P4Tools::P4Testgen::TOOL_NAME,
69 target, archName);
70
71 if (!ctxOpt.has_value()) {
72 return nullptr;
73 }
74 return std::make_unique<AutoCompileContext>(ctxOpt.value());
75 }
76};
77
79class SymbolicConverter : public Transform {
80 public:
81 const IR::SymbolicVariable *preorder(IR::Member *member) override;
82};
83
84} // namespace P4::P4Tools::Test
85
86#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_ */
Definition common/compiler/compiler_result.h:20
static std::optional< ICompileContext * > initializeTarget(std::string_view toolName, const std::vector< const char * > &args)
Definition p4tools/common/core/target.cpp:73
Definition test/gtest_utils.h:61
static std::unique_ptr< AutoCompileContext > SetUp(std::string_view target, std::string_view archName)
Definition test/gtest_utils.h:63
const IR::P4Program & getProgram() const
Definition test/gtest_utils.cpp:54
const P4Tools::CompilerResult & getCompilerResult() const
Definition test/gtest_utils.cpp:58
static std::optional< const P4ToolsTestCase > create_16(std::string deviceName, std::string archName, const std::string &source)
Factory method for producing a test case from a P4_16 program source.
Definition test/gtest_utils.cpp:68
static std::optional< const P4ToolsTestCase > create(std::string deviceName, std::string archName, CompilerOptions::FrontendVersion langVersion, const std::string &source)
Factory method for producing a test case from a P4 program source.
Definition test/gtest_utils.cpp:27
static std::optional< const P4ToolsTestCase > create_14(std::string deviceName, std::string archName, const std::string &source)
Factory method for producing a test case from a P4_14 program source.
Definition test/gtest_utils.cpp:62
Converts IR::Member into symbolic variables.
Definition test/gtest_utils.h:79