// // Created by erki on 07.03.21. // #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"); } TEST_CASE("Find subscript operation", "[basic_operation]") { RunOnCodeFixture fixture; const std::string code = "int main() { int a[4]; (void)a[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 == "subscript"); REQUIRE(op->type_lhs == "int *"); REQUIRE(op->type_rhs == "int"); REQUIRE(op->type_result == "int"); } TEST_CASE("Find subscript operation reversed", "[basic_operation]") { RunOnCodeFixture fixture; const std::string code = "int main() { int a[4]; (void)4[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 == "subscript"); REQUIRE(op->type_lhs == "int *"); REQUIRE(op->type_rhs == "int"); REQUIRE(op->type_result == "int"); }