summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <pwithnall@endlessos.org>2021-06-11 12:27:11 +0100
committerPhilip Withnall <pwithnall@endlessos.org>2021-06-16 12:14:58 +0100
commitb63a3189e7f3fdaf3ce8b3dc9d89c31df09765e5 (patch)
treefa06e93ed490404750a541b7b2853c602209603e
parentfc7b316bc7b236bb1d21b1337196515de4053c47 (diff)
downloadglib-b63a3189e7f3fdaf3ce8b3dc9d89c31df09765e5.tar.gz
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>
-rw-r--r--glib/tests/bytes.c25
1 files changed, 25 insertions, 0 deletions
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
@@ -316,6 +316,30 @@ test_to_data_non_malloc (void)
}
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)
{
gconstpointer memory;
@@ -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);