readline test
should work with READLINE and EPICS command line libraries
This commit is contained in:
committed by
Andrew Johnson
parent
87b8050437
commit
a2be1c4f4f
@@ -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
|
||||
|
||||
18
modules/libcom/test/multiline-expect.txt
Normal file
18
modules/libcom/test/multiline-expect.txt
Normal file
@@ -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
|
||||
|
||||
|
||||
33
modules/libcom/test/multiline-input.txt
Normal file
33
modules/libcom/test/multiline-input.txt
Normal file
@@ -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
|
||||
93
modules/libcom/test/readlineTest.c
Normal file
93
modules/libcom/test/readlineTest.c
Normal file
@@ -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 <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <osiFileName.h>
|
||||
#include <epicsUnitTest.h>
|
||||
#include <testMain.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
Reference in New Issue
Block a user