]> www.ginac.de Git - ginac.git/blob - check/parser_bugs.cpp
Do not expand pre-factored polynomials.
[ginac.git] / check / parser_bugs.cpp
1 /** @file parser_bugs.cpp
2  *
3  *  Check for some silly bugs in the parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2017 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "ginac.h"
24 using namespace GiNaC;
25
26 #include <iostream>
27 #include <sstream>
28 #include <stdexcept>
29 #include <string>
30
31 // - a - b was misparsed as -a + b due to a bug in parser::parse_unary_expr()
32 static int check1(std::ostream& err_str)
33 {
34         const std::string srep("-a-b");
35         parser reader;
36         ex e = reader(srep);
37         ex a = reader.get_syms()["a"];
38         ex b = reader.get_syms()["b"];
39         ex g = - a - b;
40         ex d = (e - g).expand();
41         if (!d.is_zero()) {
42                 err_str << "\"" << srep << "\" was misparsed as \""
43                         << e << "\"" << std::endl;
44                 return 1;
45         }
46         return 0;
47 }
48
49 /// Parser was rejecting the valid expression '5 - (3*x)/10'.
50 static int check2(std::ostream& err_str)
51 {
52         const std::string srep("5-(3*x)/10");
53         parser reader;
54         ex e = reader(srep);
55         ex x = reader.get_syms()["x"];
56         ex g = 5 - (3*x)/10;
57         ex d = (e - g).expand();
58         if (!d.is_zero()) {
59                 err_str << "\"" << srep << "\" was misparsed as \""
60                         << e << "\"" << std::endl;
61                 return 1;
62         }
63         return 0;
64 }
65
66 /// parse_literal_expr forget to consume the token, so parser get
67 /// very confused.
68 static int check3(std::ostream& err_str)
69 {
70         const std::string srep("5-(2*I)/3");
71         parser reader;
72         ex e = reader(srep);
73         ex g = numeric(5) - (numeric(2)*I)/3;
74         ex d = (e - g).expand();
75         if (!d.is_zero()) {
76                 err_str << "\"" << srep << "\" was misparsed as \""
77                         << e << "\"" << std::endl;
78                 return 1;
79         }
80         return 0;
81 }
82
83 /// parser happily accepted various junk like 'x^2()+1'
84 static int check4(std::ostream& err_str)
85 {
86         const std::string junk("x^2()+1");
87         parser reader;
88         ex e;
89         try {
90                 e = reader(junk);
91                 err_str << "parser accepts junk: \"" << junk << "\"" << std::endl;
92                 return 1;
93         } catch (parse_error& err) {
94                 // Ok, parser rejects the nonsense.
95                 return 0;
96         }
97 }
98
99 int main(int argc, char** argv)
100 {
101         std::cout << "checking for parser bugs. " << std::flush;
102         std::ostringstream err_str;
103         int errors = 0;
104         errors += check1(err_str);
105         errors += check2(err_str);
106         errors += check3(err_str);
107         errors += check4(err_str);
108         if (errors) {
109                 std::cout << "Yes, unfortunately:" << std::endl;
110                 std::cout << err_str.str();
111         } else {
112                 std::cout << "Not found. ";
113         }
114         return errors;
115 }