From 6feaaebd755efa4504b6a1be13f684b5391b2abf Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sat, 20 Aug 2022 10:53:57 -0700 Subject: [PATCH] test epicsStrtok_r --- modules/libcom/test/epicsStringTest.c | 58 ++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/modules/libcom/test/epicsStringTest.c b/modules/libcom/test/epicsStringTest.c index 7abffa3da..8cd67ee81 100644 --- a/modules/libcom/test/epicsStringTest.c +++ b/modules/libcom/test/epicsStringTest.c @@ -141,6 +141,61 @@ void testDistance(void) { #undef TEST } +static +void testTok(const char *inp, + const char *delim, + const char *save, + const char *expectTok, + const char *expectSave) +{ + char *scratch = !inp ? NULL : strdup(inp); + char *allocSave = !save ? NULL : strdup(save); + char *actualSave = allocSave; + char *actualTok; + int ok; + + if((!inp || scratch) && (!save || allocSave)) { + + actualTok = epicsStrtok_r(scratch, delim, &actualSave); + +#define COMPSTR(A, B) ((!(A) && !(B)) || ((A) && (B) && strcmp(A,B)==0)) + +#define ORNIL(S) (S ? S : "(nil)") + + testOk(COMPSTR(expectSave, actualSave) && COMPSTR(expectTok, actualTok), + "inp: \"%s\" delim: \"%s\" tok: \"%s\"==\"%s\" save: \"%s\"==\"%s\"", + ORNIL(inp), ORNIL(delim), ORNIL(expectTok), ORNIL(actualTok), ORNIL(expectSave), ORNIL(actualSave)); +#undef COMPSTR +#undef ORNIL + + } else { + testFail("nomem"); + } + free(scratch); + free(allocSave); +} + +static +void testStrTok(void) +{ + testDiag("testStrTok()"); + + testTok("", " \t", NULL, NULL, NULL); + testTok(" \t", " \t", NULL, NULL, NULL); + testTok(NULL, " \t", "", NULL, NULL); + testTok(NULL, " \t", " ", NULL, NULL); + testTok(NULL, " \t", " \t", NULL, NULL); + + testTok("a", " \t", NULL, "a", NULL); + testTok(" a", " \t", NULL, "a", NULL); + testTok("a ", " \t", NULL, "a", " "); + testTok("\ta ", " \t", NULL, "a", " "); + + testTok("aaa bbb ", " \t", NULL, "aaa", "bbb "); + testTok("aaa\tbbb ", " \t", NULL, "aaa", "bbb "); + testTok(NULL, " \t", "bbb ", "bbb", ""); +} + MAIN(epicsStringTest) { const char * const empty = ""; @@ -155,7 +210,7 @@ MAIN(epicsStringTest) char *s; int status; - testPlan(427); + testPlan(439); testChars(); @@ -352,6 +407,7 @@ MAIN(epicsStringTest) testOk(result[status] == 0, " 0-terminated"); testDistance(); + testStrTok(); return testDone(); }