Add unit tests for the byte swapping macros.
Peter noted a weirdness in my new bswap code, which could use some tests to justify it. Signed-off-by: Eric Anholt <eric@anholt.net> Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
563b6ee873
commit
4552238960
43
test/misc.c
43
test/misc.c
|
@ -192,6 +192,48 @@ dix_request_size_checks(void)
|
||||||
assert(rc == Success);
|
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
|
int
|
||||||
misc_test(void)
|
misc_test(void)
|
||||||
|
@ -199,6 +241,7 @@ misc_test(void)
|
||||||
dix_version_compare();
|
dix_version_compare();
|
||||||
dix_update_desktop_dimensions();
|
dix_update_desktop_dimensions();
|
||||||
dix_request_size_checks();
|
dix_request_size_checks();
|
||||||
|
bswap_test();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue