]> sigrok.org Git - libsigrok.git/commitdiff
tests: also cover endianess conversion helpers
authorGerhard Sittig <redacted>
Sun, 10 May 2020 20:37:29 +0000 (22:37 +0200)
committerGerhard Sittig <redacted>
Fri, 29 May 2020 04:09:47 +0000 (06:09 +0200)
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.

Makefile.am
tests/conv.c [new file with mode: 0644]
tests/lib.h
tests/main.c

index 2e111e9341ef9953ce59c7577265a10b39d4d9db..dd281c5b8e18a31291c838c8f82592e87a704408 100644 (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)
 
diff --git a/tests/conv.c b/tests/conv.c
new file mode 100644 (file)
index 0000000..f3e47a1
--- /dev/null
@@ -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;
+}
index f2dd6291169aa0183e84dc17ab4e7d560c33a714..cc027ab67e8a8a4a8ea3ec2de45649c18b8d1801 100644 (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
index 5c295bfa1c5402a63d2efa93cc81cf1d92c9f6d1..88783d0c262844a185804abde181cbda65b6b879 100644 (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);