]> www.ginac.de Git - ginac.git/blob - check/paranoia_check.cpp
Fixed 50 percent of the bugs on Alex' list. More, soon...
[ginac.git] / check / paranoia_check.cpp
1 /** @file paranoia_check.cpp
2  *
3  *  This set of tests checks for some of GiNaC's oopses which showed up during
4  *  development.  Things were evaluated wrongly and so.  It should not find such
5  *  a sick behaviour again.  But since we are paranoic and we want to exclude
6  *  that behaviour for good...
7  *
8  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include <ginac/ginac.h>
26
27 // The very first pair of historic problems had its roots in power.cpp and was
28 // finally resolved on April 27th. (Fixing the first on April 23rd actually
29 // introduced the second.)
30 static unsigned paranoia_check1(void)
31 {
32     unsigned result = 0;
33     symbol x("x"), y("y"), z("z");
34     ex e, f, g;
35
36     e = x * y * z;
37     f = y * z;
38     g = e / f;
39
40     // In the first one expand did not do any job at all:
41     if ( !g.expand().is_equal(x) ) {
42         clog << "e = x*y*z; f = y*z; expand(e/f) erroneously returned "
43              << g.expand() << endl;
44         ++result;
45     }
46
47     // This one somehow used to return 0:
48     e = pow(x + 1, -1);
49     if (!e.expand().is_equal(e)) {
50         clog << "expand(pow(x + 1, -1)) erroneously returned "
51              << e.expand() << endl;
52         ++result;
53     }
54
55     return result;
56 }
57
58 // And here the second oops which showed up until May 17th 1999.  It had to do
59 // with lexicographic canonicalization and thus showed up only if the variables
60 // had the names as given here:
61 static unsigned paranoia_check2(void)
62 {
63     unsigned result = 0;
64     symbol x("x"), y("y"), z("z");
65     ex e, f, g;
66
67     e = x + z*x;
68     f = e*y;
69     g = f - e*y;
70
71     // After .expand(), g should be zero:
72     if (!g.expand().is_equal(exZERO())) {
73         clog << "e = (x + z*x); f = e*y; expand(f - e*y) erroneously returned "
74              << g.expand() << endl;
75         ++result;
76     }
77     // After .eval(), g should be zero:
78     if (!g.eval().is_equal(exZERO())) {
79         clog << "e = (x + z*x); f = e*y; eval(f - e*y) erroneously returned "
80              << g.eval() << endl;
81         ++result;
82     }
83     // This actually worked already back in April.  But we are very paranoic!
84     if (!g.expand().eval().is_equal(exZERO())) {
85         clog << "e = (x + z*x); f = e*y; eval(expand(f - e*y)) erroneously returned "
86              << g.expand().eval() << endl;
87         ++result;
88     }
89
90     return result;
91 }
92
93 // The third bug was introduced on May 18, discovered on May 19 and fixed that
94 // same day.  It worked when x was substituted by 1 but not with other numbers:
95 static unsigned paranoia_check3(void)
96 {
97     unsigned result = 0;
98     symbol x("x"), y("y");
99     ex e, f;
100
101     e = x*y - y;
102     f = e.subs(x == 2);
103
104     if (!f.is_equal(y)) {
105         clog << "e = x*y - y; f = e.subs(x == 2) erroneously returned "
106              << f << endl;
107         ++result;
108     }
109     if (!f.eval().is_equal(y)) {
110         clog << "e = x*y - y; eval(e.subs(x == 2)) erroneously returned "
111              << f.eval() << endl;
112         ++result;
113     }
114     if (!f.expand().is_equal(y)) {
115         clog << "e = x*y - y; expand(e.subs(x == 2)) erroneously returned "
116              << f.expand() << endl;
117         ++result;
118     }
119
120     return result;
121 }
122
123 // The fourth bug was also discovered on May 19 and fixed immediately:
124 static unsigned paranoia_check4(void)
125 {
126     unsigned result = 0;
127     symbol x("x");
128     ex e, f, g;
129
130     e = pow(x, 2) + x + 1;
131     f = pow(x, 2) + x + 1;
132     g = e - f;
133
134     if (!g.is_equal(exZERO())) {
135         clog << "e = pow(x,2) + x + 1; f = pow(x,2) + x + 1; g = e-f; g erroneously returned "
136              << g << endl;
137         ++result;
138     }
139     if (!g.is_equal(exZERO())) {
140         clog << "e = pow(x,2) + x + 1; f = pow(x,2) + x + 1; g = e-f; g.eval() erroneously returned "
141              << g.eval() << endl;
142         ++result;
143     }
144
145     return result;
146 }
147
148 // The fifth oops was discovered on May 20 and fixed a day later:
149 static unsigned paranoia_check5(void)
150 {
151     unsigned result = 0;
152     symbol x("x"), y("y");
153
154     ex e, f;
155     e = pow(x*y + 1, 2);
156     f = pow(x, 2) * pow(y, 2) + 2*x*y + 1;
157
158     if (!(e-f).expand().is_equal(exZERO())) {
159         clog << "e = pow(x*y+1,2); f = pow(x,2)*pow(y,2) + 2*x*y + 1; (e-f).expand() erroneously returned "
160              << (e-f).expand() << endl;
161         ++result;
162     }
163
164     return result;
165 }
166
167 // This one was discovered on Jun 1 and fixed the same day:
168 static unsigned paranoia_check6(void)
169 {
170     unsigned result = 0;
171     symbol x("x");
172
173     ex e, f;
174     e = pow(x, -5);
175     f = e.denom();
176
177     if (!f.is_equal(pow(x, 5))) {
178         clog << "e = pow(x, -5); f = e.denom(); f was " << f << " (should be x^5)" << endl;
179         ++result;
180     }
181     return result;
182 }
183
184 // This one was introduced on June 1 by some aggressive manual optimization.
185 // Discovered and fixed on June 2.
186 static unsigned paranoia_check7(void)
187 {
188     unsigned result = 0;
189     symbol x("x"), y("y");
190
191     ex e = y + y*x + 2;
192     ex f = expand(pow(e, 2) - (e*y*(x + 1)));
193
194     if (f.nops() > 3) {
195         clog << "e=y+y*x+2; f=expand(pow(e,2)-(e*y*(x+1))) has "
196              << f.nops() << " arguments instead of 3 ( f=="
197              << f << " )" << endl;
198         ++result;
199     }
200     return result;
201 }
202
203 // This one was a result of the rewrite of mul::max_coefficient when we
204 // introduced the overall_coefficient field in expairseq objects on Oct 1.
205 // Fixed on Oct 4.
206 static unsigned paranoia_check8(void)
207 {
208     unsigned result = 0;
209     symbol x("x");
210
211     ex e = -x / (x+1);
212     ex f = e.normal();
213
214     // The bug caused a division by zero in normal(), so the following
215     // check is actually bogus...
216     if (!f.is_equal(e)) {
217         clog << "normal(-x/(x+1)) returns " << f << " instead of -x/(x+1)\n";
218         ++result;
219     }
220     return result;
221 }
222
223 unsigned paranoia_check(void)
224 {
225     unsigned result = 0;
226
227     cout << "checking several ex-bugs just out of pure paranoia..." << flush;
228     clog << "---------several ex-bugs just out of pure paranoia:" << endl;
229
230     result += paranoia_check1();
231     result += paranoia_check2();
232     result += paranoia_check3();
233     result += paranoia_check4();
234     result += paranoia_check5();
235     result += paranoia_check6();
236     result += paranoia_check7();
237     result += paranoia_check8();
238
239     if (! result) {
240         cout << " passed ";
241         clog << "(no output)" << endl;
242     } else {
243         cout << " failed ";
244     }
245
246     return result;
247 }