[PATCH xserver] Add unit tests for the byte swapping macros.
Peter Hutterer
peter.hutterer at who-t.net
Wed Mar 29 00:19:10 UTC 2017
On Tue, Mar 28, 2017 at 01:17:21PM -0700, Eric Anholt wrote:
> Peter noted a weirdness in my new bswap code, which could use some
> tests to justify it.
>
> Signed-off-by: Eric Anholt <eric at anholt.net>
Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net>
thanks
Cheers,
Peter
> ---
> test/misc.c | 43 +++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 43 insertions(+)
>
> diff --git a/test/misc.c b/test/misc.c
> index ae46ccccbab9..c10a2b935bc4 100644
> --- a/test/misc.c
> +++ b/test/misc.c
> @@ -192,6 +192,48 @@ dix_request_size_checks(void)
> assert(rc == Success);
> }
>
> +static void
> +bswap_test(void)
> +{
> + const uint16_t test_16 = 0xaabb;
> + const uint16_t expect_16 = 0xbbaa;
> + const uint32_t test_32 = 0xaabbccdd;
> + const uint32_t expect_32 = 0xddccbbaa;
> + const uint64_t test_64 = 0x11223344aabbccddull;
> + const uint64_t expect_64 = 0xddccbbaa44332211ull;
> + uint16_t result_16;
> + uint32_t result_32;
> + uint64_t result_64;
> + unsigned buffer[sizeof(test_64) + 4];
> + void *unaligned = &buffer[1];
> +
> + assert(bswap_16(test_16) == expect_16);
> + assert(bswap_32(test_32) == expect_32);
> + assert(bswap_64(test_64) == expect_64);
> +
> + /* Test the swapping-in-a-pointer functions, with unaligned
> + * addresses (the functions shouldn't cause traps in that case).
> + */
> + for (int i = 0; i < 2; i++) {
> + unaligned = buffer + i;
> + if (((uintptr_t)unaligned & 1) == 1)
> + break;
> + }
> + memcpy(unaligned, &test_16, sizeof(test_16));
> + swaps((uint16_t *)unaligned);
> + memcpy(&result_16, unaligned, sizeof(result_16));
> + assert(result_16 == expect_16);
> +
> + memcpy(unaligned, &test_32, sizeof(test_32));
> + swapl((uint32_t *)unaligned);
> + memcpy(&result_32, unaligned, sizeof(result_32));
> + assert(result_32 == expect_32);
> +
> + memcpy(unaligned, &test_64, sizeof(test_64));
> + swapll((uint64_t *)unaligned);
> + memcpy(&result_64, unaligned, sizeof(result_64));
> + assert(result_64 == expect_64);
> +}
>
> int
> misc_test(void)
> @@ -199,6 +241,7 @@ misc_test(void)
> dix_version_compare();
> dix_update_desktop_dimensions();
> dix_request_size_checks();
> + bswap_test();
>
> return 0;
> }
> --
> 2.11.0
>
> _______________________________________________
> xorg-devel at lists.x.org: X.Org development
> Archives: http://lists.x.org/archives/xorg-devel
> Info: https://lists.x.org/mailman/listinfo/xorg-devel
>
More information about the xorg-devel
mailing list