X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Ftests%2Frealops.cpp;h=dd45aa8dbd58a36f27bdaae9ff6bdc100144808b;hp=8076a1416fea8cc303c767e00fc18b51df116197;hb=1d179b93f6a1b2a4fe3823c26fba862c24bc5d6e;hpb=c64ec8fda6d6ad1bb5bdc5f2edd7d1d47c2d2680 diff --git a/src/tests/realops.cpp b/src/tests/realops.cpp index 8076a14..dd45aa8 100644 --- a/src/tests/realops.cpp +++ b/src/tests/realops.cpp @@ -1,25 +1,35 @@ +/** + * Test mathematical operations on the Real type and consistency with double + */ + #include "main.h" #include "real.h" using namespace std; using namespace IPDF; -#define TEST_CASES 100 +#define TEST_CASES 1000 + +static double g_totalerror = 0; -bool NotEqual(double a, double b, double threshold=1e-2) +bool NotEqual(double a, double b, double threshold=1e-4) { - return (fabs(a-b) > threshold); + double error = fabs(a-b); + g_totalerror += error; + return (error > threshold); } int main(int argc, char ** argv) { srand(time(NULL)); + DebugRealInfo(); unsigned failures = 0; for (unsigned i = 0; i < TEST_CASES; ++i) { - double da = (double)(rand()%100 + 1) / (double)(rand()%100 + 1); - double db = (double)(rand()%100 + 1) / (double)(rand()%100 + 1); + //Debug("Test %u of %u", i, TEST_CASES); + double da = (double)(rand() + 1) / (double)(rand() + 1); + double db = (double)(rand() + 1) / (double)(rand() + 1); if (rand() % 2 == 0) da = -da; @@ -106,7 +116,18 @@ int main(int argc, char ** argv) { failures++; Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop)); + } + if (NotEqual(Double(a*0.0 + 1.0), da*0.0 + 1.0)) + { + failures++; + Warn("a * 0 = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop)); } + + if (NotEqual(Double(a=b), da=db)) + { + failures++; + Warn("a = b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop)); + } if (failures > old_failures) { @@ -117,5 +138,6 @@ int main(int argc, char ** argv) } } Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures); + Debug("Total accumulated difference between Real and Double operations was %f", g_totalerror); }