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