1 /** @file match_bug.cpp
3 * Check for bug in GiNaC::ex::match() described here:
4 * http://www.ginac.de/pipermail/ginac-devel/2006-April/000942.html */
7 * GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "error_report.h"
28 using namespace GiNaC;
31 * basic::match(lst&) used to have an obscure side effect: repl_lst
32 * could be modified even if the match failed! Although this "feature"
33 * was documented it happend to be very confusing *even for GiNaC
35 * http://www.ginac.de/pipermail/ginac-devel/2006-April/000942.html
37 * It was fixed in 192ed7390b7b2b705ad100e3db0a92eedd2b20ad. Let's make
38 * sure it will be never re-added:
40 static void failed_match_have_side_effects()
44 ex pattern = pow(wild(0), -1);
45 // obviously e does NOT match the pattern
47 bool match_p = e.match(pattern, repls);
48 bug_on(match_p, "match(" << e << ", " << pattern << ") says \"Yes\"");
49 bug_on(repls.size() != 0,
50 "failed match have side effects: repls = " << repls);
54 * As a consequence of the bug described above pattern matching can wrongly
55 * fail. In particular, x^5*y^(-1) fails to match ($0)^(-1)*x^($2).
57 * The first thing that is attempted to match is x^5 with $0^(-1). This match
58 * will fail. However repl_lst will contain $0 == x as a side effect. This
59 * repl_lst will prevent the match of y^(-1) to ($0)^(-1) to succeed.
61 * This issue was worked around by 73f0ce4cf8d91f073f35a45443f5fbe886921c5c.
62 * Now we have a real fix (192ed7390b7b2b705ad100e3db0a92eedd2b20ad), but
65 static void match_false_negative()
67 symbol x("x"), y("y");
68 ex e = pow(x, 5)*pow(y, -1);
69 ex pattern = pow(wild(0), -1)*pow(x, wild(2));
71 bool match_p = e.match(pattern, repls);
72 bug_on(!match_p, "false negative: " << e << " did not match "
76 int main(int argc, char** argv)
78 std::cout << "checking for historical bugs in match()... " << std::flush;
79 failed_match_have_side_effects();
80 match_false_negative();
81 std::cout << "not found. ";