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.
This commit is contained in:
Gerhard Sittig 2020-05-10 22:37:29 +02:00
parent 883db4dad1
commit f2a9a7c2c8
4 changed files with 82 additions and 1 deletions

View File

@ -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)

78
tests/conv.c Normal file
View File

@ -0,0 +1,78 @@
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2020 Gerhard Sittig <gerhard.sittig@gmx.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <check.h>
#include <libsigrok/libsigrok.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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

View File

@ -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);