tests: extend endianess conversion tests to also cover inline routines

Cover the recently introduced inline routines which back the preprocessor
macros for endianess conversion. Add test sequences for read and write
routines for different data types of different sizes, different endianess
formats and signedness, and include those routines which increment the
read/write position.
This commit is contained in:
Gerhard Sittig 2020-05-27 19:53:09 +02:00
parent 080b6bcf09
commit d770bfbbbc
1 changed files with 152 additions and 0 deletions

View File

@ -28,6 +28,12 @@
static const uint8_t buff1234[] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
};
static const uint8_t buff8125fb[] = {
0x41, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
static const uint8_t buff8125fl[] = {
0x00, 0x00, 0x02, 0x41, 0x00, 0x00, 0x00, 0x00,
};
START_TEST(test_endian_macro)
{
@ -63,6 +69,148 @@ START_TEST(test_endian_macro)
}
END_TEST
START_TEST(test_endian_read)
{
fail_unless(read_u8(&buff1234[0]) == 0x11);
fail_unless(read_u8(&buff1234[3]) == 0x44);
fail_unless(read_u8(&buff1234[7]) == 0x88);
fail_unless(read_u16be(&buff1234[0]) == 0x1122);
fail_unless(read_u16be(&buff1234[6]) == 0x7788);
fail_unless(read_u16le(&buff1234[0]) == 0x2211);
fail_unless(read_u16le(&buff1234[6]) == 0x8877);
fail_unless(read_i16be(&buff1234[6]) == 0x7788);
fail_unless(read_i16le(&buff1234[6]) == (int16_t)0x8877);
fail_unless(read_u32be(&buff1234[0]) == 0x11223344);
fail_unless(read_u32be(&buff1234[4]) == 0x55667788);
fail_unless(read_u32le(&buff1234[0]) == 0x44332211);
fail_unless(read_u32le(&buff1234[4]) == 0x88776655);
fail_unless(read_i32be(&buff1234[0]) == 0x11223344);
fail_unless(read_i32be(&buff1234[4]) == 0x55667788);
fail_unless(read_i32le(&buff1234[4]) == (int32_t)0x88776655ull);
fail_unless(read_u64be(&buff1234[0]) == 0x1122334455667788ull);
fail_unless(read_u64le(&buff1234[0]) == 0x8877665544332211ull);
fail_unless(read_i64be(&buff1234[0]) == 0x1122334455667788ull);
fail_unless(read_i64le(&buff1234[0]) == (int64_t)0x8877665544332211ull);
fail_unless(read_fltbe(&buff8125fb[0]) == 8.125);
fail_unless(read_fltle(&buff8125fl[0]) == 8.125);
}
END_TEST
START_TEST(test_endian_read_inc)
{
const uint8_t *p;
p = &buff1234[0];
fail_unless(read_u8_inc(&p) == 0x11);
fail_unless(read_u8_inc(&p) == 0x22);
fail_unless(read_u8_inc(&p) == 0x33);
fail_unless(p == &buff1234[3 * sizeof(uint8_t)]);
p = &buff1234[0];
fail_unless(read_u16be_inc(&p) == 0x1122);
fail_unless(read_u16be_inc(&p) == 0x3344);
fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
p = &buff1234[0];
fail_unless(read_u16le_inc(&p) == 0x2211);
fail_unless(read_u16le_inc(&p) == 0x4433);
fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
p = &buff1234[0];
fail_unless(read_u24le_inc(&p) == 0x332211);
fail_unless(read_u24le_inc(&p) == 0x665544);
fail_unless(p == &buff1234[2 * 3 * sizeof(uint8_t)]);
p = &buff1234[0];
fail_unless(read_u32be_inc(&p) == 0x11223344ul);
fail_unless(read_u32be_inc(&p) == 0x55667788ul);
fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
p = &buff1234[0];
fail_unless(read_u32le_inc(&p) == 0x44332211ul);
fail_unless(read_u32le_inc(&p) == 0x88776655ul);
fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
p = &buff1234[0];
fail_unless(read_u64be_inc(&p) == 0x1122334455667788);
fail_unless(p == &buff1234[sizeof(uint64_t)]);
p = &buff1234[0];
fail_unless(read_u64le_inc(&p) == 0x8877665544332211ull);
fail_unless(p == &buff1234[sizeof(uint64_t)]);
}
END_TEST
START_TEST(test_endian_write)
{
uint8_t buff[2 * sizeof(uint64_t)];
memset(buff, 0, sizeof(buff));
write_u8(&buff[0], 0x11);
fail_unless(memcmp(&buff[0], &buff1234[0], sizeof(uint8_t)) == 0);
memset(buff, 0, sizeof(buff));
write_u8(&buff[0], 0x22);
write_u8(&buff[1], 0x33);
write_u8(&buff[2], 0x44);
write_u8(&buff[3], 0x55);
fail_unless(memcmp(&buff[0], &buff1234[1], 4 * sizeof(uint8_t)) == 0);
memset(buff, 0, sizeof(buff));
write_u16be(&buff[0 * sizeof(uint16_t)], 0x1122);
write_u16be(&buff[1 * sizeof(uint16_t)], 0x3344);
fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint16_t)) == 0);
memset(buff, 0, sizeof(buff));
write_u16le(&buff[0 * sizeof(uint16_t)], 0x4433);
write_u16le(&buff[1 * sizeof(uint16_t)], 0x6655);
fail_unless(memcmp(&buff[0], &buff1234[2], 2 * sizeof(uint16_t)) == 0);
memset(buff, 0, sizeof(buff));
write_u32be(&buff[0 * sizeof(uint32_t)], 0x11223344);
write_u32be(&buff[1 * sizeof(uint32_t)], 0x55667788);
fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
memset(buff, 0, sizeof(buff));
write_u32le(&buff[0 * sizeof(uint32_t)], 0x44332211);
write_u32le(&buff[1 * sizeof(uint32_t)], 0x88776655);
fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
memset(buff, 0, sizeof(buff));
write_fltbe(&buff[0], 8.125);
fail_unless(memcmp(&buff[0], &buff8125fb[0], sizeof(float)) == 0);
memset(buff, 0, sizeof(buff));
write_fltle(&buff[0], 8.125);
fail_unless(memcmp(&buff[0], &buff8125fl[0], sizeof(float)) == 0);
}
END_TEST
START_TEST(test_endian_write_inc)
{
uint8_t buff[2 * sizeof(uint64_t)];
uint8_t *p;
size_t l;
memset(buff, 0, sizeof(buff));
p = &buff[0];
write_u8_inc(&p, 0x11);
write_u16be_inc(&p, 0x2233);
write_u32be_inc(&p, 0x44556677);
l = p - &buff[0];
fail_unless(l == sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t));
fail_unless(memcmp(&buff[0], &buff1234[0], l) == 0);
}
END_TEST
Suite *suite_conv(void)
{
Suite *s;
@ -72,6 +220,10 @@ Suite *suite_conv(void)
tc = tcase_create("endian");
tcase_add_test(tc, test_endian_macro);
tcase_add_test(tc, test_endian_read);
tcase_add_test(tc, test_endian_read_inc);
tcase_add_test(tc, test_endian_write);
tcase_add_test(tc, test_endian_write_inc);
suite_add_tcase(s, tc);
return s;