[PATCH libinput v3 4/5] litest: floating point comparison macros

Andreas Pokorny andreas.pokorny at canonical.com
Mon Jun 22 03:48:07 PDT 2015


Adds the macros ck_assert_double_{eq,ne,lt,gt,le,ge} to compare double
values using a fixed tolerance value. The tolerance value is
picked based on the range of values to be expected by the libinput API.

Signed-off-by: Andreas Pokorny <andreas.pokorny at canonical.com>
---
 test/litest-selftest.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test/litest.h          | 51 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+)

diff --git a/test/litest-selftest.c b/test/litest-selftest.c
index f797477..5016514 100644
--- a/test/litest-selftest.c
+++ b/test/litest-selftest.c
@@ -289,6 +289,61 @@ START_TEST(litest_ptr_notnull_trigger_NULL)
 }
 END_TEST
 
+START_TEST(ck_double_eq_and_ne)
+{
+	ck_assert_double_eq(0.4,0.4);
+	ck_assert_double_eq(0.4,0.4 + 1E-6);
+	ck_assert_double_ne(0.4,0.4 + 1E-3);
+}
+END_TEST
+
+START_TEST(ck_double_lt_gt)
+{
+	ck_assert_double_lt(12.0,13.0);
+	ck_assert_double_gt(15.4,13.0);
+	ck_assert_double_le(12.0,12.0);
+	ck_assert_double_le(12.0,20.0);
+	ck_assert_double_ge(12.0,12.0);
+	ck_assert_double_ge(20.0,12.0);
+}
+END_TEST
+
+START_TEST(ck_double_eq_fails)
+{
+	ck_assert_double_eq(0.41,0.4);
+}
+END_TEST
+
+START_TEST(ck_double_ne_fails)
+{
+	ck_assert_double_ne(0.4 + 1E-7,0.4);
+}
+END_TEST
+
+START_TEST(ck_double_lt_fails)
+{
+	ck_assert_double_lt(6,5);
+}
+END_TEST
+
+START_TEST(ck_double_gt_fails)
+{
+	ck_assert_double_gt(5,6);
+}
+END_TEST
+
+START_TEST(ck_double_le_fails)
+{
+	ck_assert_double_le(6,5);
+}
+END_TEST
+
+START_TEST(ck_double_ge_fails)
+{
+	ck_assert_double_ge(5,6);
+}
+END_TEST
+
 static Suite *
 litest_assert_macros_suite(void)
 {
@@ -342,6 +397,17 @@ litest_assert_macros_suite(void)
 	tcase_add_test(tc, litest_ptr_notnull_notrigger);
 	suite_add_tcase(s, tc);
 
+	tc = tcase_create("double comparison ");
+	tcase_add_test(tc, ck_double_eq_and_ne);
+	tcase_add_test(tc, ck_double_lt_gt);
+	tcase_add_exit_test(tc, ck_double_eq_fails, 1);
+	tcase_add_exit_test(tc, ck_double_ne_fails, 1);
+	tcase_add_exit_test(tc, ck_double_lt_fails, 1);
+	tcase_add_exit_test(tc, ck_double_gt_fails, 1);
+	tcase_add_exit_test(tc, ck_double_le_fails, 1);
+	tcase_add_exit_test(tc, ck_double_ge_fails, 1);
+	suite_add_tcase(s, tc);
+
 	return s;
 }
 
diff --git a/test/litest.h b/test/litest.h
index edb8960..f7abc55 100644
--- a/test/litest.h
+++ b/test/litest.h
@@ -433,4 +433,55 @@ void litest_semi_mt_touch_up(struct litest_device *d,
 #define ck_assert_notnull(ptr) ck_assert_ptr_ne(ptr, NULL)
 #endif
 
+#define CK_DOUBLE_EQ_EPSILON 1E-3
+#define ck_assert_double_eq(X,Y)  \
+	do { \
+		double _ck_x = X; \
+		double _ck_y = Y; \
+		ck_assert_msg(fabs(_ck_x - _ck_y) < CK_DOUBLE_EQ_EPSILON, \
+                              "Assertion '" #X " == " #Y \
+                              "' failed: "#X"==%f, "#Y"==%f", \
+                              _ck_x, \
+                              _ck_y); \
+	} while (0)
+
+#define ck_assert_double_ne(X,Y)  \
+	do { \
+		double _ck_x = X; \
+		double _ck_y = Y; \
+		ck_assert_msg(fabs(_ck_x - _ck_y) > CK_DOUBLE_EQ_EPSILON, \
+                              "Assertion '" #X " != " #Y \
+                              "' failed: "#X"==%f, "#Y"==%f", \
+                              _ck_x, \
+                              _ck_y); \
+	} while (0)
+
+#define _ck_assert_double_eq(X, OP, Y)  \
+	do { \
+		double _ck_x = X; \
+		double _ck_y = Y; \
+		ck_assert_msg(_ck_x OP _ck_y || \
+                              fabs(_ck_x - _ck_y) < CK_DOUBLE_EQ_EPSILON, \
+                              "Assertion '" #X#OP#Y \
+                              "' failed: "#X"==%f, "#Y"==%f", \
+                              _ck_x, \
+                              _ck_y); \
+	} while (0)
+
+#define _ck_assert_double_ne(X, OP,Y) \
+do { \
+		double _ck_x = X; \
+		double _ck_y = Y; \
+		ck_assert_msg(_ck_x OP _ck_y && \
+                              fabs(_ck_x - _ck_y) > CK_DOUBLE_EQ_EPSILON, \
+                              "Assertion '" #X#OP#Y \
+                              "' failed: "#X"==%f, "#Y"==%f", \
+                              _ck_x, \
+                              _ck_y); \
+	} while (0)
+#define ck_assert_double_lt(X, Y) _ck_assert_double_ne(X, <, Y)
+#define ck_assert_double_le(X, Y) _ck_assert_double_eq(X, <=, Y)
+#define ck_assert_double_gt(X, Y) _ck_assert_double_ne(X, >, Y)
+#define ck_assert_double_ge(X, Y) _ck_assert_double_eq(X, >=, Y)
+
 #endif /* LITEST_H */
-- 
2.1.4



More information about the wayland-devel mailing list