]> www.ginac.de Git - ginac.git/blob - check/parser_bugs.cpp
340fe20fb2cc4da5b038172e0e5d8204e8bbb16c
[ginac.git] / check / parser_bugs.cpp
1 /// @file parser_a_b.cpp Check for some silly bugs in the parser.
2 #include "ginac.h"
3 #include <string>
4 #include <iostream>
5 #include <stdexcept>
6 #include <sstream>
7 using namespace GiNaC;
8
9 // - a - b was misparsed as -a + b due to a bug in parser::parse_unary_expr()
10 static int check1(std::ostream& err_str)
11 {
12         const std::string srep("-a-b");
13         parser reader;
14         ex e = reader(srep);
15         ex a = reader.get_syms()["a"];
16         ex b = reader.get_syms()["b"];
17         ex g = - a - b;
18         ex d = (e - g).expand();
19         if (!d.is_zero()) {
20                 err_str << "\"" << srep << "\" was misparsed as \""
21                         << e << "\"" << std::endl;
22                 return 1;
23         }
24         return 0;
25 }
26
27 /// Parser was rejecting the valid expression '5 - (3*x)/10'.
28 static int check2(std::ostream& err_str)
29 {
30         const std::string srep("5-(3*x)/10");
31         parser reader;
32         ex e = reader(srep);
33         ex x = reader.get_syms()["x"];
34         ex g = 5 - (3*x)/10;
35         ex d = (e - g).expand();
36         if (!d.is_zero()) {
37                 err_str << "\"" << srep << "\" was misparsed as \""
38                         << e << "\"" << std::endl;
39                 return 1;
40         }
41         return 0;
42 }
43
44 int main(int argc, char** argv)
45 {
46         std::cout << "checking for parser bugs. " << std::flush;
47         std::ostringstream err_str;
48         int errors = 0;
49         errors += check1(err_str);
50         errors += check2(err_str);
51         if (errors) {
52                 std::cout << "Yes, unfortunately:" << std::endl;
53                 std::cout << err_str.str();
54         } else {
55                 std::cout << "Not found. ";
56         }
57         return errors;
58 }
59