From: Gerhard Sittig Date: Sun, 10 May 2020 20:37:29 +0000 (+0200) Subject: tests: also cover endianess conversion helpers X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=f2a9a7c2c8a5fe18b2bb8dcfbfba109c8356b526;p=libsigrok.git tests: also cover endianess conversion helpers Introduce a new tests/conv.c source file which exercises the endianess conversion macros. It's assumed that some use cases may break their operation, fortunately these edge cases were not seen before, or the unreliable operation went unnoticed. This test raises awareness of the implementation's constraints. This is a start, the test sequence will benefit from adding some more cases to increase coverage. --- diff --git a/Makefile.am b/Makefile.am index 2e111e93..dd281c5b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -715,7 +715,8 @@ tests_main_SOURCES = \ tests/driver_all.c \ tests/device.c \ tests/trigger.c \ - tests/analog.c + tests/analog.c \ + tests/conv.c tests_main_LDADD = libsigrok.la $(SR_EXTRA_LIBS) $(TESTS_LIBS) diff --git a/tests/conv.c b/tests/conv.c new file mode 100644 index 00000000..f3e47a13 --- /dev/null +++ b/tests/conv.c @@ -0,0 +1,78 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2020 Gerhard Sittig + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include +#include +#include +#include +#include +#include "lib.h" +#include "libsigrok-internal.h" + +static const uint8_t buff1234[] = { + 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, +}; + +START_TEST(test_endian_macro) +{ + const uint8_t *p8; + const uint16_t *p16; + const uint32_t *p32; + + p8 = (const void *)&buff1234[0]; + fail_unless(R8(&p8[0]) == 0x11); + fail_unless(R8(&p8[1]) == 0x22); + fail_unless(R8(&p8[2]) == 0x33); + fail_unless(R8(&p8[3]) == 0x44); + + p16 = (const void *)&buff1234[0]; + fail_unless(RB16(&p16[0]) == 0x1122); + fail_unless(RB16(&p16[1]) == 0x3344); + + p16 = (const void *)&buff1234[0]; + fail_unless(RL16(&p16[0]) == 0x2211); + fail_unless(RL16(&p16[1]) == 0x4433); + + p32 = (const void *)&buff1234[0]; + fail_unless(RB32(&p32[0]) == 0x11223344); + fail_unless(RB32(&p32[1]) == 0x55667788); + + p32 = (const void *)&buff1234[0]; + fail_unless(RL32(&p32[0]) == 0x44332211); + fail_unless(RL32(&p32[1]) == 0x88776655); + + p16 = (const void *)&buff1234[0]; + fail_unless(RB16(p16++) == 0x1122); + fail_unless(RB16(p16++) == 0x3344); +} +END_TEST + +Suite *suite_conv(void) +{ + Suite *s; + TCase *tc; + + s = suite_create("conv"); + + tc = tcase_create("endian"); + tcase_add_test(tc, test_endian_macro); + suite_add_tcase(s, tc); + + return s; +} diff --git a/tests/lib.h b/tests/lib.h index f2dd6291..cc027ab6 100644 --- a/tests/lib.h +++ b/tests/lib.h @@ -53,5 +53,6 @@ Suite *suite_version(void); Suite *suite_device(void); Suite *suite_trigger(void); Suite *suite_analog(void); +Suite *suite_conv(void); #endif diff --git a/tests/main.c b/tests/main.c index 5c295bfa..88783d0c 100644 --- a/tests/main.c +++ b/tests/main.c @@ -45,6 +45,7 @@ int main(void) srunner_add_suite(srunner, suite_device()); srunner_add_suite(srunner, suite_trigger()); srunner_add_suite(srunner, suite_analog()); + srunner_add_suite(srunner, suite_conv()); srunner_run_all(srunner, CK_VERBOSE); ret = srunner_ntests_failed(srunner);