]> www.ginac.de Git - ginac.git/blob - check/exam_parser.cpp
Clarify types of test suite files.
[ginac.git] / check / exam_parser.cpp
1 /** @file parser_bugs.cpp
2  *
3  *  Check for some silly bugs in the parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2020 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 using namespace std;
32
33 // - a - b was misparsed as -a + b due to a bug in parser::parse_unary_expr()
34 static int check1(ostream& err_str)
35 {
36         const string srep("-a-b");
37         parser reader;
38         ex e = reader(srep);
39         ex a = reader.get_syms()["a"];
40         ex b = reader.get_syms()["b"];
41         ex g = - a - b;
42         ex d = (e - g).expand();
43         if (!d.is_zero()) {
44                 err_str << "\"" << srep << "\" was misparsed as \""
45                         << e << "\"" << endl;
46                 return 1;
47         }
48         return 0;
49 }
50
51 /// Parser was rejecting the valid expression '5 - (3*x)/10'.
52 static int check2(ostream& err_str)
53 {
54         const string srep("5-(3*x)/10");
55         parser reader;
56         ex e = reader(srep);
57         ex x = reader.get_syms()["x"];
58         ex g = 5 - (3*x)/10;
59         ex d = (e - g).expand();
60         if (!d.is_zero()) {
61                 err_str << "\"" << srep << "\" was misparsed as \""
62                         << e << "\"" << endl;
63                 return 1;
64         }
65         return 0;
66 }
67
68 /// parse_literal_expr forget to consume the token, so parser get
69 /// very confused.
70 static int check3(ostream& err_str)
71 {
72         const string srep("5-(2*I)/3");
73         parser reader;
74         ex e = reader(srep);
75         ex g = numeric(5) - (numeric(2)*I)/3;
76         ex d = (e - g).expand();
77         if (!d.is_zero()) {
78                 err_str << "\"" << srep << "\" was misparsed as \""
79                         << e << "\"" << endl;
80                 return 1;
81         }
82         return 0;
83 }
84
85 /// parser happily accepted various junk like 'x^2()+1'
86 static int check4(ostream& err_str)
87 {
88         const string junk("x^2()+1");
89         parser reader;
90         ex e;
91         try {
92                 e = reader(junk);
93                 err_str << "parser accepts junk: \"" << junk << "\"" << endl;
94                 return 1;
95         } catch (parse_error& err) {
96                 // Ok, parser rejects the nonsense.
97                 return 0;
98         }
99 }
100
101 int main(int argc, char** argv)
102 {
103         cout << "examining old parser bugs" << flush;
104         ostringstream err_str;
105         int errors = 0;
106         errors += check1(err_str);  cout << '.' << flush;
107         errors += check2(err_str);  cout << '.' << flush;
108         errors += check3(err_str);  cout << '.' << flush;
109         errors += check4(err_str);  cout << '.' << flush;
110         if (errors) {
111                 cout << "Yes, unfortunately:" << endl;
112                 cout << err_str.str();
113         } else {
114                 cout << "Not found. ";
115         }
116         return errors;
117 }