From a2be1c4f4fdf4beb226d9d8bc8bb993de14deb43 Mon Sep 17 00:00:00 2001 From: Hinko Kocevar Date: Fri, 21 Feb 2025 17:09:53 +0100 Subject: [PATCH] readline test should work with READLINE and EPICS command line libraries --- modules/libcom/test/Makefile | 12 +++ modules/libcom/test/multiline-expect.txt | 18 +++++ modules/libcom/test/multiline-input.txt | 33 +++++++++ modules/libcom/test/readlineTest.c | 93 ++++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 modules/libcom/test/multiline-expect.txt create mode 100644 modules/libcom/test/multiline-input.txt create mode 100644 modules/libcom/test/readlineTest.c diff --git a/modules/libcom/test/Makefile b/modules/libcom/test/Makefile index 59114f13a..718f62dfc 100644 --- a/modules/libcom/test/Makefile +++ b/modules/libcom/test/Makefile @@ -344,6 +344,18 @@ TESTS += nonEpicsThreadPriorityTest endif endif +# HK: improve detection of valid command line libraries +ifeq ($(COMMANDLINE_LIBRARY),READLINE) +TESTPROD_HOST += readlineTest +readlineTest_SRCS += readlineTest.c +TESTS += readlineTest +endif +ifeq ($(COMMANDLINE_LIBRARY),EPICS) +TESTPROD_HOST += readlineTest +readlineTest_SRCS += readlineTest.c +TESTS += readlineTest +endif + include $(TOP)/configure/RULES rtemsTestData.c : $(TESTFILES) $(TOOLS)/epicsMakeMemFs.pl diff --git a/modules/libcom/test/multiline-expect.txt b/modules/libcom/test/multiline-expect.txt new file mode 100644 index 000000000..3500e481d --- /dev/null +++ b/modules/libcom/test/multiline-expect.txt @@ -0,0 +1,18 @@ +1 not a multiline string + +2 first multiline string + +3 second multiline string with more lines + +4 several lines .. next line is empty: next has only a space: next line has 3 spaces: END + +5 it is fine to spit words, or really chop them up! + + + + + +# keep this one last +99 will not get to this line 'cause input file line has no newline + + diff --git a/modules/libcom/test/multiline-input.txt b/modules/libcom/test/multiline-input.txt new file mode 100644 index 000000000..182db6cd9 --- /dev/null +++ b/modules/libcom/test/multiline-input.txt @@ -0,0 +1,33 @@ +# this is a comment; comments are ignored and so are empty lines +1 not a multiline string + +2 first multiline \ +string + +3 second multiline \ +string \ +with more lines + +4 several lines .. \ +next line is empty: \ +\ +next has only a space:\ + \ +next line has 3 spaces:\ + \ +END + +5 it is fine to sp\ +it words, or really \ +c\ +h\ +o\ +p\ + them up! + + + + + +# keep this one last +99 this one will not appear 'cause of no newline \ No newline at end of file diff --git a/modules/libcom/test/readlineTest.c b/modules/libcom/test/readlineTest.c new file mode 100644 index 000000000..71d4cbe25 --- /dev/null +++ b/modules/libcom/test/readlineTest.c @@ -0,0 +1,93 @@ +/*************************************************************************\ +* Copyright (c) 2025 Hinko Kocevar +* SPDX-License-Identifier: EPICS +* EPICS BASE is distributed subject to a Software License Agreement found +* in file LICENSE that is included with this distribution. +\*************************************************************************/ + +#include +#include +#include + +#include +#include +#include + +#include "epicsReadline.h" + +MAIN(readlineTest) +{ + const char *input = ".." OSI_PATH_SEPARATOR "multiline-input.txt"; + const char *expect = ".." OSI_PATH_SEPARATOR "multiline-expect.txt"; + + testPlan(5); + + testDiag("open input file \"%s\"", input); + FILE *fp_input = fopen(input, "r"); + if (fp_input == NULL) { + testAbort("unable to open \"%s\"", input); + return -1; + } + + testDiag("open expect file \"%s\"", expect); + FILE *fp_expect = fopen(expect, "r"); + if (fp_expect == NULL) { + testAbort("unable to open \"%s\"", expect); + return -1; + } + + void *context = epicsReadlineBegin(fp_input); + + char *line_input = NULL; + char c = 0; + int icin = 0; + char line_expect[500]; + + while (1) { + // get string from input (can be multi-line delimited by \) + if ((line_input = epicsReadline(NULL, context)) == NULL) { + // EOF reached or some other low level error + break; + } + icin = 0; + while ((c = line_input[icin]) && isspace(c)) { + icin++; + } + // ignore empty lines and comments + if (!c || c == '#') { + continue; + } + + do { + // expected strings are always single line strings + fgets(line_expect, 499, fp_expect); + icin = 0; + while ((c = line_expect[icin]) && isspace(c)) { + icin++; + } + // ignore empty lines and comments + } while (!c || c == '#'); + + // fgets() stores \n if present; get rid of it + size_t len_expect = strlen(line_expect); + if (line_expect[len_expect-1] == '\n') { + line_expect[len_expect-1] = '\0'; + len_expect -= 1; + } + size_t len_input = strlen(line_input); + + if (len_input != len_expect) { + testAbort("lines are not of same length: input %ld, expected %ld", len_input, len_expect); + } + if (strncmp(line_input, line_expect, len_expect)) { + testAbort("lines are not the same:\ninput:\"%s\"\nexpected: \"%s\"", line_input, line_expect); + } + testOk(1, "line \"%s\"", line_input); + } + + epicsReadlineEnd(context); + fclose(fp_input); + fclose(fp_expect); + + return testDone(); +}