[PATCH 5/7] test: add pointer comparison macros

Peter Hutterer peter.hutterer at who-t.net
Thu May 7 22:57:48 PDT 2015


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 test/litest-selftest.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++
 test/litest.c          |  12 +++++
 test/litest.h          |  26 +++++++++
 3 files changed, 179 insertions(+)

diff --git a/test/litest-selftest.c b/test/litest-selftest.c
index 5ed20ff..f797477 100644
--- a/test/litest-selftest.c
+++ b/test/litest-selftest.c
@@ -164,6 +164,131 @@ START_TEST(litest_int_ge_notrigger)
 }
 END_TEST
 
+START_TEST(litest_ptr_eq_notrigger)
+{
+	int v = 10;
+	int *a = &v;
+	int *b = &v;
+	int c = NULL;
+	int d = NULL;
+
+	litest_assert_ptr_eq(a, b);
+	litest_assert_ptr_eq(c, d);
+}
+END_TEST
+
+START_TEST(litest_ptr_eq_trigger)
+{
+	int v = 10;
+	int v2 = 11;
+	int *a = &v;
+	int *b = &v2;
+
+	litest_assert_ptr_eq(a, b);
+}
+END_TEST
+
+START_TEST(litest_ptr_eq_trigger_NULL)
+{
+	int v = 10;
+	int *a = &v;
+	int *b = NULL;
+
+	litest_assert_ptr_eq(a, b);
+}
+END_TEST
+
+START_TEST(litest_ptr_eq_trigger_NULL2)
+{
+	int v = 10;
+	int *a = &v;
+	int *b = NULL;
+
+	litest_assert_ptr_eq(b, a);
+}
+END_TEST
+
+START_TEST(litest_ptr_ne_trigger)
+{
+	int v = 10;
+	int *a = &v;
+	int *b = &v;
+
+	litest_assert_ptr_ne(a, b);
+}
+END_TEST
+
+START_TEST(litest_ptr_ne_trigger_NULL)
+{
+	int *a = NULL;
+
+	litest_assert_ptr_ne(a, NULL);
+}
+END_TEST
+
+START_TEST(litest_ptr_ne_trigger_NULL2)
+{
+	int *a = NULL;
+
+	litest_assert_ptr_ne(NULL, a);
+}
+END_TEST
+
+START_TEST(litest_ptr_ne_notrigger)
+{
+	int v1 = 10;
+	int v2 = 10;
+	int *a = &v1;
+	int *b = &v2;
+	int *c = NULL;
+
+	litest_assert_ptr_ne(a, b);
+	litest_assert_ptr_ne(a, c);
+	litest_assert_ptr_ne(c, b);
+}
+END_TEST
+
+START_TEST(litest_ptr_null_notrigger)
+{
+	int *a = NULL;
+
+	litest_assert_ptr_null(a);
+	litest_assert_ptr_null(NULL);
+}
+END_TEST
+
+START_TEST(litest_ptr_null_trigger)
+{
+	int v;
+	int *a = &v;
+
+	litest_assert_ptr_null(a);
+}
+END_TEST
+
+START_TEST(litest_ptr_notnull_notrigger)
+{
+	int v;
+	int *a = &v;
+
+	litest_assert_ptr_notnull(a);
+}
+END_TEST
+
+START_TEST(litest_ptr_notnull_trigger)
+{
+	int *a = NULL;
+
+	litest_assert_ptr_notnull(a);
+}
+END_TEST
+
+START_TEST(litest_ptr_notnull_trigger_NULL)
+{
+	litest_assert_ptr_notnull(NULL);
+}
+END_TEST
+
 static Suite *
 litest_assert_macros_suite(void)
 {
@@ -201,6 +326,22 @@ litest_assert_macros_suite(void)
 	tcase_add_test(tc, litest_int_gt_notrigger);
 	suite_add_tcase(s, tc);
 
+	tc = tcase_create("pointer comparison ");
+	tcase_add_test_raise_signal(tc, litest_ptr_eq_trigger, SIGABRT);
+	tcase_add_test_raise_signal(tc, litest_ptr_eq_trigger_NULL, SIGABRT);
+	tcase_add_test_raise_signal(tc, litest_ptr_eq_trigger_NULL2, SIGABRT);
+	tcase_add_test(tc, litest_ptr_eq_notrigger);
+	tcase_add_test_raise_signal(tc, litest_ptr_ne_trigger, SIGABRT);
+	tcase_add_test_raise_signal(tc, litest_ptr_ne_trigger_NULL, SIGABRT);
+	tcase_add_test_raise_signal(tc, litest_ptr_ne_trigger_NULL2, SIGABRT);
+	tcase_add_test(tc, litest_ptr_ne_notrigger);
+	tcase_add_test_raise_signal(tc, litest_ptr_null_trigger, SIGABRT);
+	tcase_add_test(tc, litest_ptr_null_notrigger);
+	tcase_add_test_raise_signal(tc, litest_ptr_notnull_trigger, SIGABRT);
+	tcase_add_test_raise_signal(tc, litest_ptr_notnull_trigger_NULL, SIGABRT);
+	tcase_add_test(tc, litest_ptr_notnull_notrigger);
+	suite_add_tcase(s, tc);
+
 	return s;
 }
 
diff --git a/test/litest.c b/test/litest.c
index f284d22..9612e17 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -188,6 +188,18 @@ litest_fail_comparison_int(const char *file,
 	abort();
 }
 
+void
+litest_fail_comparison_ptr(const char *file,
+			   int line,
+			   const char *func,
+			   const char *comparison)
+{
+	litest_log("FAILED COMPARISON: %s\n", comparison);
+	litest_log("in %s() (%s:%d)\n", func, file, line);
+	litest_backtrace();
+	abort();
+}
+
 struct test {
 	struct list node;
 	char *name;
diff --git a/test/litest.h b/test/litest.h
index f13ea7e..74909cc 100644
--- a/test/litest.h
+++ b/test/litest.h
@@ -89,6 +89,27 @@
 #define litest_assert_int_gt(a_, b_) \
 	litest_assert_comparison_int_(a_, >, b_)
 
+#define litest_assert_comparison_ptr_(a_, op_, b_) \
+	do { \
+		__typeof__(a_) _a = a_; \
+		__typeof__(b_) _b = b_; \
+		if (!((_a) op_ (_b))) \
+			litest_fail_comparison_ptr(__FILE__, __LINE__, __func__,\
+						   #a_ " " #op_ " " #b_); \
+	} while(0)
+
+#define litest_assert_ptr_eq(a_, b_) \
+	litest_assert_comparison_ptr_(a_, ==, b_)
+
+#define litest_assert_ptr_ne(a_, b_) \
+	litest_assert_comparison_ptr_(a_, !=, b_)
+
+#define litest_assert_ptr_null(a_) \
+	litest_assert_comparison_ptr_(a_, ==, NULL)
+
+#define litest_assert_ptr_notnull(a_) \
+	litest_assert_comparison_ptr_(a_, !=, NULL)
+
 enum litest_device_type {
 	LITEST_NO_DEVICE = -1,
 	LITEST_SYNAPTICS_CLICKPAD = -2,
@@ -173,6 +194,11 @@ litest_fail_comparison_int(const char *file,
 			   int b,
 			   const char *astr,
 			   const char *bstr);
+void
+litest_fail_comparison_ptr(const char *file,
+			   int line,
+			   const char *func,
+			   const char *comparison);
 
 void litest_add(const char *name, void *func,
 		enum litest_device_feature required_feature,
-- 
2.3.5



More information about the wayland-devel mailing list