diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2e09aee48cb59acd13aecf8057b8db42ed1598dc..48856c05eb60139334a2f09e704bc4b64cb98522 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -39,6 +39,7 @@ add_executable (unit_tests test_BiCGStab.cpp test_CFunctionEmbedder.cpp test_CFunctionEmbedderTable.cpp + test_CFunctionProcessor.cpp test_CMathModule.cpp test_ContinueProcessor.cpp test_ConcatExpressionProcessor.cpp diff --git a/tests/test_CFunctionProcessor.cpp b/tests/test_CFunctionProcessor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23034e555b0f5f90fe61007e9ba600af1a13a590 --- /dev/null +++ b/tests/test_CFunctionProcessor.cpp @@ -0,0 +1,86 @@ +#include <catch2/catch.hpp> + +#include <ASTNodeValueBuilder.hpp> + +#include <ASTBuilder.hpp> +#include <ASTNodeDataTypeBuilder.hpp> + +#include <ASTNodeDeclarationToAffectationConverter.hpp> +#include <ASTNodeTypeCleaner.hpp> + +#include <ASTModulesImporter.hpp> + +#include <ASTNodeExpressionBuilder.hpp> + +#include <ASTNodeAffectationExpressionBuilder.hpp> + +#include <ASTSymbolTableBuilder.hpp> + +#include <ASTPrinter.hpp> + +#include <Demangle.hpp> + +#include <PEGGrammar.hpp> + +#include <sstream> + +#define CHECK_CFUNCTION_EVALUATION_RESULT(data, variable_name, expected_value) \ + { \ + string_input input{data, "test.pgs"}; \ + auto ast = ASTBuilder::build(input); \ + \ + ASTModulesImporter{*ast}; \ + ASTNodeTypeCleaner<language::import_instruction>{*ast}; \ + \ + ASTSymbolTableBuilder{*ast}; \ + ASTNodeDataTypeBuilder{*ast}; \ + ASTNodeValueBuilder{*ast}; \ + \ + ASTNodeDeclarationToAffectationConverter{*ast}; \ + ASTNodeTypeCleaner<language::declaration>{*ast}; \ + ASTNodeTypeCleaner<language::let_declaration>{*ast}; \ + \ + ASTNodeExpressionBuilder{*ast}; \ + ExecUntilBreakOrContinue exec_policy; \ + ast->execute(exec_policy); \ + \ + auto symbol_table = ast->m_symbol_table; \ + \ + using namespace TAO_PEGTL_NAMESPACE; \ + position use_position{internal::iterator{"fixture"}, "fixture"}; \ + use_position.byte = 10000; \ + auto [symbol, found] = symbol_table->find(variable_name, use_position); \ + \ + auto attributes = symbol->attributes(); \ + auto value = std::get<decltype(expected_value)>(attributes.value()); \ + \ + REQUIRE(value == expected_value); \ + } + +TEST_CASE("CFunctionProcessor", "[language]") +{ + SECTION("math module functions") + { + // @note HERE we do not use SECTION to be able to count tests and to check + // that all math functions are actually tested + + std::set<std::string> tested_function_set; + { // sqrt + tested_function_set.insert("sqrt"); + std::string_view data = R"( +import math; +R x = sqrt(4); +)"; + CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::sqrt(4l)}); + } + + { // sin + tested_function_set.insert("sin"); + std::string_view data = R"( +import math; +R x = sin(1.3); +)"; + CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::sin(1.3)}); + } + } +}