1#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_SMALL_STEP_UTIL_H_
2#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_SMALL_STEP_UTIL_H_
4#include <gtest/gtest.h>
13#include "backends/p4tools/common/compiler/compiler_target.h"
14#include "backends/p4tools/common/core/z3_solver.h"
15#include "backends/p4tools/common/lib/namespace_context.h"
16#include "backends/p4tools/common/lib/symbolic_env.h"
17#include "ir/declaration.h"
18#include "ir/indexed_vector.h"
20#include "lib/cstring.h"
21#include "lib/enumerator.h"
23#include "backends/p4tools/modules/testgen/core/small_step/small_step.h"
24#include "backends/p4tools/modules/testgen/core/target.h"
25#include "backends/p4tools/modules/testgen/lib/continuation.h"
26#include "backends/p4tools/modules/testgen/lib/execution_state.h"
27#include "backends/p4tools/modules/testgen/test/gtest_utils.h"
29namespace P4::P4Tools::Test {
31using Body = P4Testgen::Continuation::Body;
32using Continuation = P4Testgen::Continuation;
33using ExecutionState = P4Testgen::ExecutionState;
34using NamespaceContext = NamespaceContext;
35using Return = P4Testgen::Continuation::Return;
36using SmallStepEvaluator = P4Testgen::SmallStepEvaluator;
37using TestgenTarget = P4Testgen::TestgenTarget;
38using Z3Solver = Z3Solver;
46namespace SmallStepUtil {
52const T *extractExpr(
const IR::P4Program &program) {
54 auto *decl = program.getDeclsByName(
"mau"_cs)->single();
58 const auto *control = decl->checkedTo<IR::P4Control>();
59 if (control->body->components.size() != 1) {
64 const auto *mcStmt = control->body->components[0]->to<IR::MethodCallStatement>();
70 const auto *mcArgs = mcStmt->methodCall->arguments;
71 if (mcArgs->size() != 1) {
74 return (*mcArgs)[0]->expression->to<T>();
79void stepAndExamineValue(
const T *value,
const CompilerResult &compilerResult) {
82 ASSERT_TRUE(progInfo);
86 Body bodyBase({Return(v->param)});
87 Continuation continuationBase(v, bodyBase);
92 auto &testState = esBase.clone();
93 Body body({Return(value)});
94 testState.replaceBody(body);
95 testState.pushContinuation(
96 *
new ExecutionState::StackFrame(continuationBase, esBase.getNamespaceContext()));
97 SmallStepEvaluator eval(
solver, *progInfo);
98 auto *successors = eval.step(testState);
99 ASSERT_EQ(successors->size(), 1u);
102 const auto branch = (*successors)[0];
103 const auto *constraint = branch.constraint;
104 auto executionState = branch.nextState;
105 ASSERT_TRUE(constraint->checkedTo<IR::BoolLiteral>()->value);
107 ASSERT_TRUE(executionState.get().getSymbolicEnv().getInternalMap().empty());
110 Body finalBody = executionState.get().getBody();
111 ASSERT_EQ(finalBody, Body({Return(value)}));
114 ASSERT_EQ(executionState.get().getStack().empty(),
true);
122void stepAndExamineOp(
123 const T *op,
const IR::Expression *subexpr,
const CompilerResult &compilerResult,
124 std::function<
const IR::Expression *(
const IR::PathExpression *)> rebuildNode) {
127 ASSERT_TRUE(progInfo);
131 Body body({Return(op)});
133 SmallStepEvaluator eval(
solver, *progInfo);
134 auto *successors = eval.step(es);
135 ASSERT_EQ(successors->size(), 1U);
138 const auto branch = (*successors)[0];
139 const auto *constraint = branch.constraint;
140 auto executionState = branch.nextState;
141 ASSERT_TRUE(constraint->checkedTo<IR::BoolLiteral>()->value);
143 ASSERT_TRUE(executionState.get().getSymbolicEnv().getInternalMap().empty());
146 Body finalBody = executionState.get().getBody();
147 ASSERT_EQ(finalBody, Body({Return(subexpr)}));
150 ASSERT_EQ(executionState.get().getStack().size(), 1u);
151 const auto stackFrame = executionState.get().getStack().top();
152 ASSERT_TRUE(stackFrame.get().getExceptionHandlers().empty());
156 Continuation pushedContinuation = stackFrame.get().getContinuation();
157 ASSERT_TRUE(pushedContinuation.parameterOpt);
158 Body pushedBody = pushedContinuation.body;
159 ASSERT_EQ(pushedBody, Body({Return(rebuildNode(*pushedContinuation.parameterOpt))}));
static const NamespaceContext * Empty
Represents the empty namespace context.
Definition namespace_context.h:33
A continuation body is a list of commands.
Definition continuation.h:129
Definition phv/solver/action_constraint_solver.cpp:33