Skip to content

Commit

Permalink
tests/posix: Add tests for swab
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennisbonke committed Oct 8, 2024
1 parent 2edd963 commit 3f650b6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ all_test_cases = [
'posix/mkstemp',
'posix/waitid',
'posix/usershell',
'posix/swab',
'glibc/getopt',
'glibc/ffsl-ffsll',
'glibc/error_message_count',
Expand Down
62 changes: 62 additions & 0 deletions tests/posix/swab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <assert.h>
#include <unistd.h>

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;
}

0 comments on commit 3f650b6

Please sign in to comment.