From 6675ff79316b6df68f0ca65eeeaca5d54037528b Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Tue, 5 Nov 2019 19:18:06 +0100
Subject: [PATCH] Begin CFunctionProcessor tests

---
 tests/CMakeLists.txt              |  1 +
 tests/test_CFunctionProcessor.cpp | 86 +++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 tests/test_CFunctionProcessor.cpp

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 2e09aee48..48856c05e 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 000000000..23034e555
--- /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)});
+    }
+  }
+}
-- 
GitLab