[glib] gtestutils: add g_assert_cmpmem()



commit 9f2e3f6b7262a8cae817a4cc12388fcc1bd63bce
Author: Dan Winship <danw gnome org>
Date:   Wed Dec 3 05:57:29 2014 -0500

    gtestutils: add g_assert_cmpmem()
    
    Add a test macro to compare two buffers (which are not already known
    to be the same length) for equality.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=754283

 docs/reference/glib/glib-sections.txt |    1 +
 gio/tests/async-close-output-stream.c |    9 +-----
 gio/tests/converter-stream.c          |   46 +++++++++++++++-----------------
 gio/tests/gdbus-peer.c                |    3 +-
 gio/tests/gsettings.c                 |    4 +--
 gio/tests/gsubprocess.c               |   11 +++----
 gio/tests/readwrite.c                 |   10 ++-----
 glib/gtestutils.c                     |   33 ++++++++++++++++++++---
 glib/gtestutils.h                     |   10 +++++++
 glib/tests/base64.c                   |   25 +++--------------
 glib/tests/bytes.c                    |   23 +++++----------
 glib/tests/checksum.c                 |    3 +-
 glib/tests/gvariant.c                 |    3 +-
 glib/tests/hmac.c                     |    3 +-
 glib/tests/strfuncs.c                 |    3 +-
 glib/tests/string.c                   |   10 +++----
 glib/tests/testing.c                  |   26 ++++++++++++++++++
 17 files changed, 120 insertions(+), 103 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 880dab6..d5fa3fe 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -3062,6 +3062,7 @@ g_assert_cmpint
 g_assert_cmpuint
 g_assert_cmphex
 g_assert_cmpfloat
+g_assert_cmpmem
 g_assert_no_error
 g_assert_error
 g_assert_true
diff --git a/gio/tests/async-close-output-stream.c b/gio/tests/async-close-output-stream.c
index 507214c..5f66202 100644
--- a/gio/tests/async-close-output-stream.c
+++ b/gio/tests/async-close-output-stream.c
@@ -100,15 +100,10 @@ compare_output (SetupData *data)
   gsize size;
   gpointer written;
 
-  size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
-
-  /* compare the size of the data */
-  g_assert_cmpint (size, ==, data->expected_size);
-
   written = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->data_stream));
+  size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
 
-  /* compare the data itself */
-  g_assert (memcmp (written, data->expected_output, size) == 0);
+  g_assert_cmpmem (written, size, data->expected_output, data->expected_size);
 }
 
 static void
diff --git a/gio/tests/converter-stream.c b/gio/tests/converter-stream.c
index c867249..8bec95c 100644
--- a/gio/tests/converter-stream.c
+++ b/gio/tests/converter-stream.c
@@ -341,8 +341,7 @@ test_expander (void)
       total_read += res;
     }
 
-  g_assert (total_read == n_written);
-  g_assert (memcmp (converted1, converted2, n_written)  == 0);
+  g_assert_cmpmem (converted1, n_written, converted2, total_read);
 
   g_converter_reset (expander);
 
@@ -371,10 +370,9 @@ test_expander (void)
 
   g_output_stream_close (cstream_out, NULL, NULL);
 
-  g_assert (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)) == n_written);
-  g_assert (memcmp (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
-                   converted1,
-                   n_written)  == 0);
+  g_assert_cmpmem (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
+                   g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)),
+                   converted1, n_written);
 
   g_free (converted1);
   g_free (converted2);
@@ -433,8 +431,8 @@ test_compressor (void)
       total_read += res;
     }
 
-  g_assert (total_read == n_read - 1); /* Last 2 zeros are combined */
-  g_assert (memcmp (converted, unexpanded_data, total_read)  == 0);
+  /* "n_read - 1" because last 2 zeros are combined */
+  g_assert_cmpmem (unexpanded_data, n_read - 1, converted, total_read);
 
   g_object_unref (cstream);
 
@@ -461,10 +459,11 @@ test_compressor (void)
 
   g_output_stream_close (cstream_out, NULL, NULL);
 
-  g_assert (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)) == n_read - 1); /* Last 
2 zeros are combined */
-  g_assert (memcmp (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
-                   unexpanded_data,
-                   g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)))  == 0);
+  /* "n_read - 1" because last 2 zeros are combined */
+  g_assert_cmpmem (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
+                   g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)),
+                   unexpanded_data,
+                   n_read - 1);
 
   g_object_unref (cstream_out);
 
@@ -717,8 +716,7 @@ test_converter_leftover (void)
       total_read += res;
   }
 
-  g_assert_cmpint (total_read, ==, LEFTOVER_BUFSIZE);
-  g_assert (memcmp (converted, orig, LEFTOVER_BUFSIZE)  == 0);
+  g_assert_cmpmem (orig, LEFTOVER_BUFSIZE, converted, total_read);
 
   g_object_unref (cstream);
   g_free (orig);
@@ -795,10 +793,9 @@ test_roundtrip (gconstpointer data)
   g_output_stream_splice (ostream2, cistream1, 0, NULL, &error);
   g_assert_no_error (error);
 
-  g_assert_cmpuint (DATA_LENGTH * sizeof (guint32), ==,
-    g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (ostream2)));
-  g_assert (memcmp (data0, g_memory_output_stream_get_data (
-    G_MEMORY_OUTPUT_STREAM (ostream2)), DATA_LENGTH * sizeof (guint32)) == 0);
+  g_assert_cmpmem (data0, DATA_LENGTH * sizeof (guint32),
+                   g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (ostream2)),
+                   g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (ostream2)));
   g_object_unref (istream1);
   g_converter_reset (decompressor);
   g_object_get (decompressor, "format", &fmt, NULL);
@@ -1057,8 +1054,8 @@ test_converter_pollable (void)
       total_read += res;
     }
 
-  g_assert (total_read == n_read - 1); /* Last 2 zeros are combined */
-  g_assert (memcmp (converted, unexpanded_data, total_read)  == 0);
+  /* "n_read - 1" because last 2 zeros are combined */
+  g_assert_cmpmem (unexpanded_data, n_read - 1, converted, total_read);
 
   g_object_unref (cstream);
   g_object_unref (left);
@@ -1094,10 +1091,11 @@ test_converter_pollable (void)
 
   g_output_stream_close (cstream_out, NULL, NULL);
 
-  g_assert (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)) == n_read - 1); /* Last 
2 zeros are combined */
-  g_assert (memcmp (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
-                   unexpanded_data,
-                   g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)))  == 0);
+  /* "n_read - 1" because last 2 zeros are combined */
+  g_assert_cmpmem (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
+                   g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)),
+                   unexpanded_data,
+                   n_read - 1);
 
   g_object_unref (cstream_out);
 
diff --git a/gio/tests/gdbus-peer.c b/gio/tests/gdbus-peer.c
index 2bb3b8a..841c40c 100644
--- a/gio/tests/gdbus-peer.c
+++ b/gio/tests/gdbus-peer.c
@@ -835,8 +835,7 @@ test_peer (void)
                          &len2,
                          &error);
     g_assert_no_error (error);
-    g_assert_cmpint (len, ==, len2);
-    g_assert (memcmp (buf, buf2, len) == 0);
+    g_assert_cmpmem (buf, len, buf2, len2);
     g_free (buf2);
     g_free (buf);
   }
diff --git a/gio/tests/gsettings.c b/gio/tests/gsettings.c
index d0bc5aa..ee8e8fa 100644
--- a/gio/tests/gsettings.c
+++ b/gio/tests/gsettings.c
@@ -1761,9 +1761,7 @@ test_strinfo (void)
     strinfo_builder_append_item (builder, "foo", 1);
     strinfo_builder_append_item (builder, "bar", 2);
     g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
-    g_assert_cmpint (builder->len % 4, ==, 0);
-    g_assert_cmpint (builder->len / 4, ==, length);
-    g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
+    g_assert_cmpmem (builder->str, builder->len, strinfo, length * 4);
     g_string_free (builder, TRUE);
   }
 
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index 92bddd0..e1270b1 100644
--- a/gio/tests/gsubprocess.c
+++ b/gio/tests/gsubprocess.c
@@ -331,8 +331,9 @@ test_cat_utf8 (void)
 
   output_buf = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)output_buf_stream);
 
-  g_assert_cmpint (g_bytes_get_size (output_buf), ==, 13);
-  g_assert_cmpint (memcmp (g_bytes_get_data (output_buf, NULL), "hello, world!", 13), ==, 0);
+  g_assert_cmpmem (g_bytes_get_data (output_buf, NULL),
+                   g_bytes_get_size (output_buf),
+                   "hello, world!", 13);
 
   g_bytes_unref (output_buf);
   g_main_loop_unref (data.loop);
@@ -611,8 +612,7 @@ on_communicate_complete (GObject               *proc,
       stdout_len = strlen (stdout_str);
     }
 
-  g_assert_cmpint (stdout_len, ==, 11);
-  g_assert (memcmp (stdout_data, "hello world", 11) == 0);
+  g_assert_cmpmem (stdout_data, stdout_len, "hello world", 11);
   if (stdout)
     g_bytes_unref (stdout);
   g_free (stdout_str);
@@ -683,8 +683,7 @@ test_communicate (void)
   g_assert_no_error (error);
   stdout_data = g_bytes_get_data (stdout, &stdout_len);
 
-  g_assert_cmpint (stdout_len, ==, 11);
-  g_assert (memcmp (stdout_data, "hello world", 11) == 0);
+  g_assert_cmpmem (stdout_data, stdout_len, "hello world", 11);
   g_bytes_unref (stdout);
   
   g_bytes_unref (input);
diff --git a/gio/tests/readwrite.c b/gio/tests/readwrite.c
index ad156d3..9f674c3 100644
--- a/gio/tests/readwrite.c
+++ b/gio/tests/readwrite.c
@@ -46,9 +46,7 @@ verify_iostream (GFileIOStream *file_iostream)
 
   res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
   g_assert (res);
-  g_assert_cmpint ((int)n_bytes, ==, 20);
-
-  g_assert (memcmp (buffer, original_data, 20) == 0);
+  g_assert_cmpmem (buffer, n_bytes, original_data, 20);
 
   verify_pos (iostream, 20);
 
@@ -60,8 +58,7 @@ verify_iostream (GFileIOStream *file_iostream)
 
   res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
   g_assert (res);
-  g_assert_cmpint ((int)n_bytes, ==, 10);
-  g_assert (memcmp (buffer, original_data + strlen (original_data) - 10, 10) == 0);
+  g_assert_cmpmem (buffer, n_bytes, original_data + strlen (original_data) - 10, 10);
 
   verify_pos (iostream, strlen (original_data));
 
@@ -118,8 +115,7 @@ verify_iostream (GFileIOStream *file_iostream)
 
   res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
   g_assert (res);
-  g_assert_cmpint ((int)n_bytes, ==, 15);
-  g_assert (memcmp (buffer, modified_data, 15) == 0);
+  g_assert_cmpmem (buffer, n_bytes, modified_data, 15);
 
   error = NULL;
   res = g_output_stream_write_all (out, new_data, strlen (new_data),
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index 0357ee9..ac4c196 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -88,11 +88,11 @@
  * "assertions", which consists of running the test_assertions function.
  *
  * In addition to the traditional g_assert(), the test framework provides
- * an extended set of assertions for string and numerical comparisons:
- * g_assert_cmpfloat(), g_assert_cmpint(), g_assert_cmpuint(),
- * g_assert_cmphex(), g_assert_cmpstr(). The advantage of these variants
- * over plain g_assert() is that the assertion messages can be more
- * elaborate, and include the values of the compared entities.
+ * an extended set of assertions for comparisons: g_assert_cmpfloat(),
+ * g_assert_cmpint(), g_assert_cmpuint(), g_assert_cmphex(),
+ * g_assert_cmpstr(), and g_assert_cmpmem(). The advantage of these
+ * variants over plain g_assert() is that the assertion messages can be
+ * more elaborate, and include the values of the compared entities.
  *
  * GLib ships with two utilities called [gtester][gtester] and
  * [gtester-report][gtester-report] to facilitate running tests and producing
@@ -545,6 +545,29 @@
  */
 
 /**
+ * g_assert_cmpmem:
+ * @m1: pointer to a buffer
+ * @l1: length of @m1
+ * @m2: pointer to another buffer
+ * @l2: length of @m2
+ *
+ * Debugging macro to compare memory regions. If the comparison fails,
+ * an error message is logged and the application is either terminated
+ * or the testcase marked as failed.
+ *
+ * The effect of `g_assert_cmpmem (m1, l1, m2, l2)` is
+ * the same as `g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)`.
+ * The advantage of this macro is that it can produce a message that
+ * includes the actual values of @l1 and @l2.
+ *
+ * |[<!-- language="C" -->
+ *   g_assert_cmpmem (buf->data, buf->len, expected, sizeof (expected));
+ * ]|
+ *
+ * Since: 2.46
+ */
+
+/**
  * g_assert_no_error:
  * @err: a #GError, possibly %NULL
  *
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 6e1d786..3f56f0a 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -68,6 +68,16 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); \
                                         } G_STMT_END
+#define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
+                                             gconstpointer __m1 = m1, __m2 = m2; \
+                                             int __l1 = l1, __l2 = l2; \
+                                             if (__l1 != __l2) \
+                                               g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
+                                                                           #l1 " (len(" #m1 ")) == " #l2 " 
(len(" #m2 "))", __l1, "==", __l2, 'i'); \
+                                             else if (memcmp (__m1, __m2, __l1) != 0) \
+                                               g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
+                                                                    "assertion failed (" #m1 " == " #m2 
")"); \
+                                        } G_STMT_END
 #define g_assert_no_error(err)          G_STMT_START { \
                                              if (err) \
                                                g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
diff --git a/glib/tests/base64.c b/glib/tests/base64.c
index 3d3ba60..ffb00d5 100644
--- a/glib/tests/base64.c
+++ b/glib/tests/base64.c
@@ -57,10 +57,7 @@ test_incremental (gboolean line_break,
       len -= chunk_len;
     }
 
-  /* Check decoded length */
-  g_assert_cmpint (decoded_len, ==, length);
-  /* Check decoded data */
-  g_assert (memcmp (data, data2, length) == 0);
+  g_assert_cmpmem (data, length, data2, decoded_len);
 
   g_free (text);
   g_free (data2);
@@ -94,10 +91,7 @@ test_full (gconstpointer d)
   data2 = g_base64_decode (text, &len);
   g_free (text);
 
-  /* Check decoded length */
-  g_assert_cmpint (len, ==, length);
-  /* Check decoded base64 data */
-  g_assert (memcmp (data, data2, length) == 0);
+  g_assert_cmpmem (data, length, data2, len);
 
   g_free (data2);
 }
@@ -249,13 +243,9 @@ decode_and_compare (const gchar            *datap,
 {
   guchar *data2;
   gsize len;
-  int memcmp_decode;
 
   data2 = g_base64_decode (datap, &len);
-  g_assert_cmpint (len, ==, p->length);
-  /* g_printerr ("length: got %d, expected %d\n",len, length); */
-  memcmp_decode = memcmp (p->data, data2, p->length);
-  g_assert_cmpint (memcmp_decode, ==, 0);
+  g_assert_cmpmem (p->data, p->length, data2, len);
   g_free (data2);
 }
 
@@ -266,14 +256,10 @@ decode_inplace_and_compare (const gchar            *datap,
   gchar *data;
   guchar *data2;
   gsize len;
-  int memcmp_decode;
 
   data = g_strdup (datap);
   data2 = g_base64_decode_inplace (data, &len);
-  g_assert_cmpint (len, ==, p->length);
-  /* g_printerr ("length: got %d, expected %d\n",len, length); */
-  memcmp_decode = memcmp (p->data, data2, p->length);
-  g_assert_cmpint (memcmp_decode, ==, 0);
+  g_assert_cmpmem (p->data, p->length, data2, len);
   g_free (data2);
 }
 
@@ -361,8 +347,7 @@ test_base64_decode_smallblock (gconstpointer blocksize_p)
 
       decoded_atonce = g_base64_decode (str, &decoded_atonce_size);
 
-      g_assert_cmpint (decoded_size, ==, decoded_atonce_size);
-      g_assert (memcmp (decoded, decoded_atonce, decoded_size) == 0);
+      g_assert_cmpmem (decoded, decoded_size, decoded_atonce, decoded_atonce_size);
       
       g_free (decoded);
       g_free (decoded_atonce);
diff --git a/glib/tests/bytes.c b/glib/tests/bytes.c
index 044450d..58f3bed 100644
--- a/glib/tests/bytes.c
+++ b/glib/tests/bytes.c
@@ -33,7 +33,7 @@ test_new (void)
   g_assert (g_bytes_get_data (bytes, &size) != data);
   g_assert_cmpuint (size, ==, 4);
   g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4);
-  g_assert (memcmp (data, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes)) == 0);
+  g_assert_cmpmem (data, 4, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
 
   g_bytes_unref (bytes);
 }
@@ -84,10 +84,9 @@ test_new_from_bytes (void)
 
   g_assert (sub != NULL);
   g_assert (g_bytes_get_data (sub, NULL) == ((gchar *)g_bytes_get_data (bytes, NULL)) + 10);
-  g_assert (g_bytes_get_size (sub) == 4);
   g_bytes_unref (bytes);
 
-  g_assert (memcmp (g_bytes_get_data (sub, NULL), "wave", 4) == 0);
+  g_assert_cmpmem (g_bytes_get_data (sub, NULL), g_bytes_get_size (sub), "wave", 4);
   g_bytes_unref (sub);
 }
 
@@ -212,8 +211,7 @@ test_to_data_transferred (void)
   memory = g_bytes_get_data (bytes, NULL);
   data = g_bytes_unref_to_data (bytes, &size);
   g_assert (data == memory);
-  g_assert_cmpuint (size, ==, N_NYAN);
-  g_assert (memcmp (data, NYAN, N_NYAN) == 0);
+  g_assert_cmpmem (data, size, NYAN, N_NYAN);
   g_free (data);
 }
 
@@ -231,8 +229,7 @@ test_to_data_two_refs (void)
   memory = g_bytes_get_data (bytes, NULL);
   data = g_bytes_unref_to_data (bytes, &size);
   g_assert (data != memory);
-  g_assert_cmpuint (size, ==, N_NYAN);
-  g_assert (memcmp (data, NYAN, N_NYAN) == 0);
+  g_assert_cmpmem (data, size, NYAN, N_NYAN);
   g_free (data);
   g_assert (g_bytes_get_data (bytes, &size) == memory);
   g_assert_cmpuint (size, ==, N_NYAN);
@@ -252,8 +249,7 @@ test_to_data_non_malloc (void)
   g_assert (g_bytes_get_data (bytes, NULL) == NYAN);
   data = g_bytes_unref_to_data (bytes, &size);
   g_assert (data != (gpointer)NYAN);
-  g_assert_cmpuint (size, ==, N_NYAN);
-  g_assert (memcmp (data, NYAN, N_NYAN) == 0);
+  g_assert_cmpmem (data, size, NYAN, N_NYAN);
   g_free (data);
 }
 
@@ -270,8 +266,7 @@ test_to_array_transferred (void)
   array = g_bytes_unref_to_array (bytes);
   g_assert (array != NULL);
   g_assert (array->data == memory);
-  g_assert_cmpuint (array->len, ==, N_NYAN);
-  g_assert (memcmp (array->data, NYAN, N_NYAN) == 0);
+  g_assert_cmpmem (array->data, array->len, NYAN, N_NYAN);
   g_byte_array_unref (array);
 }
 
@@ -290,8 +285,7 @@ test_to_array_two_refs (void)
   array = g_bytes_unref_to_array (bytes);
   g_assert (array != NULL);
   g_assert (array->data != memory);
-  g_assert_cmpuint (array->len, ==, N_NYAN);
-  g_assert (memcmp (array->data, NYAN, N_NYAN) == 0);
+  g_assert_cmpmem (array->data, array->len, NYAN, N_NYAN);
   g_byte_array_unref (array);
   g_assert (g_bytes_get_data (bytes, &size) == memory);
   g_assert_cmpuint (size, ==, N_NYAN);
@@ -311,8 +305,7 @@ test_to_array_non_malloc (void)
   array = g_bytes_unref_to_array (bytes);
   g_assert (array != NULL);
   g_assert (array->data != (gpointer)NYAN);
-  g_assert_cmpuint (array->len, ==, N_NYAN);
-  g_assert (memcmp (array->data, NYAN, N_NYAN) == 0);
+  g_assert_cmpmem (array->data, array->len, NYAN, N_NYAN);
   g_byte_array_unref (array);
 }
 
diff --git a/glib/tests/checksum.c b/glib/tests/checksum.c
index b6ae3ef..347b41d 100644
--- a/glib/tests/checksum.c
+++ b/glib/tests/checksum.c
@@ -868,8 +868,7 @@ test_checksum_reset (gconstpointer d)
       g_assert_cmpint (len, ==, len2);
       digest2 = g_malloc (len2);
       g_checksum_get_digest (checksum, digest2, &len2);
-      g_assert_cmpint (len, ==, len2);
-      g_assert (memcmp (digest, digest2, len) == 0);
+      g_assert_cmpmem (digest, len, digest2, len);
       g_free (digest);
       g_free (digest2);
 
diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c
index e13aa82..889b28b 100644
--- a/glib/tests/gvariant.c
+++ b/glib/tests/gvariant.c
@@ -2224,8 +2224,7 @@ test_byteswap (void)
 
   g_variant_serialised_byteswap (two);
 
-  g_assert_cmpint (one.size, ==, two.size);
-  g_assert (memcmp (one.data, two.data, one.size) == 0);
+  g_assert_cmpmem (one.data, one.size, two.data, two.size);
 
   tree_instance_free (tree);
   g_free (one.data);
diff --git a/glib/tests/hmac.c b/glib/tests/hmac.c
index cb5cc0d..269c3e4 100644
--- a/glib/tests/hmac.c
+++ b/glib/tests/hmac.c
@@ -352,8 +352,7 @@ test_hmac (HmacCase *t)
   g_hmac_update (hmac, t->data, t->data_len);
   g_hmac_get_digest (hmac, digest, &digest_len);
 
-  g_assert_cmpuint (digest_len, ==, hmac_len);
-  g_assert (memcmp (digest, t->result, digest_len) == 0);
+  g_assert_cmpmem (digest, hmac_len, t->result, digest_len);
 
   g_free (digest);
   g_hmac_unref (hmac);
diff --git a/glib/tests/strfuncs.c b/glib/tests/strfuncs.c
index 694b124..ad14a3c 100644
--- a/glib/tests/strfuncs.c
+++ b/glib/tests/strfuncs.c
@@ -1262,8 +1262,7 @@ test_bounds (void)
   tmp = g_strjoinv (".", array);
   g_strfreev (array);
 
-  g_assert_cmpint (strlen (tmp), ==, 4095);
-  g_assert (memcmp (tmp, string, 4095) == 0);
+  g_assert_cmpmem (tmp, strlen (tmp), string, 4095);
   g_free (tmp);
 
   tmp = g_strconcat (string, string, string, NULL);
diff --git a/glib/tests/string.c b/glib/tests/string.c
index ba55d3d..017fdff 100644
--- a/glib/tests/string.c
+++ b/glib/tests/string.c
@@ -423,13 +423,11 @@ test_string_nul_handling (void)
   g_assert (!g_string_equal (string1, string2));
   g_assert (string1->len == 8);
   g_string_append (string1, "yzzy");
-  g_assert (string1->len == 12);
-  g_assert (memcmp (string1->str, "fiddle\0xyzzy", 13) == 0);
+  g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
   g_string_insert (string1, 1, "QED");
-  g_assert (memcmp (string1->str, "fQEDiddle\0xyzzy", 16) == 0);
+  g_assert_cmpmem (string1->str, string1->len + 1, "fQEDiddle\0xyzzy", 16);
   g_string_printf (string1, "fiddle%cxyzzy", '\0');
-  g_assert (string1->len == 12);
-  g_assert (memcmp (string1->str, "fiddle\0xyzzy", 13) == 0);
+  g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
 
   g_string_free (string1, TRUE);
   g_string_free (string2, TRUE);
@@ -490,7 +488,7 @@ test_string_to_bytes (void)
 
   g_assert_cmpint (byte_len, ==, 7);
 
-  g_assert_cmpint (memcmp (byte_data, "foo-bar", byte_len), ==, 0);
+  g_assert_cmpmem (byte_data, byte_len, "foo-bar", 7);
 
   g_bytes_unref (bytes);
 }
diff --git a/glib/tests/testing.c b/glib/tests/testing.c
index 85acb4a..3913506 100644
--- a/glib/tests/testing.c
+++ b/glib/tests/testing.c
@@ -47,6 +47,20 @@ test_assertions_bad_cmpint (void)
 }
 
 static void
+test_assertions_bad_cmpmem_len (void)
+{
+  g_assert_cmpmem ("foo", 3, "foot", 4);
+  exit (0);
+}
+
+static void
+test_assertions_bad_cmpmem_data (void)
+{
+  g_assert_cmpmem ("foo", 3, "fzz", 3);
+  exit (0);
+}
+
+static void
 test_assertions (void)
 {
   gchar *fuu;
@@ -66,6 +80,7 @@ test_assertions (void)
   g_assert_cmpstr ("foo", <, "fzz");
   g_assert_cmpstr ("fzz", >, "faa");
   g_assert_cmpstr ("fzz", ==, "fzz");
+  g_assert_cmpmem ("foo", 3, "foot", 3);
 
   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstr", 0, 0);
   g_test_trap_assert_failed ();
@@ -74,6 +89,15 @@ test_assertions (void)
   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpint", 0, 0);
   g_test_trap_assert_failed ();
   g_test_trap_assert_stderr ("*assertion failed*");
+
+  g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_len", 0, 0);
+  g_test_trap_assert_failed ();
+  g_test_trap_assert_stderr ("*assertion failed*len*");
+
+  g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_data", 0, 0);
+  g_test_trap_assert_failed ();
+  g_test_trap_assert_stderr ("*assertion failed*");
+  g_test_trap_assert_stderr_unmatched ("*assertion failed*len*");
 }
 
 /* test g_test_timer* API */
@@ -714,6 +738,8 @@ main (int   argc,
   g_test_add_func ("/misc/assertions", test_assertions);
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
   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_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
   g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, 
fixturetest_teardown);
   if (g_test_perf())


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