From d5e6793b834a09b78a816a7001f2280c554986a2 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Dec 2021 09:59:46 +0000 Subject: [PATCH 1/2] tests: Factor out common calculations in test_basic_bits() test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This decreases the overall test time from 0.17s to 0.12s for me, and will help further in the following commit where I’m going to repeat some of these calculations again for further comparisons. Signed-off-by: Philip Withnall --- glib/tests/utils.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/glib/tests/utils.c b/glib/tests/utils.c index bd22cc612..da97a92e2 100644 --- a/glib/tests/utils.c +++ b/glib/tests/utils.c @@ -310,27 +310,32 @@ test_basic_bits (void) /* we loop like this: 0, -1, 1, -2, 2, -3, 3, ... */ for (i = 0; (glong) i < 1500; i = -(i + ((glong) i >= 0))) { + guint naive_bit_storage_i = naive_bit_storage (i); + #if TEST_BUILTINS - g_assert_cmpint (naive_bit_storage (i), ==, builtin_bit_storage (i)); + g_assert_cmpint (naive_bit_storage_i, ==, builtin_bit_storage (i)); #endif - g_assert_cmpint (naive_bit_storage (i), ==, g_bit_storage (i)); + g_assert_cmpint (naive_bit_storage_i, ==, g_bit_storage (i)); for (nth_bit = -3; nth_bit <= 2 + GLIB_SIZEOF_LONG * 8; nth_bit++) { + gint naive_bit_nth_lsf_i_nth_bit = naive_bit_nth_lsf (i, nth_bit); + gint naive_bit_nth_msf_i_nth_bit = naive_bit_nth_msf (i, nth_bit); + #if TEST_BUILTINS - g_assert_cmpint (naive_bit_nth_lsf (i, nth_bit), ==, + g_assert_cmpint (naive_bit_nth_lsf_i_nth_bit, ==, builtin_bit_nth_lsf1 (i, nth_bit)); - g_assert_cmpint (naive_bit_nth_lsf (i, nth_bit), ==, + g_assert_cmpint (naive_bit_nth_lsf_i_nth_bit, ==, builtin_bit_nth_lsf2 (i, nth_bit)); #endif - g_assert_cmpint (naive_bit_nth_lsf (i, nth_bit), ==, + g_assert_cmpint (naive_bit_nth_lsf_i_nth_bit, ==, g_bit_nth_lsf (i, nth_bit)); #if TEST_BUILTINS - g_assert_cmpint (naive_bit_nth_msf (i, nth_bit), ==, + g_assert_cmpint (naive_bit_nth_msf_i_nth_bit, ==, builtin_bit_nth_msf (i, nth_bit)); #endif - g_assert_cmpint (naive_bit_nth_msf (i, nth_bit), ==, + g_assert_cmpint (naive_bit_nth_msf_i_nth_bit, ==, g_bit_nth_msf (i, nth_bit)); } } From cbd48824bd5ecec83f07df201fc43fd266423546 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Dec 2021 10:01:17 +0000 Subject: [PATCH 2/2] tests: Test the function forms of g_bit_*() APIs too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tests were previously only checking the macro forms. The function forms should behave identically, but since it’s easy enough to get coverage of them, we might as well. Signed-off-by: Philip Withnall --- glib/tests/utils.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/glib/tests/utils.c b/glib/tests/utils.c index da97a92e2..6068cd9be 100644 --- a/glib/tests/utils.c +++ b/glib/tests/utils.c @@ -312,10 +312,18 @@ test_basic_bits (void) { guint naive_bit_storage_i = naive_bit_storage (i); + /* Test the g_bit_*() implementations against the compiler builtins (if + * available), and against a slow-but-correct ‘naive’ implementation. + * They should all agree. + * + * The macro and function versions of the g_bit_*() functions are tested, + * hence one call with the function name in brackets (to avoid it being + * expanded as a macro). */ #if TEST_BUILTINS g_assert_cmpint (naive_bit_storage_i, ==, builtin_bit_storage (i)); #endif g_assert_cmpint (naive_bit_storage_i, ==, g_bit_storage (i)); + g_assert_cmpint (naive_bit_storage_i, ==, (g_bit_storage) (i)); for (nth_bit = -3; nth_bit <= 2 + GLIB_SIZEOF_LONG * 8; nth_bit++) { @@ -330,6 +338,8 @@ test_basic_bits (void) #endif g_assert_cmpint (naive_bit_nth_lsf_i_nth_bit, ==, g_bit_nth_lsf (i, nth_bit)); + g_assert_cmpint (naive_bit_nth_lsf_i_nth_bit, ==, + (g_bit_nth_lsf) (i, nth_bit)); #if TEST_BUILTINS g_assert_cmpint (naive_bit_nth_msf_i_nth_bit, ==, @@ -337,6 +347,8 @@ test_basic_bits (void) #endif g_assert_cmpint (naive_bit_nth_msf_i_nth_bit, ==, g_bit_nth_msf (i, nth_bit)); + g_assert_cmpint (naive_bit_nth_msf_i_nth_bit, ==, + (g_bit_nth_msf) (i, nth_bit)); } } }