From c64ec8fda6d6ad1bb5bdc5f2edd7d1d47c2d2680 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Sun, 6 Jul 2014 20:17:09 +0800 Subject: [PATCH] More testing particularly of negatives --- src/tests/realops.cpp | 47 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/tests/realops.cpp b/src/tests/realops.cpp index 0aeeeb6..8076a14 100644 --- a/src/tests/realops.cpp +++ b/src/tests/realops.cpp @@ -6,7 +6,7 @@ using namespace IPDF; #define TEST_CASES 100 -bool NotEqual(double a, double b, double threshold=1e-4) +bool NotEqual(double a, double b, double threshold=1e-2) { return (fabs(a-b) > threshold); } @@ -18,13 +18,29 @@ int main(int argc, char ** argv) unsigned failures = 0; for (unsigned i = 0; i < TEST_CASES; ++i) { - double da = (double)(rand()) / (double)(rand()); - double db = (double)(rand()) / (double)(rand()); + double da = (double)(rand()%100 + 1) / (double)(rand()%100 + 1); + double db = (double)(rand()%100 + 1) / (double)(rand()%100 + 1); + + if (rand() % 2 == 0) + da = -da; + if (rand() % 2 == 0) + db = -db; Real a(da); Real b(db); - - + Real aa(a); + Real bb(b); + unsigned old_failures = failures; + if (NotEqual(Double(a), Double(aa))) + { + failures++; + Warn("a != Real(a); %f vs %f", Double(a), Double(aa)); + } + if (NotEqual(Double(b), Double(bb))) + { + failures++; + Warn("b != Real(b); %f vs %f", Double(b), Double(bb)); + } if (NotEqual(Double(a), da)) { @@ -67,27 +83,38 @@ int main(int argc, char ** argv) failures++; Warn("b has changed after +-*/ from %f to %f", db, Double(b)); } - + Real abeforeop(a); if (NotEqual(Double(a+=b), da+=db)) { failures++; - Warn("a += b = %f should be %f", Double(a), da); + Warn("a += b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop)); } + abeforeop = a; if (NotEqual(Double(a-=b), da-=db)) { failures++; - Warn("a -= b = %f should be %f", Double(a), da); + Warn("a -= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop)); } + abeforeop = a; if (NotEqual(Double(a*=b), da*=db)) { failures++; - Warn("a *= b = %f should be %f", Double(a), da); + Warn("a *= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop)); } + abeforeop = a; if (NotEqual(Double(a/=b), da/=db)) { failures++; - Warn("a /= b = %f should be %f", Double(a), da); + Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop)); } + + if (failures > old_failures) + { + Error("%u failures on case %u da = %f, db = %f, a = %f, b = %f, aa = %f, bb = %f", failures-old_failures, i, da, db, Double(a), Double(b), Double(aa), Double(bb)); + #if REAL == REAL_RATIONAL || REAL == REAL_RATIONAL_ARBINT + Debug("\tStrings are a = %s, b = %s, aa = %s, bb = %s", a.Str().c_str(), b.Str().c_str(), aa.Str().c_str(), bb.Str().c_str()); + #endif + } } Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures); -- 2.20.1