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>
This commit is contained in:
Philip Withnall 2021-06-11 12:27:11 +01:00
parent fc7b316bc7
commit b63a3189e7

View File

@ -315,6 +315,30 @@ test_to_data_non_malloc (void)
g_free (data); 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 dont 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 cant use a custom free func to check it isnt 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 isnt g_free().
* See discussion in https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2152 */
}
static void static void
test_to_array_transferred (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/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/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/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", 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/transferred/oversize", test_to_array_transferred_oversize);
g_test_add_func ("/bytes/to-array/two-refs", test_to_array_two_refs); g_test_add_func ("/bytes/to-array/two-refs", test_to_array_two_refs);