Skip to content
Snippets Groups Projects
Select Git revision
  • 54a389acab48ad5e2ef9f37c2b7323a6771cd127
  • develop default protected
  • origin/stage/bouguettaia
  • feature/kinetic-schemes
  • feature/reconstruction
  • feature/local-dt-fsi
  • feature/composite-scheme-sources
  • feature/composite-scheme-other-fluxes
  • feature/serraille
  • feature/variational-hydro
  • feature/composite-scheme
  • hyperplastic
  • feature/polynomials
  • feature/gks
  • feature/implicit-solver-o2
  • feature/coupling_module
  • feature/implicit-solver
  • feature/merge-local-dt-fsi
  • master protected
  • feature/escobar-smoother
  • feature/hypoelasticity-clean
  • v0.5.0 protected
  • v0.4.1 protected
  • v0.4.0 protected
  • v0.3.0 protected
  • v0.2.0 protected
  • v0.1.0 protected
  • Kidder
  • v0.0.4 protected
  • v0.0.3 protected
  • v0.0.2 protected
  • v0 protected
  • v0.0.1 protected
33 results

test_WhileProcessor.cpp

Blame
    • Stéphane Del Pino's avatar
      54a389ac
      Take into account suggestions of Emmanuel · 54a389ac
      Stéphane Del Pino authored
      This includes an important change in the code since one cannot
      reuse a variable name in a nested block (this means that if a variable
      is declared, one cannot declare a variable with a same name even in a
      nested block, while the first variable exists).
      54a389ac
      History
      Take into account suggestions of Emmanuel
      Stéphane Del Pino authored
      This includes an important change in the code since one cannot
      reuse a variable name in a nested block (this means that if a variable
      is declared, one cannot declare a variable with a same name even in a
      nested block, while the first variable exists).
    test_WhileProcessor.cpp 4.66 KiB
    #include <catch2/catch_test_macros.hpp>
    #include <catch2/matchers/catch_matchers_all.hpp>
    
    #include <language/ast/ASTBuilder.hpp>
    #include <language/ast/ASTModulesImporter.hpp>
    #include <language/ast/ASTNodeAffectationExpressionBuilder.hpp>
    #include <language/ast/ASTNodeDataTypeBuilder.hpp>
    #include <language/ast/ASTNodeDeclarationToAffectationConverter.hpp>
    #include <language/ast/ASTNodeExpressionBuilder.hpp>
    #include <language/ast/ASTNodeTypeCleaner.hpp>
    #include <language/ast/ASTSymbolTableBuilder.hpp>
    #include <utils/Demangle.hpp>
    
    #include <pegtl/string_input.hpp>
    
    #include <sstream>
    
    #define CHECK_WHILE_PROCESSOR_RESULT(data, variable_name, expected_value)     \
      {                                                                           \
        TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"};                \
        auto ast = ASTBuilder::build(input);                                      \
                                                                                  \
        ASTModulesImporter{*ast};                                                 \
        ASTNodeTypeCleaner<language::import_instruction>{*ast};                   \
                                                                                  \
        ASTSymbolTableBuilder{*ast};                                              \
        ASTNodeDataTypeBuilder{*ast};                                             \
                                                                                  \
        ASTNodeDeclarationToAffectationConverter{*ast};                           \
        ASTNodeTypeCleaner<language::var_declaration>{*ast};                      \
                                                                                  \
        ASTNodeExpressionBuilder{*ast};                                           \
        ExecutionPolicy 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);                                         \
      }
    
    #define CHECK_WHILE_PROCESSOR_THROWS_WITH(data, error_message)        \
      {                                                                   \
        TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"};        \
        auto ast = ASTBuilder::build(input);                              \
                                                                          \
        ASTSymbolTableBuilder{*ast};                                      \
                                                                          \
        REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast}, error_message); \
      }
    
    // clazy:excludeall=non-pod-global-static
    
    TEST_CASE("WhileProcessor", "[language]")
    {
      SECTION("simple loop")
      {
        std::string_view data = R"(
    let i:N, i = 3;
    let j:N, j = 0;
    while(i<10) {
      j++;
      i += j;
    }
    )";
        CHECK_WHILE_PROCESSOR_RESULT(data, "i", 13ul);
      }
    
      SECTION("simple with break")
      {
        std::string_view data = R"(
    let i:N, i = 3;
    let j:N, j = 0;
    while(i<10) {
      j++;
      if (j==2) break;
      i += j;
    }
    )";
        CHECK_WHILE_PROCESSOR_RESULT(data, "i", 4ul);
      }
    
      SECTION("simple with continue")
      {
        std::string_view data = R"(
    let i:N, i = 3;
    let j:N, j = 0;
    while(i<10) {
      j++;
      if (j<=3) continue;
      i += j;
    }
    )";
        CHECK_WHILE_PROCESSOR_RESULT(data, "i", 12ul);
      }
    
      SECTION("while symbol table untouched")
      {
        std::string_view data = R"(
    let i:N, i = 3;
    while(i != 3);
    )";
        CHECK_WHILE_PROCESSOR_RESULT(data, "i", 3ul);
      }
    
      SECTION("single instruction while symbol table untouched")
      {
        std::string_view data = R"(
    let i:N, i = 3;
    while (i == 3)
      i = 2;
    )";
        CHECK_WHILE_PROCESSOR_RESULT(data, "i", 2ul);
      }
    
      SECTION("errors")
      {
        SECTION("bad test type")
        {
          std::string_view data = R"(
    while(1) {
    }
    )";
    
          CHECK_WHILE_PROCESSOR_THROWS_WITH(data, "invalid implicit conversion: Z -> B");
        }
      }
    }