From 3f650b6851ca358e7c9e95f95d71382731d23627 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Wed, 9 Oct 2024 01:49:24 +0200 Subject: [PATCH] tests/posix: Add tests for swab --- tests/meson.build | 1 + tests/posix/swab.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/posix/swab.c diff --git a/tests/meson.build b/tests/meson.build index bbc0cd6491..ec1b860fce 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -98,6 +98,7 @@ all_test_cases = [ 'posix/mkstemp', 'posix/waitid', 'posix/usershell', + 'posix/swab', 'glibc/getopt', 'glibc/ffsl-ffsll', 'glibc/error_message_count', diff --git a/tests/posix/swab.c b/tests/posix/swab.c new file mode 100644 index 0000000000..12515c0d9e --- /dev/null +++ b/tests/posix/swab.c @@ -0,0 +1,62 @@ +#include +#include + +void test_swab_even_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t output[4]; + + swab(input, output, sizeof(input)); + + assert(output[0] == 0x02); + assert(output[1] == 0x01); + assert(output[2] == 0x04); + assert(output[3] == 0x03); +} + +void test_swab_odd_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04, 0x05}; + uint8_t output[5]; + + swab(input, output, sizeof(input)); + + assert(output[0] == 0x02); + assert(output[1] == 0x01); + assert(output[2] == 0x04); + assert(output[3] == 0x03); + + // Last byte is UB, assume unchanged? + assert(output[4] == 0x05); +} + +void test_swab_negative_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t output[4]; + + swab(input, output, -1); // Should change nothing + + assert(output[0] == 0x01); + assert(output[1] == 0x02); + assert(output[2] == 0x03); + assert(output[3] == 0x04); +} + +void test_swab_zero_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t output[4]; + + swab(input, output, 0); // Should change nothing + + assert(output[0] == 0x01); + assert(output[1] == 0x02); + assert(output[2] == 0x03); + assert(output[3] == 0x04); +} + +int main() { + test_swab_even_bytes(); + test_swab_odd_bytes(); + test_swab_negative_bytes(); + test_swab_zero_bytes(); + + return 0; +}