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