[glib: 3/4] tests: Add a test for GBytes memory transfer with an odd free func
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 3/4] tests: Add a test for GBytes memory transfer with an odd free func
- Date: Wed, 16 Jun 2021 13:51:41 +0000 (UTC)
commit b63a3189e7f3fdaf3ce8b3dc9d89c31df09765e5
Author: Philip Withnall <pwithnall endlessos org>
Date: Fri Jun 11 12:27:11 2021 +0100
tests: Add a test for GBytes memory transfer with an odd free func
This is basically a contrived test to trigger the `bytes->user_data !=
bytes->data` condition (and none of the earlier short-circuiting
conditions in that statement) in `try_steal_and_unref()`. This gives
100% line and branch coverage for `gbytes.c`.
Signed-off-by: Philip Withnall <pwithnall endlessos org>
glib/tests/bytes.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
---
diff --git a/glib/tests/bytes.c b/glib/tests/bytes.c
index 534687fe6..8178bc2a8 100644
--- a/glib/tests/bytes.c
+++ b/glib/tests/bytes.c
@@ -315,6 +315,30 @@ test_to_data_non_malloc (void)
g_free (data);
}
+static void
+test_to_data_different_free_func (void)
+{
+ gpointer data;
+ gsize size;
+ GBytes *bytes;
+ gchar *sentinel = g_strdup ("hello");
+
+ /* Memory copied: free func and user_data don’t point to the bytes data */
+ bytes = g_bytes_new_with_free_func (NYAN, N_NYAN, g_free, sentinel);
+ g_assert_true (g_bytes_get_data (bytes, NULL) == NYAN);
+
+ data = g_bytes_unref_to_data (bytes, &size);
+ g_assert_true (data != (gpointer)NYAN);
+ g_assert_cmpmem (data, size, NYAN, N_NYAN);
+ g_free (data);
+
+ /* @sentinel should not be leaked; testing that requires this test to be run
+ * under valgrind. We can’t use a custom free func to check it isn’t leaked,
+ * as the point of this test is to hit a condition in `try_steal_and_unref()`
+ * which is short-circuited if the free func isn’t g_free().
+ * See discussion in https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2152 */
+}
+
static void
test_to_array_transferred (void)
{
@@ -475,6 +499,7 @@ main (int argc, char *argv[])
g_test_add_func ("/bytes/to-data/transferred", test_to_data_transferred);
g_test_add_func ("/bytes/to-data/two-refs", test_to_data_two_refs);
g_test_add_func ("/bytes/to-data/non-malloc", test_to_data_non_malloc);
+ g_test_add_func ("/bytes/to-data/different-free-func", test_to_data_different_free_func);
g_test_add_func ("/bytes/to-array/transferred", test_to_array_transferred);
g_test_add_func ("/bytes/to-array/transferred/oversize", test_to_array_transferred_oversize);
g_test_add_func ("/bytes/to-array/two-refs", test_to_array_two_refs);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]