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..ee6b45df02 --- /dev/null +++ b/tests/posix/swab.c @@ -0,0 +1,66 @@ +#include +#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]; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstringop-overflow" + swab(input, output, -1); // Should change nothing +#pragma GCC diagnostic pop + + 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; +}