1 /** @file exam_misc.cpp
6 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 static unsigned exam_expand_subs()
33 for (unsigned i=0; i<VECSIZE; ++i)
36 // prepare aux so it will swallow anything but a1^2:
37 aux = -e + a[0] + a[1];
38 e = expand(subs(expand(pow(e, 2)), a[0] == aux));
40 if (e != pow(a[1],2)) {
41 clog << "Denny Fliegner's quick consistency check erroneously returned "
49 /* A simple modification of Denny Fliegner's three step consistency test:
50 * 1) e = (a0 + a1)^200
52 * 3) substitute a0 by -a1 in e
53 * after which e should return 0 (without expanding). */
54 static unsigned exam_expand_subs2()
57 symbol a("a"), b("b");
60 e = pow(a+b,200).expand();
64 clog << "e = pow(a+b,200).expand(); f = e.subs(a == -b); erroneously returned "
65 << f << " instead of simplifying to 0." << endl;
72 static unsigned exam_expand_power()
75 symbol x("x"), a("a"), b("b");
78 e = pow(x,pow(a+b,2)-pow(a,2)-pow(b,2)-a*b*2).expand();
81 clog << "e = pow(x,pow(a+b,2)-pow(a,2)-pow(b,2)-a*b*2).expand(); erroneously returned "
82 << e << " instead of simplifying to 1." << endl;
89 static unsigned exam_sqrfree()
92 symbol x("x"), y("y");
95 e1 = (1+x)*pow((2+x),2)*pow((3+x),3)*pow((4+x),4);
96 e2 = sqrfree(expand(e1),lst(x));
98 clog << "sqrfree(expand(" << e1 << ")) erroneously returned "
103 e1 = (x+y)*pow((x+2*y),2)*pow((x+3*y),3)*pow((x+4*y),4);
104 e2 = sqrfree(expand(e1));
106 clog << "sqrfree(expand(" << e1 << ")) erroneously returned "
110 e2 = sqrfree(expand(e1),lst(x));
112 clog << "sqrfree(expand(" << e1 << "),[x]) erroneously returned "
116 e2 = sqrfree(expand(e1),lst(y));
118 clog << "sqrfree(expand(" << e1 << "),[y]) erroneously returned "
122 e2 = sqrfree(expand(e1),lst(x,y));
124 clog << "sqrfree(expand(" << e1 << "),[x,y]) erroneously returned "
132 /* Arithmetic Operators should behave just as one expects from built-in types.
133 * When somebody screws up the operators this routine will most probably fail
134 * to compile. Unfortunately we can only test the stuff that is allowed, not
135 * what is forbidden (e.g. e1+e2 = 42) since that must not compile. :-( */
136 static unsigned exam_operator_semantics()
142 // Assignment should not return const ex though it may be obfuscated:
147 if (e1!=i1 || e2!=i2) {
148 clog << "Semantics of ex::operator=() screwed." << endl;
153 if (e1!=i1 || e2!=i2) {
154 clog << "Semantics of ex::operator=() screwed." << endl;
159 if (e1!=i1 || e2!=i2) {
160 clog << "Semantics of ex::operator=() screwed." << endl;
164 // Prefix/postfix increment/decrement behaviour:
169 if (e1!=i1 || e2!=i2) {
170 clog << "Semantics of increment/decrement operators screwed." << endl;
175 if (e1!=i1 || e2!=i2) {
176 clog << "Semantics of increment/decrement operators screwed." << endl;
180 // prefix increment/decrement must return an lvalue (contrary to postfix):
183 --++----e1; ++(++++++++(++++e2));
184 --++----i1; ++(++++++++(++++i2));
185 if (e1!=i1 || e2!=i2) {
186 clog << "Semantics of prefix increment/decrement operators screwed." << endl;
190 // This one has a good chance of detecting problems in self-assignment:
191 // (which incidentally was severely broken from version 0.7.3 to 0.8.2).
192 ex selfprobe = numeric("65536");
193 selfprobe = selfprobe;
194 if (!is_exactly_a<numeric>(selfprobe)) {
195 clog << "ex (of numeric) after self-assignment became " << selfprobe << endl;
202 /* This checks whether subs() works as intended in some special cases. */
203 static unsigned exam_subs()
209 // This used to fail in GiNaC 1.0.5 because it first substituted
210 // x+1 -> (x-1)+1 -> x, and then substituted again x -> x-1, giving
213 e2 = e1.subs(x == x-1);
214 if (!e2.is_equal(x)) {
215 clog << "(x+1).subs(x==x-1) erroneously returned " << e2 << " instead of x" << endl;
220 e2 = e1.subs(sin(wild()) == cos(wild()));
221 if (!e2.is_equal(cos(1+cos(x)))) {
222 clog << "sin(1+sin(x)).subs(sin($1)==cos($1)) erroneously returned " << e2 << " instead of cos(1+cos(x))" << endl;
229 /* Joris van der Hoeven (he of TeXmacs fame) is a funny guy. He has his own
230 * ideas what a symbolic system should do. Let's make sure we won't disappoint
231 * him some day. Incidentally, this seems to always have worked. */
232 static unsigned exam_joris()
237 ex e = expand(pow(x, x-1) * x);
238 if (e != pow(x, x)) {
239 clog << "x^(x-1)*x did not expand to x^x. Please call Joris!" << endl;
246 /* Test Chris Dams' algebraic substitutions. */
247 static unsigned exam_subs_algebraic()
250 symbol x("x"), y("y");
252 ex e = ex(x*x*x*y*y).subs(x*y==2, subs_options::subs_algebraic);
254 clog << "(x^3*y^2).subs(x*y==2,subs_options::subs_algebraic) erroneously returned " << e << endl;
258 e = ex(x*x*x*x*x).subs(x*x==y, subs_options::subs_algebraic);
260 clog << "x^5.subs(x^2==y,subs_options::subs_algebraic) erroneously returned " << e << endl;
271 cout << "examining miscellaneous other things" << flush;
272 clog << "----------miscellaneous other things:" << endl;
274 result += exam_expand_subs(); cout << '.' << flush;
275 result += exam_expand_subs2(); cout << '.' << flush;
276 result += exam_expand_power(); cout << '.' << flush;
277 result += exam_sqrfree(); cout << '.' << flush;
278 result += exam_operator_semantics(); cout << '.' << flush;
279 result += exam_subs(); cout << '.' << flush;
280 result += exam_joris(); cout << '.' << flush;
281 result += exam_subs_algebraic(); cout << '.' << flush;
284 cout << " passed " << endl;
285 clog << "(no output)" << endl;
287 cout << " failed " << endl;