[json-glib/issue-28: 1/2] Add float comparison utility macros for tests



commit 70e2648e02232c1a439a7418388f18fee9afb3fe
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Nov 4 15:43:19 2017 +0000

    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.

 json-glib/tests/json-test-utils.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
---
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)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]