Files
revng-revng/tests/unit/IRHelpers.cpp
T
Pietro Fezzardi 6d5699c174 Unit Tests: replace revng_assert with revng_check
Calls to `revng_assert` are removed when compiling in release, calls to
`revng_check` are not.
This replacemente makes tests more robust in release.
2020-02-26 18:35:59 +01:00

100 lines
2.4 KiB
C++

/// \file IRHelpers.cpp
/// \brief Tests for IRHelpers
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// Boost includes
#define BOOST_TEST_MODULE IRHelpers
bool init_unit_test();
#include <boost/test/unit_test.hpp>
// Local libraries includes
#include "revng/Support/IRHelpers.h"
#include "revng/UnitTestHelpers/LLVMTestHelpers.h"
#include "revng/UnitTestHelpers/UnitTestHelpers.h"
using namespace llvm;
const char *VisitorTestBody = R"LLVM(
%a = add i64 0, 0
br i1 true, label %first_if_true, label %first_if_false
first_if_true:
%b = add i64 0, 0
br label %center
first_if_false:
%c = add i64 0, 0
br label %center
center:
%d = add i64 0, 0
%target = add i64 0, 0
br i1 true, label %second_if_true, label %second_if_false
second_if_true:
%e = add i64 0, 0
br label %end
second_if_false:
%f = add i64 0, 0
br label %end
end:
%g = add i64 0, 0
ret void
)LLVM";
BOOST_AUTO_TEST_CASE(TestBackwardBFSVisitor) {
struct Visitor : public BackwardBFSVisitor<Visitor> {
std::vector<std::string> VisitLog;
VisitAction visit(instruction_range Range) {
for (Instruction &I : Range)
VisitLog.push_back(getName(&I));
return Continue;
}
};
LLVMContext TestContext;
std::unique_ptr<Module> M = loadModule(TestContext, VisitorTestBody);
Function *F = M->getFunction("main");
Visitor V;
V.run(instructionByName(F, "target"));
const std::vector<std::string> GroundTruth = { "target", "d",
"first_if_false:2", "c",
"first_if_true:2", "b",
"initial_block:2", "a" };
revng_check(V.VisitLog == GroundTruth);
}
BOOST_AUTO_TEST_CASE(TestForwardBFSVisitor) {
struct Visitor : public ForwardBFSVisitor<Visitor> {
std::vector<std::string> VisitLog;
VisitAction visit(instruction_range Range) {
for (Instruction &I : Range)
VisitLog.push_back(getName(&I));
return Continue;
}
};
LLVMContext TestContext;
std::unique_ptr<Module> M = loadModule(TestContext, VisitorTestBody);
Function *F = M->getFunction("main");
Visitor V;
V.run(instructionByName(F, "target"));
const std::vector<std::string> GroundTruth = {
"center:3", "e", "second_if_true:2", "f", "second_if_false:2", "g", "end:2"
};
revng_check(V.VisitLog == GroundTruth);
}