From 326decd3deadf7fea7e7daaa00d8b48946607136 Mon Sep 17 00:00:00 2001 From: Erki Date: Sun, 7 Mar 2021 15:32:02 +0200 Subject: [PATCH] Unit tests for basic operation --- op-finder-tests/CMakeLists.txt | 2 +- op-finder-tests/basic_operations.cpp | 46 +++++++++++++++++++ op-finder-tests/fixtures/RunOnCodeFixture.cpp | 32 +++++++++++++ op-finder-tests/fixtures/RunOnCodeFixture.hpp | 25 ++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 op-finder-tests/fixtures/RunOnCodeFixture.cpp create mode 100644 op-finder-tests/fixtures/RunOnCodeFixture.hpp diff --git a/op-finder-tests/CMakeLists.txt b/op-finder-tests/CMakeLists.txt index 1d4f5c0..ccf5f02 100644 --- a/op-finder-tests/CMakeLists.txt +++ b/op-finder-tests/CMakeLists.txt @@ -15,7 +15,7 @@ if (NOT LLVM_ENABLE_RTTI) endif () 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 PUBLIC diff --git a/op-finder-tests/basic_operations.cpp b/op-finder-tests/basic_operations.cpp index 9f63613..75e71c6 100644 --- a/op-finder-tests/basic_operations.cpp +++ b/op-finder-tests/basic_operations.cpp @@ -4,4 +4,50 @@ #include +#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"); +} diff --git a/op-finder-tests/fixtures/RunOnCodeFixture.cpp b/op-finder-tests/fixtures/RunOnCodeFixture.cpp new file mode 100644 index 0000000..2af9256 --- /dev/null +++ b/op-finder-tests/fixtures/RunOnCodeFixture.cpp @@ -0,0 +1,32 @@ +// +// Created by erki on 07.03.21. +// + +#include "RunOnCodeFixture.hpp" + +#include + +#include +#include + +RunOnCodeFixture::RunOnCodeFixture() + : finder(&storage) + , action(&finder) +{ } + +const std::vector& 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); +} diff --git a/op-finder-tests/fixtures/RunOnCodeFixture.hpp b/op-finder-tests/fixtures/RunOnCodeFixture.hpp new file mode 100644 index 0000000..7abd514 --- /dev/null +++ b/op-finder-tests/fixtures/RunOnCodeFixture.hpp @@ -0,0 +1,25 @@ +// +// Created by erki on 07.03.21. +// + +#ifndef C_ANALYZER_RUNONCODEFIXTURE_HPP +#define C_ANALYZER_RUNONCODEFIXTURE_HPP + +#include +#include +#include + +struct RunOnCodeFixture +{ + constexpr static char INPUT_FILE[] = "input.c"; + + OperationStorage storage; + OperationFinder finder; + OperationFinderAstAction action; + + RunOnCodeFixture(); + + const std::vector& operator()(const std::string& code); +}; + +#endif //C_ANALYZER_RUNONCODEFIXTURE_HPP