P4C
The P4 Compiler
Loading...
Searching...
No Matches
test/gtest_utils.h
1#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_
2#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_
3
4#include <gtest/gtest.h>
5
6#include <memory>
7#include <optional>
8#include <string>
9
10#include "frontends/common/options.h"
11#include "ir/ir.h"
12#include "lib/compile_context.h"
13
14#include "backends/p4tools/modules/testgen/core/target.h"
15#include "backends/p4tools/modules/testgen/register.h"
16#include "backends/p4tools/modules/testgen/toolname.h"
17
18namespace P4::P4Tools::Test {
19
22 public:
24 static std::optional<const P4ToolsTestCase> create(std::string deviceName, std::string archName,
25 CompilerOptions::FrontendVersion langVersion,
26 const std::string &source);
27
29 static std::optional<const P4ToolsTestCase> create_14(std::string deviceName,
30 std::string archName,
31 const std::string &source);
32
34 static std::optional<const P4ToolsTestCase> create_16(std::string deviceName,
35 std::string archName,
36 const std::string &source);
37
38 explicit P4ToolsTestCase(const P4Tools::CompilerResult &compilerResults);
39
41 [[nodiscard]] const IR::P4Program &getProgram() const;
42
45 [[nodiscard]] const P4Tools::CompilerResult &getCompilerResult() const;
46
47 private:
49 std::reference_wrapper<const P4Tools::CompilerResult> compilerResults;
50
52 static void ensureInit();
53};
54
55class P4TestgenTest : public testing::Test {
56 public:
57 [[nodiscard]] static std::unique_ptr<AutoCompileContext> SetUp(std::string_view target,
58 std::string_view archName) {
59 P4Tools::P4Testgen::registerTestgenTargets();
62 auto ctxOpt = P4Testgen::TestgenTarget::initializeTarget(P4Tools::P4Testgen::TOOL_NAME,
63 target, archName);
64
65 if (!ctxOpt.has_value()) {
66 return nullptr;
67 }
68 return std::make_unique<AutoCompileContext>(ctxOpt.value());
69 }
70};
71
74 public:
75 const IR::SymbolicVariable *preorder(IR::Member *member) override;
76};
77
78} // namespace P4::P4Tools::Test
79
80#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_ */
Definition common/compiler/compiler_result.h:14
static std::optional< ICompileContext * > initializeTarget(std::string_view toolName, const std::vector< const char * > &args)
Definition p4tools/common/core/target.cpp:70
Definition test/gtest_utils.h:55
static std::unique_ptr< AutoCompileContext > SetUp(std::string_view target, std::string_view archName)
Definition test/gtest_utils.h:57
Encapsulates functionality for running the front- and mid-ends of the compiler in a test.
Definition test/gtest_utils.h:21
const IR::P4Program & getProgram() const
Definition test/gtest_utils.cpp:50
const P4Tools::CompilerResult & getCompilerResult() const
Definition test/gtest_utils.cpp:54
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:64
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:23
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:58
Converts IR::Member into symbolic variables.
Definition test/gtest_utils.h:73
Definition visitor.h:424