spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit 7fa3c4c55704e8130e302c85d189c114bdebaddc
parent 8f5bc2ad39d038177746d4a13fad58f5f99fd30b
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Fri, 22 Feb 2013 01:00:23 -0800

Fix floating point comparison in unit tests

Diffstat:
Mtests/test.h | 13++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tests/test.h b/tests/test.h @@ -19,6 +19,7 @@ #ifndef TEST_H_ #define TEST_H_ +#include <cmath> #include <functional> #include <iostream> #include <string> @@ -28,10 +29,20 @@ void run(std::function<void ()> func, const std::string& message); extern int g_total; extern int g_passes; +template<class T> bool equal(const T& a, const T& b) +{ + return a == b; +} + +template<> inline bool equal<double>(const double& a, const double& b) +{ + return std::abs(a - b) < 1e-8; +} + template<class T> void test(const std::string& message, const T& expected, const T& actual) { g_total++; - if (expected == actual) { + if (equal(expected, actual)) { g_passes++; } else { std::cerr << "FAIL: " << message;