[glib: 1/2] gtestutils: Allow cmpmem() arguments to be NULL iff lengths are zero



commit 55997a0aad5db61c702de4dfd4284636a26246c7
Author: Philip Withnall <withnall endlessm com>
Date:   Mon Sep 30 12:00:30 2019 +0100

    gtestutils: Allow cmpmem() arguments to be NULL iff lengths are zero
    
    Document this and add a test.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Fixes: #1897

 glib/gtestutils.c    |  6 ++++--
 glib/gtestutils.h    |  8 +++++++-
 glib/tests/testing.c | 15 +++++++++++++++
 3 files changed, 26 insertions(+), 3 deletions(-)
---
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index 5c2a55896..1010dcc1b 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -661,9 +661,9 @@
 
 /**
  * g_assert_cmpmem:
- * @m1: pointer to a buffer
+ * @m1: (nullable): pointer to a buffer
  * @l1: length of @m1
- * @m2: pointer to another buffer
+ * @m2: (nullable): pointer to another buffer
  * @l2: length of @m2
  *
  * Debugging macro to compare memory regions. If the comparison fails,
@@ -675,6 +675,8 @@
  * The advantage of this macro is that it can produce a message that
  * includes the actual values of @l1 and @l2.
  *
+ * @m1 may be %NULL if (and only if) @l1 is zero; similarly for @m2 and @l2.
+ *
  * |[<!-- language="C" -->
  *   g_assert_cmpmem (buf->data, buf->len, expected, sizeof (expected));
  * ]|
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 26de21216..c4203f010 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -79,7 +79,13 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
 #define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
                                              gconstpointer __m1 = m1, __m2 = m2; \
                                              int __l1 = l1, __l2 = l2; \
-                                             if (__l1 != __l2) \
+                                             if (__l1 != 0 && __m1 == NULL) \
+                                               g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
+                                                                    "assertion failed (" #l1 " == 0 || " #m1 
" != NULL)"); \
+                                             else if (__l2 != 0 && __m2 == NULL) \
+                                               g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
+                                                                    "assertion failed (" #l2 " == 0 || " #m2 
" != NULL)"); \
+                                             else if (__l1 != __l2) \
                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
                                                                            #l1 " (len(" #m1 ")) == " #l2 " 
(len(" #m2 "))", \
                                                                            (long double) __l1, "==", (long 
double) __l2, 'i'); \
diff --git a/glib/tests/testing.c b/glib/tests/testing.c
index 98fe66fba..d0ce2c1e7 100644
--- a/glib/tests/testing.c
+++ b/glib/tests/testing.c
@@ -94,6 +94,13 @@ test_assertions_bad_cmpmem_data (void)
   exit (0);
 }
 
+static void
+test_assertions_bad_cmpmem_null (void)
+{
+  g_assert_cmpmem (NULL, 3, NULL, 3);
+  exit (0);
+}
+
 static void
 test_assertions_bad_cmpfloat_epsilon (void)
 {
@@ -127,6 +134,9 @@ test_assertions (void)
   g_assert_cmpstr ("fzz", >, "faa");
   g_assert_cmpstr ("fzz", ==, "fzz");
   g_assert_cmpmem ("foo", 3, "foot", 3);
+  g_assert_cmpmem (NULL, 0, NULL, 0);
+  g_assert_cmpmem (NULL, 0, "foot", 0);
+  g_assert_cmpmem ("foo", 0, NULL, 0);
 
   v1 = g_variant_new_parsed ("['hello', 'there']");
   v2 = g_variant_new_parsed ("['hello', 'there']");
@@ -162,6 +172,10 @@ test_assertions (void)
   g_test_trap_assert_stderr ("*assertion failed*");
   g_test_trap_assert_stderr_unmatched ("*assertion failed*len*");
 
+  g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_null", 0, 0);
+  g_test_trap_assert_failed ();
+  g_test_trap_assert_stderr ("*assertion failed*NULL*");
+
   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpfloat_epsilon", 0, 0);
   g_test_trap_assert_failed ();
   g_test_trap_assert_stderr ("*assertion failed*");
@@ -1266,6 +1280,7 @@ main (int   argc,
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_len", test_assertions_bad_cmpmem_len);
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_data", test_assertions_bad_cmpmem_data);
+  g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_null", test_assertions_bad_cmpmem_null);
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpfloat_epsilon", test_assertions_bad_cmpfloat_epsilon);
   g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
   g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, 
fixturetest_teardown);


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