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
This commit is contained in:
Philip Withnall 2019-09-30 12:00:30 +01:00
parent 3ede078e2c
commit 55997a0aad
3 changed files with 26 additions and 3 deletions

View File

@ -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));
* ]|

View File

@ -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'); \

View File

@ -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);