Add exam for exam_common_factors.
authorRichard Kreckel <kreckel@ginac.de>
Sun, 28 Jun 2020 15:21:33 +0000 (17:21 +0200)
committerRichard Kreckel <kreckel@ginac.de>
Sun, 28 Jun 2020 15:24:29 +0000 (17:24 +0200)
Including a case involving a hidden zero to catch errors like the one fixed
in 5f7f81ad35a, as suggested by Alexey Sheplyakov <asheplyakov@yandex.ru>.

check/CMakeLists.txt
check/Makefile.am
check/exam_collect_common_factors.cpp [new file with mode: 0644]

index c229a818789e41ecc0a0acac1e2b13b246eb175b..9d9e08390035b74efe6b60ee670e385eeef9dee1 100644 (file)
@@ -19,6 +19,7 @@ set(ginac_tests
        exam_inifcns_nstdsums
        exam_differentiation
        exam_polygcd
+       exam_collect_common_factors
        exam_normalization
        exam_factor
        exam_pseries
index e8adab0728960bad743e1ceed9512d1946e333d8..8d00fdb1bedd3b133638a9649d36a61570f2ddf4 100644 (file)
@@ -16,6 +16,7 @@ EXAMS = exam_paranoia \
        exam_inifcns_nstdsums \
        exam_differentiation  \
        exam_polygcd  \
+       exam_collect_common_factors \
        exam_normalization  \
        exam_factor \
        exam_pseries  \
@@ -114,6 +115,9 @@ exam_differentiation_LDADD = ../ginac/libginac.la
 exam_polygcd_SOURCES = exam_polygcd.cpp
 exam_polygcd_LDADD = ../ginac/libginac.la
 
+exam_collect_common_factors_SOURCES = exam_collect_common_factors.cpp
+exam_collect_common_factors_LDADD = ../ginac/libginac.la
+
 exam_normalization_SOURCES = exam_normalization.cpp
 exam_normalization_LDADD = ../ginac/libginac.la
 
diff --git a/check/exam_collect_common_factors.cpp b/check/exam_collect_common_factors.cpp
new file mode 100644 (file)
index 0000000..520e0d8
--- /dev/null
@@ -0,0 +1,59 @@
+#include <iostream>
+#include "ginac.h"
+using namespace std;
+using namespace GiNaC;
+
+static unsigned exam_collect_common_factors_simple()
+{
+       unsigned result = 0;
+       symbol a("a"), b("b"), c("c"), x("x"), y("y");
+       ex ei, ef, ref;
+
+       ei = a*x + a*y;
+       ef = collect_common_factors(ei);
+       ref = (x+y)*a;
+       if (ef != ref) {
+               clog << "ERROR: collect_common_factors(" << ei << ") returned "
+                    << ef << ", not " << ref << '.' << endl;
+               ++result;
+       }
+
+       ei = a*x*x + 2*a*x*y + a*y*y;
+       ef = collect_common_factors(ei);
+       ref = a*(x*x + 2*x*y + y*y);
+       if (ef != ref) {
+               clog << "ERROR: collect_common_factors(" << ei << ") returned "
+                    << ef << ", not " << ref << '.' << endl;
+               ++result;
+       }
+
+       return result;
+}
+
+static unsigned exam_collect_common_factors_zero()
+{
+       // r = 0 = c*0 = c*(x + 1 - 1 - x) = c*(x + 1) - c - c*x
+       // e = a*r - b*r
+       symbol a("a"), b("b"), c("c"), x("x");
+
+       ex r = c*(x+1) - c - c*x;
+       ex ei = a*r + b*r;
+       ex ef = collect_common_factors(ei);
+       if (!ef.is_zero()) {
+               clog << "ERROR: " << ei << " should be 0, got " << ef << " instead." << endl;
+               return 1;
+       }
+       return 0;
+}
+
+int main(int argc, char** argv)
+{
+       int result = 0;
+
+       cout << "examining collect_common_factors" << flush;
+
+       result += exam_collect_common_factors_simple();  cout << '.' << flush;
+       result += exam_collect_common_factors_zero();  cout << '.' << flush;
+
+       return result;
+}