tests: Fix -Wsign-conversion warnings for random ints in gobject tests

There’s a painful inconsistency in the types of the
`g_{test_rand,random,rand}_int{,_range}()` functions, which vary
arbitrarily between `gint32` and `guint32`.

Unfortunately since those functions mention `int` explicitly in the name
(and then some of them return an `unsigned` integer), I don’t see a way
to make the APIs consistent without significant deprecations or
additions.

So, for the moment, to fix various `-Wsign-conversion` warnings, plaster
the tests with casts.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
This commit is contained in:
Philip Withnall 2025-04-11 17:03:23 +01:00
parent ba4f5ca44e
commit e27d64e651
No known key found for this signature in database
GPG Key ID: C5C42CFB268637CA
3 changed files with 10 additions and 10 deletions

View File

@ -790,7 +790,7 @@ test_weak_ref_many (void)
/* We register them in a somewhat juggled order. That's because below, we will clear them
* again, and we don't want to always clear them in the same order as they were registered.
* For that, we calculate the actual index by jumping around by adding a prime number. */
j = (g_test_rand_int () % (N + 1));
j = ((guint) g_test_rand_int () % (N + 1));
for (i = 0; i < N; i++)
{
j = (j + PRIME) % N;
@ -805,7 +805,7 @@ test_weak_ref_many (void)
g_assert_null (g_weak_ref_get (&weak_ref1));
}
n = g_test_rand_int () % (N + 1u);
n = (guint) g_test_rand_int () % (N + 1u);
for (i = 0; i < N; i++)
g_weak_ref_set (&weak_refs[i], i < n ? NULL : obj);
@ -972,9 +972,9 @@ test_weak_ref_concurrent (gconstpointer testdata)
for (i = 0; i < CONCURRENT_N_THREADS; i++)
{
const guint32 rnd_seed[] = {
g_test_rand_int (),
g_test_rand_int (),
g_test_rand_int (),
(guint32) g_test_rand_int (),
(guint32) g_test_rand_int (),
(guint32) g_test_rand_int (),
};
thread_data[i] = (ConcurrentThreadData){

View File

@ -147,7 +147,7 @@ test_disconnect_many_random (void)
for (i = 0; i < n_handlers; i++)
{
j = g_test_rand_int_range (0, n_handlers);
j = (guint) g_test_rand_int_range (0, (int) n_handlers);
id = handlers[i];
handlers[i] = handlers[j];
handlers[j] = id;
@ -189,7 +189,7 @@ test_disconnect_2_signals (void)
for (i = 0; i < n_handlers; i++)
{
j = g_test_rand_int_range (0, n_handlers);
j = (guint) g_test_rand_int_range (0, (int) n_handlers);
id = handlers[i];
handlers[i] = handlers[j];
handlers[j] = id;
@ -240,7 +240,7 @@ test_disconnect_2_objects (void)
for (i = 0; i < n_handlers; i++)
{
j = g_test_rand_int_range (0, n_handlers);
j = (guint) g_test_rand_int_range (0, (int) n_handlers);
id = handlers[i];
handlers[i] = handlers[j];
handlers[j] = id;
@ -282,7 +282,7 @@ test_block_many (void)
for (i = 0; i < n_handlers; i++)
{
j = g_test_rand_int_range (0, n_handlers);
j = (guint) g_test_rand_int_range (0, (int) n_handlers);
id = handlers[i];
handlers[i] = handlers[j];
handlers[j] = id;

View File

@ -217,7 +217,7 @@ my_test_do_signal3 (GTest * test)
static void
my_test_do_prop (GTest * test)
{
test->value = g_random_int ();
test->value = (int) g_random_int ();
g_object_notify (G_OBJECT (test), "test-prop");
}