X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=tests%2Fconv.c;h=788c36728c37723581e3edc2a3825409458ce989;hp=ecbe56190f32359556ce4a751a5b1862058103ab;hb=HEAD;hpb=d770bfbbbc6af16b5ba878a97e420e5a82a88557 diff --git a/tests/conv.c b/tests/conv.c index ecbe5619..788c3672 100644 --- a/tests/conv.c +++ b/tests/conv.c @@ -34,6 +34,16 @@ static const uint8_t buff8125fb[] = { static const uint8_t buff8125fl[] = { 0x00, 0x00, 0x02, 0x41, 0x00, 0x00, 0x00, 0x00, }; +static const uint8_t buff1234large[] = { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, + 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, + 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, + 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, + 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, +}; START_TEST(test_endian_macro) { @@ -104,6 +114,36 @@ START_TEST(test_endian_read) } END_TEST +START_TEST(test_endian_read_inc_len) +{ + const uint8_t *p; + size_t l; + + /* Position to the start of the input stream. */ + p = &buff1234[0]; + l = sizeof(buff1234); + + /* Read several fields of known type and values. */ + fail_unless(l == 8); + fail_unless(read_u8_inc_len(&p, &l) == 0x11); + fail_unless(l == 7); + fail_unless(read_u8_inc_len(&p, &l) == 0x22); + fail_unless(l == 6); + fail_unless(read_u16le_inc_len(&p, &l) == 0x4433); + fail_unless(l == 4); + fail_unless(read_u16le_inc_len(&p, &l) == 0x6655); + fail_unless(l == 2); + fail_unless(read_u16le_inc_len(&p, &l) == 0x8877); + fail_unless(l == 0); + + /* Read beyond the end of the input stream. */ + fail_unless(read_u8_inc_len(&p, &l) == 0x0); + fail_unless(l == 0); + fail_unless(read_u16le_inc_len(&p, &l) == 0x0); + fail_unless(l == 0); +} +END_TEST + START_TEST(test_endian_read_inc) { const uint8_t *p; @@ -196,11 +236,12 @@ END_TEST START_TEST(test_endian_write_inc) { - uint8_t buff[2 * sizeof(uint64_t)]; + uint8_t buff[3 * 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); @@ -208,6 +249,22 @@ START_TEST(test_endian_write_inc) 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); + + p = &buff[0]; + write_u48le_inc(&p, 0x060504030201); + write_u48le_inc(&p, 0x0c0b0a090807); + write_u48le_inc(&p, 0x1211100f0e0d); + write_u48le_inc(&p, 0x181716151413); + l = p - &buff[0]; + fail_unless(l == 4 * 48 / 8 * sizeof(uint8_t)); + fail_unless(memcmp(&buff[0], &buff1234large[0], l) == 0); + + p = &buff[0]; + write_u24le_inc(&p, 0xfe030201); + write_u40le_inc(&p, 0xdcba0807060504ul); + l = p - &buff[0]; + fail_unless(l == 24 / 8 + 40 / 8); + fail_unless(memcmp(&buff[0], &buff1234large[0], l) == 0); } END_TEST @@ -222,6 +279,7 @@ Suite *suite_conv(void) 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_read_inc_len); tcase_add_test(tc, test_endian_write); tcase_add_test(tc, test_endian_write_inc); suite_add_tcase(s, tc);