diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2013-04-05 14:18:46 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2013-04-05 16:27:04 +1000 |
commit | 8c778edd0dd2e0b9022484f5b20fb8795cd82550 (patch) | |
tree | 2feee274f575dabd5f702c275a50df5235414f52 | |
parent | 3ee82485bec140d5181ed30b899f78042de805ce (diff) |
common/helpers: add double_cmp helper function
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r-- | tests/common/helpers.cpp | 9 | ||||
-rw-r--r-- | tests/common/helpers.h | 7 |
2 files changed, 16 insertions, 0 deletions
diff --git a/tests/common/helpers.cpp b/tests/common/helpers.cpp index 8f1f0fe..f07b3df 100644 --- a/tests/common/helpers.cpp +++ b/tests/common/helpers.cpp @@ -27,6 +27,8 @@ #endif #include "helpers.h" +#include <math.h> + #include <sstream> #include <fstream> @@ -289,3 +291,10 @@ bool SearchFileForString(const std::string &path, const std::string &substring) return false; } + +int double_cmp(double a, double b, int precision) +{ + int ai = (int)round(a * pow(10, precision)); + int bi = (int)round(b * pow(10, precision)); + return (ai > bi) ? 1 : (ai < bi) ? -1 : 0; +} diff --git a/tests/common/helpers.h b/tests/common/helpers.h index 1cf3449..ac23ddb 100644 --- a/tests/common/helpers.h +++ b/tests/common/helpers.h @@ -175,5 +175,12 @@ bool NoEventPending(Display *dpy); * false otherwise. */ bool SearchFileForString(const std::string &path, const std::string &substring); + +/** + * Compare two doubles for equality within the given precision. + * + * @return 1, 0, -1 if a is larger, equal, or less than b, respectively + */ +int double_cmp(double a, double b, int precision = 2); #endif |