fixeria has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/35046?usp=email )


Change subject: soft_uart: add unit tests for the receiver and transmitter
......................................................................

soft_uart: add unit tests for the receiver and transmitter

Change-Id: Icdfa0c644548964d37940c32dc9dcfcfc53c3a19
Related: OS#4396
---
M tests/Makefile.am
A tests/soft_uart/soft_uart_test.c
A tests/soft_uart/soft_uart_test.ok
M tests/testsuite.at
4 files changed, 283 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/46/35046/1

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5f4914e..af17e50 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -55,6 +55,7 @@
                 v110/ra1_test                                          \
                 gsm44021/frame_csd_test                                \
                 osmo_io/osmo_io_test                                   \
+                soft_uart/soft_uart_test                               \
                 $(NULL)

 if ENABLE_MSGFILE
@@ -375,6 +376,8 @@
 
 osmo_io_osmo_io_test_SOURCES = osmo_io/osmo_io_test.c

+soft_uart_soft_uart_test_SOURCES = soft_uart/soft_uart_test.c
+

 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
@@ -693,6 +696,8 @@
        osmo_io/osmo_io_test \
                >$(srcdir)/osmo_io/osmo_io_test.ok \
                2>$(srcdir)/osmo_io/osmo_io_test.err
+       soft_uart/soft_uart_test \
+               >$(srcdir)/soft_uart/soft_uart.ok


 check-local: atconfig $(TESTSUITE)
diff --git a/tests/soft_uart/soft_uart_test.c b/tests/soft_uart/soft_uart_test.c
new file mode 100644
index 0000000..54d02b1
--- /dev/null
+++ b/tests/soft_uart/soft_uart_test.c
@@ -0,0 +1,211 @@
+/*
+ * (C) 2023 by sysmocom - s.f.m.c. GmbH <i...@sysmocom.de>
+ * Author: Vadim Yanitskiy <vyanits...@sysmocom.de>
+ *
+ * All Rights Reserved
+ *
+ * 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.
+ *
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/soft_uart.h>
+
+static void suart_rx_cb(void *priv, struct msgb *msg, unsigned int flags)
+{
+       fprintf(stdout, "%s(flags=%02x): %s\n",
+               __func__, flags, msgb_hexdump(msg));
+       msgb_free(msg);
+}
+
+static const struct osmo_soft_uart_cfg suart_default_cfg = {
+       .num_data_bits = 8,
+       .num_stop_bits = 1,
+       .parity_mode = OSMO_SUART_PARITY_NONE,
+       .tx_buf_size = 1024,
+       .rx_buf_size = 1024,
+       .rx_pull_mode = OSMO_SUART_RX_MODE_N_FRAMES,
+       .rx_pull_param = { .rx_n_uart_frames = 1 },
+       .rx_cb = &suart_rx_cb,
+};
+
+static void test_rx_exec(struct osmo_soft_uart *suart,
+                        const char *input)
+{
+       for (unsigned int i = 0; input[i] != '\0'; i++) {
+               ubit_t ubit;
+
+               switch (input[i]) {
+               case '0':
+               case '1':
+                       ubit = input[i] - '0';
+                       osmo_soft_uart_rx_ubits(suart, &ubit, 1);
+                       break;
+               case ' ': /* padding */
+                       continue;
+               default:
+                       printf("%s() @ %u: unknown opcode '%c'\n",
+                              __func__, i, input[i]);
+                       break;
+               }
+       }
+}
+
+static void test_rx(void)
+{
+       struct osmo_soft_uart_cfg cfg;
+       struct osmo_soft_uart *suart;
+
+       suart = osmo_soft_uart_alloc(NULL, &suart_default_cfg);
+       OSMO_ASSERT(suart != NULL);
+
+       osmo_soft_uart_set_rx(suart, true);
+
+       printf("======== %s(): testing 8-N-1\n", __func__);
+       test_rx_exec(suart,
+                    "111111111111" /* no data */
+                    "0 00010010 1" /* 'H' */
+                    "0 10100010 1" /* 'E' */
+                    "111111111111" /* no data */
+                    "0 00110010 1" /* 'L' */
+                    "0 00110010 1" /* 'L' */
+                    "111111111111" /* no data */
+                    "0 11110010 1" /* 'O' */
+                   );
+
+       printf("======== %s(): testing 8-N-2\n", __func__);
+       cfg = suart_default_cfg;
+       cfg.num_stop_bits = 2;
+       osmo_soft_uart_configure(suart, &cfg);
+       test_rx_exec(suart,
+                    "1111111111111" /* no data */
+                    "0 00010010 11" /* 'H' */
+                    "0 10100010 11" /* 'E' */
+                    "0 00110010 11" /* 'L' */
+                    "0 00110010 11" /* 'L' */
+                    "0 11110010 11" /* 'O' */
+                    "1111111111111" /* no data */
+                    "1111111111111" /* no data */
+                   );
+
+
+       printf("======== %s(): testing 8-E-1\n", __func__);
+       cfg = suart_default_cfg;
+       cfg.parity_mode = OSMO_SUART_PARITY_EVEN;
+       osmo_soft_uart_configure(suart, &cfg);
+       test_rx_exec(suart,
+                    "0 00000000 1 1" /* invalid parity (odd) */
+                    "0 10000000 0 1" /* invalid parity (odd) */
+                    "0 11111111 1 1" /* invalid parity (odd) */
+                    "0 00000000 0 1"
+                    "0 11111111 0 1"
+                    "0 01010101 0 1"
+                    "0 10101010 0 1"
+                    "0 00000001 1 1"
+                    "0 00000111 1 1"
+                    "0 00011111 1 1"
+                    "0 01111111 1 1"
+                   );
+
+       printf("======== %s(): testing 8-O-1\n", __func__);
+       cfg = suart_default_cfg;
+       cfg.parity_mode = OSMO_SUART_PARITY_ODD;
+       osmo_soft_uart_configure(suart, &cfg);
+       test_rx_exec(suart,
+                    "0 00000000 0 1" /* invalid parity (even) */
+                    "0 10000000 1 1" /* invalid parity (even) */
+                    "0 11111111 0 1" /* invalid parity (even) */
+                    "0 00000000 1 1"
+                    "0 11111111 1 1"
+                    "0 01010101 1 1"
+                    "0 10101010 1 1"
+                    "0 00000001 0 1"
+                    "0 00000111 0 1"
+                    "0 00011111 0 1"
+                    "0 01111111 0 1"
+                   );
+
+       osmo_soft_uart_free(suart);
+}
+
+static void test_tx_exec(struct osmo_soft_uart *suart,
+                        const char *tx_data, size_t tx_data_len,
+                        unsigned int n_bits)
+{
+       ubit_t tx_buf[64];
+
+       OSMO_ASSERT((tx_data_len * n_bits) <= sizeof(tx_buf));
+
+       osmo_soft_uart_tx(suart, (const uint8_t *)tx_data, tx_data_len);
+       osmo_soft_uart_tx_ubits(suart, &tx_buf[0], n_bits * tx_data_len);
+
+       printf("%s():", __func__);
+       for (size_t i = 0; i < (tx_data_len * n_bits); i += n_bits)
+               printf(" %s", osmo_ubit_dump(&tx_buf[i], n_bits));
+       printf("\n");
+}
+
+static void test_tx_rx(void)
+{
+       struct osmo_soft_uart_cfg cfg;
+       struct osmo_soft_uart *suart;
+
+       suart = osmo_soft_uart_alloc(NULL, &suart_default_cfg);
+       OSMO_ASSERT(suart != NULL);
+
+       osmo_soft_uart_set_tx(suart, true);
+
+       printf("======== %s(): testing 8-N-1\n", __func__);
+       test_tx_exec(suart, "\xde\xad\xbe\xef", 4, (1 + 8 + 1));
+       test_tx_exec(suart, "\x00\xaa\x55\xff", 4, (1 + 8 + 1));
+
+       printf("======== %s(): testing 8-E-1\n", __func__);
+       cfg = suart_default_cfg;
+       cfg.parity_mode = OSMO_SUART_PARITY_EVEN;
+       osmo_soft_uart_configure(suart, &cfg);
+       test_tx_exec(suart, "\xde\xad\xbe\xef", 4, (1 + 8 + 1 + 1));
+       test_tx_exec(suart, "\x00\xaa\x55\xff", 4, (1 + 8 + 1 + 1));
+
+       printf("======== %s(): testing 8-O-1\n", __func__);
+       cfg = suart_default_cfg;
+       cfg.parity_mode = OSMO_SUART_PARITY_ODD;
+       osmo_soft_uart_configure(suart, &cfg);
+       test_tx_exec(suart, "\xde\xad\xbe\xef", 4, (1 + 8 + 1 + 1));
+       test_tx_exec(suart, "\x00\xaa\x55\xff", 4, (1 + 8 + 1 + 1));
+
+       printf("======== %s(): testing 8-N-2\n", __func__);
+       cfg = suart_default_cfg;
+       cfg.num_stop_bits = 2;
+       osmo_soft_uart_configure(suart, &cfg);
+       test_tx_exec(suart, "\xde\xad\xbe\xef", 4, (1 + 8 + 2));
+       test_tx_exec(suart, "\x00\xaa\x55\xff", 4, (1 + 8 + 2));
+
+       printf("======== %s(): testing 6-N-1\n", __func__);
+       cfg = suart_default_cfg;
+       cfg.num_data_bits = 6;
+       osmo_soft_uart_configure(suart, &cfg);
+       test_tx_exec(suart, "\xde\xad\xbe\xef", 4, (1 + 6 + 1));
+       test_tx_exec(suart, "\x00\xaa\x55\xff", 4, (1 + 6 + 1));
+
+       osmo_soft_uart_free(suart);
+}
+
+int main(int argc, char **argv)
+{
+       test_rx();
+       test_tx_rx();
+
+       return 0;
+}
diff --git a/tests/soft_uart/soft_uart_test.ok 
b/tests/soft_uart/soft_uart_test.ok
new file mode 100644
index 0000000..b9eb0fd
--- /dev/null
+++ b/tests/soft_uart/soft_uart_test.ok
@@ -0,0 +1,51 @@
+======== test_rx(): testing 8-N-1
+suart_rx_cb(flags=00): 48
+suart_rx_cb(flags=00): 45
+suart_rx_cb(flags=00): 4c
+suart_rx_cb(flags=00): 4c
+suart_rx_cb(flags=00): 4f
+======== test_rx(): testing 8-N-2
+suart_rx_cb(flags=00): 48
+suart_rx_cb(flags=00): 45
+suart_rx_cb(flags=00): 4c
+suart_rx_cb(flags=00): 4c
+suart_rx_cb(flags=00): 4f
+======== test_rx(): testing 8-E-1
+suart_rx_cb(flags=02): 00
+suart_rx_cb(flags=02): 01
+suart_rx_cb(flags=02): ff
+suart_rx_cb(flags=00): 00
+suart_rx_cb(flags=00): ff
+suart_rx_cb(flags=00): aa
+suart_rx_cb(flags=00): 55
+suart_rx_cb(flags=00): 80
+suart_rx_cb(flags=00): e0
+suart_rx_cb(flags=00): f8
+suart_rx_cb(flags=00): fe
+======== test_rx(): testing 8-O-1
+suart_rx_cb(flags=02): 00
+suart_rx_cb(flags=02): 01
+suart_rx_cb(flags=02): ff
+suart_rx_cb(flags=00): 00
+suart_rx_cb(flags=00): ff
+suart_rx_cb(flags=00): aa
+suart_rx_cb(flags=00): 55
+suart_rx_cb(flags=00): 80
+suart_rx_cb(flags=00): e0
+suart_rx_cb(flags=00): f8
+suart_rx_cb(flags=00): fe
+======== test_tx_rx(): testing 8-N-1
+test_tx_exec(): 0011110111 0101101011 0011111011 0111101111
+test_tx_exec(): 0000000001 0010101011 0101010101 0111111111
+======== test_tx_rx(): testing 8-E-1
+test_tx_exec(): 00111101101 01011010111 00111110101 01111011111
+test_tx_exec(): 00000000001 00101010101 01010101001 01111111101
+======== test_tx_rx(): testing 8-O-1
+test_tx_exec(): 00111101111 01011010101 00111110111 01111011101
+test_tx_exec(): 00000000011 00101010111 01010101011 01111111111
+======== test_tx_rx(): testing 8-N-2
+test_tx_exec(): 00111101111 01011010111 00111110111 01111011111
+test_tx_exec(): 00000000011 00101010111 01010101011 01111111111
+======== test_tx_rx(): testing 6-N-1
+test_tx_exec(): 00111101 01011011 00111111 01111011
+test_tx_exec(): 00000001 00101011 01010101 01111111
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 73c3cdc..aacdad7 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -513,3 +513,9 @@
 cat $abs_srcdir/osmo_io/osmo_io_test.err > experr
 AT_CHECK([$abs_top_builddir/tests/osmo_io/osmo_io_test], [0], [expout], 
[experr])
 AT_CLEANUP
+
+AT_SETUP([soft_uart])
+AT_KEYWORDS([soft_uart])
+cat $abs_srcdir/soft_uart/soft_uart_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/soft_uart/soft_uart_test], [0], [expout], 
[ignore])
+AT_CLEANUP

--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/35046?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Icdfa0c644548964d37940c32dc9dcfcfc53c3a19
Gerrit-Change-Number: 35046
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <vyanits...@sysmocom.de>
Gerrit-MessageType: newchange

Reply via email to