diff --git a/modules/libcom/test/epicsStringTest.c b/modules/libcom/test/epicsStringTest.c index a1c2874fb..b5f744b37 100644 --- a/modules/libcom/test/epicsStringTest.c +++ b/modules/libcom/test/epicsStringTest.c @@ -195,6 +195,42 @@ void testStrTok(void) testTok(NULL, " \t", "bbb ", "bbb", ""); } +static +void testEpicsStrPrintEscaped(void) +{ + const char *filename = "testEpicsStrPrintEscaped"; + // Avoid printing to stdout by redirecting everything to a file + FILE *testFile = fopen(filename, "a"); + FILE *readOnly = fopen(filename, "r"); + + testDiag("testEpicsStrPrintEscaped()"); + + // Passing cases + testOk1(epicsStrPrintEscaped(testFile, "1234", 4) == 4); + testOk1(epicsStrPrintEscaped(testFile, "\a\b\f\n\r\t\v\\\'\"", 10) == 20); + + // Failing cases + testOk1(epicsStrPrintEscaped(NULL, "1234", 4) == -1); + // On some platforms (specifially certain versions of MinGW-w64), fprintf + // is broken and does not return -1 when the write operation fails. On + // those platforms, epicsStrPrintEscaped cannot detect failure either, so + // testing that it reports failure does not make sense on those platforms. + // For this reason, we only test this behavior of epcisStrPrintEscaped when + // after checking that fprintf behaves correctly. + if (fprintf(readOnly, "test") == -1) { + testOk1(epicsStrPrintEscaped(readOnly, "1234", 4) == -1); + } else { + testSkip(1, "fprintf is broken on this system"); + } + testOk1(epicsStrPrintEscaped(testFile, NULL, 4) == 0); + testOk1(epicsStrPrintEscaped(testFile, "", 4) == 0); + testOk1(epicsStrPrintEscaped(testFile, "1234", 0) == 0); + + fclose(testFile); + fclose(readOnly); + remove(filename); +} + MAIN(epicsStringTest) { const char * const empty = ""; @@ -209,7 +245,7 @@ MAIN(epicsStringTest) char *s; int status; - testPlan(439); + testPlan(446); testChars(); @@ -407,6 +443,7 @@ MAIN(epicsStringTest) testDistance(); testStrTok(); + testEpicsStrPrintEscaped(); return testDone(); }