]> www.ginac.de Git - ginac.git/blob - check/parser_bugs.cpp
registered_class_info: use typeid() instead of tinfo_static.
[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 /// parse_literal_expr forget to consume the token, so parser get
45 /// very confused.
46 static int check3(std::ostream& err_str)
47 {
48         const std::string srep("5-(2*I)/3");
49         parser reader;
50         ex e = reader(srep);
51         ex g = numeric(5) - (numeric(2)*I)/3;
52         ex d = (e - g).expand();
53         if (!d.is_zero()) {
54                 err_str << "\"" << srep << "\" was misparsed as \""
55                         << e << "\"" << std::endl;
56                 return 1;
57         }
58         return 0;
59 }
60
61 /// parser happily accepted various junk like 'x^2()+1'
62 static int check4(std::ostream& err_str)
63 {
64         const std::string junk("x^2()+1");
65         parser reader;
66         ex e;
67         try {
68                 e = reader(junk);
69                 err_str << "parser accepts junk: \"" << junk << "\"" << std::endl;
70                 return 1;
71         } catch (parse_error& err) {
72                 // Ok, parser rejects the nonsense.
73                 return 0;
74         }
75 }
76
77 int main(int argc, char** argv)
78 {
79         std::cout << "checking for parser bugs. " << std::flush;
80         std::ostringstream err_str;
81         int errors = 0;
82         errors += check1(err_str);
83         errors += check2(err_str);
84         errors += check3(err_str);
85         errors += check4(err_str);
86         if (errors) {
87                 std::cout << "Yes, unfortunately:" << std::endl;
88                 std::cout << err_str.str();
89         } else {
90                 std::cout << "Not found. ";
91         }
92         return errors;
93 }
94