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