File: TestSingleExprParse.cpp

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (120 lines) | stat: -rw-r--r-- 4,560 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2009- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation
 * nor does it submit to any jurisdiction.
 */

#include <fstream>
#include <iostream>
#include <map>
#include <string>

#include <boost/test/unit_test.hpp>

#include "ecflow/node/ExprAst.hpp"
#include "ecflow/node/ExprDuplicate.hpp"
#include "ecflow/node/ExprParser.hpp"
#include "ecflow/node/formatter/DefsWriter.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace std;

// DEBUG AID: to see the expression tree, invert the expected evaluation
//            so that test fail's

BOOST_AUTO_TEST_SUITE(U_Node)

BOOST_AUTO_TEST_SUITE(T_SingleExprParse)

BOOST_AUTO_TEST_CASE(test_single_expression) {
    ECF_NAME_THIS_TEST();

    // Duplicate AST are held in a static map. Delete them, to avoid ASAN complaining
    ExprDuplicate reclaim_cloned_ast_memory;

    // The map key = trigger expression,
    // value.first  = type of expected root abstract syntax tree
    // value.second = result of expected evaluation
    map<string, std::pair<string, bool>> exprMap;

    exprMap[":var == 0"] = std::make_pair(AstEqual::stype(), true);
    exprMap[":var != 1"] = std::make_pair(AstNotEqual::stype(), true);

    for (std::pair<string, std::pair<string, bool>> p : exprMap) {

        ExprParser theExprParser(p.first);
        std::string errorMsg;
        bool ok = theExprParser.doParse(errorMsg);
        BOOST_REQUIRE_MESSAGE(ok, errorMsg);

        string expectedRootType       = p.second.first;
        bool expectedEvaluationResult = p.second.second;

        Ast* top = theExprParser.getAst();
        BOOST_REQUIRE_MESSAGE(top, "No abstract syntax tree");
        BOOST_REQUIRE_MESSAGE(top->left(), "No root created");
        BOOST_REQUIRE_MESSAGE(top->left()->isRoot() || top->left()->is_attribute(),
                              "First child of top should be a root or attribute " + p.first);
        BOOST_REQUIRE_MESSAGE(top->left()->is_evaluateable(),
                              "expected ast to be evaluatable. found: " << top->left()->type() << " " << p.first);
        BOOST_REQUIRE_MESSAGE(top->left()->type() == expectedRootType || top->left()->type() == "variable",
                              "expected root type " << expectedRootType << " or 'variable' but found "
                                                    << top->left()->type() << " " << p.first);
        {
            std::stringstream ss;
            top->print_flat(ss);
            std::string print_flat = ss.str();
            cout << print_flat << "\n";
            BOOST_REQUIRE_MESSAGE(expectedEvaluationResult == top->evaluate(),
                                  "evaluation not as expected for:\n"
                                      << p.first << "\n"
                                      << print_flat << "\n"
                                      << ecf::as_string(*top, PrintStyle::DEFS));
        }
        cout << ecf::as_string(*top, PrintStyle::DEFS) << "\n";

        std::string why;
        top->why(why);
        cout << "why: " << why << "\n";
    }
}

// BOOST_AUTO_TEST_CASE( test_expression_read_from_file )
//{
//    ECF_NAME_THIS_TEST();
//
//    std::string filename = "${ECF_TEST_DEFS_DIR}/triggers.list";
//    std::ifstream the_file(filename.c_str(),std::ios_base::in);
//    BOOST_REQUIRE_MESSAGE(the_file,"file " << filename << "not found");
//
//    int  i = 0;
//    string line;
//    while ( std::getline(the_file,line) ) {
//       i++;
//       // cout << i << ": " << line << "\n";
//       ExprParser theExprParser(line);
//       std::string errorMsg;
//       bool ok = theExprParser.doParse(errorMsg);
//       BOOST_CHECK_MESSAGE(ok,errorMsg << " line: " << i );
//
//       if (ok) {
//          Ast* top = theExprParser.getAst();
//          BOOST_REQUIRE_MESSAGE( top ,"No abstract syntax tree " + line);
//          BOOST_REQUIRE_MESSAGE( top->left() ,"No root created " + line);
//          BOOST_REQUIRE_MESSAGE( top->left()->isRoot() || top->left()->is_attribute() ,"First child of top should be a
//          root or variable " + line);
//          //top->print_flat(ss);
//       }
//    }
//
//    ExprDuplicate reclaim_cloned_ast_memory;
//    cout << " read " << i << " expressions\n";
// }

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()