summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2017-11-04 15:43:19 +0000
committerEmmanuele Bassi <ebassi@gnome.org>2017-11-04 15:43:19 +0000
commit70e2648e02232c1a439a7418388f18fee9afb3fe (patch)
tree69e6ba24724a305431585604f392bd5afa4bfc92
parentda70483cc7cacf0bfb6c11be252f96bc02dfdc74 (diff)
downloadjson-glib-70e2648e02232c1a439a7418388f18fee9afb3fe.tar.gz
Add float comparison utility macros for tests
We do compare floating point values elsewhere in our code, so we should ensure that we're doing that with a certain amount of fuzziness.
-rw-r--r--json-glib/tests/json-test-utils.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/json-glib/tests/json-test-utils.h b/json-glib/tests/json-test-utils.h
new file mode 100644
index 0000000..83a02c6
--- /dev/null
+++ b/json-glib/tests/json-test-utils.h
@@ -0,0 +1,21 @@
+#include <string.h>
+#include <math.h>
+#include <float.h>
+#include <glib.h>
+#include <json-glib/json-glib.h>
+
+#define json_fuzzy_equals(n1,n2,epsilon) \
+ (((n1) > (n2) ? ((n1) - (n2)) : ((n2) - (n1))) < (epsilon))
+
+#define json_assert_fuzzy_equals(n1,n2,epsilon) \
+ G_STMT_START { \
+ double __n1 = (n1), __n2 = (n2), __epsilon = (epsilon); \
+ if (json_fuzzy_equals (__n1, __n2, __epsilon)) ; else { \
+ g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+ #n1 " == " #n2 " (+/- " #epsilon ")", \
+ __n1, "==", __n2, 'f'); \
+ } \
+ } G_STMT_END
+
+#define json_assert_almost_equals(n1,n2) \
+ json_assert_fuzzy_equals (n1, n2, DBL_EPSILON)