From 2bee2f4aa5247aaab07519c6e7ee500c2edea9b5 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 4 Jan 2011 16:29:31 -0500 Subject: [PATCH] Unit Tests - Added new "is" method for double-precision floating point comparison, with tolerance value. --- test/test.cpp | 31 ++++++++++++++++++++++++++++++- test/test.h | 3 ++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/test/test.cpp b/test/test.cpp index bb5645b4a..cd8d2d8c5 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -270,7 +271,35 @@ void UnitTest::is (double actual, double expected, const std::string& name) } /////////////////////////////////////////////////////////////////////////////// -void UnitTest::is (char actual, char expected, const std::string& name) +void UnitTest::is (double actual, double expected, double tolerance, const std::string& name) +{ + ++mCounter; + if (fabs (actual - expected) <= tolerance) + { + ++mPassed; + std::cout << "ok " + << mCounter + << " - " + << name + << "\n"; + } + else + { + ++mFailed; + std::cout << "not ok " + << mCounter + << " - " + << name + << "\n# expected: " + << expected + << "\n# got: " + << actual + << "\n"; + } +} + +/////////////////////////////////////////////////////////////////////////////// +void UnitTest::is (unsigned char actual, unsigned char expected, const std::string& name) { ++mCounter; if (actual == expected) diff --git a/test/test.h b/test/test.h index a8ab4e4d0..487258816 100644 --- a/test/test.h +++ b/test/test.h @@ -44,7 +44,8 @@ public: void is (size_t, size_t, const std::string&); void is (int, int, const std::string&); void is (double, double, const std::string&); - void is (char, char, const std::string&); + void is (double, double, double, const std::string&); + void is (unsigned char, unsigned char, const std::string&); void is (const std::string&, const std::string&, const std::string&); void is (const char*, const char*, const std::string&); void diag (const std::string&);