Unit tests for basic operation
This commit is contained in:
parent
7b294045bf
commit
326decd3de
@ -15,7 +15,7 @@ if (NOT LLVM_ENABLE_RTTI)
|
|||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
add_executable(op-finder-tests
|
add_executable(op-finder-tests
|
||||||
main.cpp basic_operations.cpp)
|
main.cpp basic_operations.cpp fixtures/RunOnCodeFixture.cpp fixtures/RunOnCodeFixture.hpp)
|
||||||
|
|
||||||
target_link_libraries(op-finder-tests
|
target_link_libraries(op-finder-tests
|
||||||
PUBLIC
|
PUBLIC
|
||||||
|
|||||||
@ -4,4 +4,50 @@
|
|||||||
|
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include "fixtures/RunOnCodeFixture.hpp"
|
||||||
|
|
||||||
|
TEST_CASE("Finds binary operations", "[basic_operation]")
|
||||||
|
{
|
||||||
|
auto binary_operand = GENERATE(std::string("="), std::string("+"), std::string("-"), std::string("/"),
|
||||||
|
std::string("*"), std::string("<<"), std::string(">>"),
|
||||||
|
std::string("^"), std::string("=="), std::string("|"), std::string("&"));
|
||||||
|
RunOnCodeFixture fixture;
|
||||||
|
|
||||||
|
const std::string code = "int main() { int a; (void)(a " + binary_operand + " 4); }";
|
||||||
|
const auto& operations = fixture(code);
|
||||||
|
|
||||||
|
REQUIRE(operations.size() == 1);
|
||||||
|
|
||||||
|
const OperationLog& log = operations.front();
|
||||||
|
|
||||||
|
REQUIRE(log.entry_type == OperationLog::BasicOperation::TYPE_NAME);
|
||||||
|
|
||||||
|
const OperationLog::BasicOperation* op = (OperationLog::BasicOperation*)(log.entry.get());
|
||||||
|
|
||||||
|
REQUIRE(op->operation_name == binary_operand);
|
||||||
|
REQUIRE(op->type_lhs == "int");
|
||||||
|
REQUIRE(op->type_rhs == "int");
|
||||||
|
REQUIRE(op->type_result == "int");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Find unary operations", "[basic_operation]")
|
||||||
|
{
|
||||||
|
auto unary_operand = GENERATE(std::string("++"), std::string("--"), std::string("~"), std::string("!"));
|
||||||
|
RunOnCodeFixture fixture;
|
||||||
|
|
||||||
|
const std::string code = "int main() { int a; (void)(" + unary_operand + "a); }";
|
||||||
|
const auto& operations = fixture(code);
|
||||||
|
|
||||||
|
REQUIRE(operations.size() == 1);
|
||||||
|
|
||||||
|
const OperationLog& log = operations.front();
|
||||||
|
|
||||||
|
REQUIRE(log.entry_type == OperationLog::BasicOperation::TYPE_NAME);
|
||||||
|
|
||||||
|
const OperationLog::BasicOperation* op = (OperationLog::BasicOperation*)(log.entry.get());
|
||||||
|
|
||||||
|
REQUIRE(op->operation_name == unary_operand);
|
||||||
|
REQUIRE(op->type_lhs == "int");
|
||||||
|
REQUIRE(op->type_rhs.empty());
|
||||||
|
REQUIRE(op->type_result == "int");
|
||||||
|
}
|
||||||
|
|||||||
32
op-finder-tests/fixtures/RunOnCodeFixture.cpp
Normal file
32
op-finder-tests/fixtures/RunOnCodeFixture.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 07.03.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "RunOnCodeFixture.hpp"
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include <clang/Tooling/Tooling.h>
|
||||||
|
#include <clang/Frontend/FrontendActions.h>
|
||||||
|
|
||||||
|
RunOnCodeFixture::RunOnCodeFixture()
|
||||||
|
: finder(&storage)
|
||||||
|
, action(&finder)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
const std::vector<OperationLog>& RunOnCodeFixture::operator()(const std::string& code)
|
||||||
|
{
|
||||||
|
const bool success = clang::tooling::runToolOnCode(
|
||||||
|
clang::tooling::newFrontendActionFactory(&action)->create(),
|
||||||
|
code,
|
||||||
|
RunOnCodeFixture::INPUT_FILE
|
||||||
|
);
|
||||||
|
|
||||||
|
REQUIRE(success);
|
||||||
|
|
||||||
|
const auto& operations = storage.getOperations();
|
||||||
|
|
||||||
|
REQUIRE(operations.count(RunOnCodeFixture::INPUT_FILE) == 1);
|
||||||
|
|
||||||
|
return operations.at(RunOnCodeFixture::INPUT_FILE);
|
||||||
|
}
|
||||||
25
op-finder-tests/fixtures/RunOnCodeFixture.hpp
Normal file
25
op-finder-tests/fixtures/RunOnCodeFixture.hpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 07.03.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef C_ANALYZER_RUNONCODEFIXTURE_HPP
|
||||||
|
#define C_ANALYZER_RUNONCODEFIXTURE_HPP
|
||||||
|
|
||||||
|
#include <OperationStorage.hpp>
|
||||||
|
#include <OperationFinder.hpp>
|
||||||
|
#include <OperationFinderAstAction.hpp>
|
||||||
|
|
||||||
|
struct RunOnCodeFixture
|
||||||
|
{
|
||||||
|
constexpr static char INPUT_FILE[] = "input.c";
|
||||||
|
|
||||||
|
OperationStorage storage;
|
||||||
|
OperationFinder finder;
|
||||||
|
OperationFinderAstAction action;
|
||||||
|
|
||||||
|
RunOnCodeFixture();
|
||||||
|
|
||||||
|
const std::vector<OperationLog>& operator()(const std::string& code);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //C_ANALYZER_RUNONCODEFIXTURE_HPP
|
||||||
Loading…
x
Reference in New Issue
Block a user