From e7530d3a0bdfa13663fedde0673aede563e5ef19 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Mon, 7 Sep 2009 19:10:45 +0000 Subject: [PATCH] * add more NextToken list-tests * make pStart ~NULL if we finished evaluating the buffer; This way we can clearly differentiate between "not started" and "finished". --- libcitadel/lib/stringbuf.c | 8 +++-- libcitadel/tests/stringbuf_test.c | 53 +++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 9a6ef4785..5d6d4125b 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -1316,7 +1316,9 @@ int StrBufHaveNextToken(const StrBuf *Source, const char **pStart) { const char *Null = NULL; Null --; - if ((Source == NULL) || (*pStart == Null)) + if ((Source == NULL) || + (*pStart == Null) || + (Source->BufUsed == 0)) { return 0; } @@ -1419,7 +1421,9 @@ int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pSt /* did we reach the end? */ if ((s > EndBuffer)) { - *pStart = EndBuffer + 1; + EndBuffer = NULL; + EndBuffer --; + *pStart = EndBuffer; } else { *pStart = s; /* remember the position for the next run */ diff --git a/libcitadel/tests/stringbuf_test.c b/libcitadel/tests/stringbuf_test.c index 76385ab8a..4552e9ae9 100644 --- a/libcitadel/tests/stringbuf_test.c +++ b/libcitadel/tests/stringbuf_test.c @@ -119,10 +119,14 @@ static void TestCreateBuf(void) static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens) { const char *pCh = NULL; + char *NotNull; StrBuf *Buf2; long CountTokens = 0; - long HaveNextToken; - long HaveNextTokenF; + long HaveNextToken = 0; + long HaveNextTokenF = 0; + + NotNull = NULL; + NotNull --; printf("\n\nTemplate: >%s<\n", ChrPtr(Buf)); @@ -133,7 +137,11 @@ static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens) { CountTokens++; - printf("Token: >%s< >%s< %ld:%ld\n", ChrPtr(Buf2), pCh, HaveNextToken, HaveNextTokenF); + printf("Token: >%s< >%s< %ld:%ld\n", + ChrPtr(Buf2), + ((pCh != NULL) && (pCh != NotNull))? pCh : "N/A", + HaveNextToken, + HaveNextTokenF); CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0)); CU_ASSERT(CountTokens <= NTokens); @@ -141,7 +149,7 @@ static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens) CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0)); } -static void TestNextTokenizer1(void) +static void TestNextTokenizer_EndWithEmpty(void) { StrBuf *Buf; @@ -150,7 +158,7 @@ static void TestNextTokenizer1(void) FreeStrBuf(&Buf); } -static void TestNextTokenizer2(void) +static void TestNextTokenizer_StartWithEmpty(void) { StrBuf *Buf; @@ -159,6 +167,33 @@ static void TestNextTokenizer2(void) FreeStrBuf(&Buf); } +static void TestNextTokenizer_Empty(void) +{ + StrBuf *Buf; + + Buf = NewStrBufPlain(HKEY("")); + NextTokenizerIterateBuf(Buf, 8); + FreeStrBuf(&Buf); +} + +static void TestNextTokenizer_TwoEmpty(void) +{ + StrBuf *Buf; + + Buf = NewStrBufPlain(HKEY(",")); + NextTokenizerIterateBuf(Buf, 8); + FreeStrBuf(&Buf); +} + +static void TestNextTokenizer_One(void) +{ + StrBuf *Buf; + + Buf = NewStrBufPlain(HKEY("one")); + NextTokenizerIterateBuf(Buf, 8); + FreeStrBuf(&Buf); +} + static void testSuccessAssertTrue(void) @@ -290,8 +325,12 @@ static void AddStrBufSimlpeTests(void) pTest = CU_add_test(pGroup, "testCreateBuf", TestCreateBuf); pGroup = CU_add_suite("TestStringTokenizer", NULL, NULL); - pTest = CU_add_test(pGroup, "testNextTokenizer_1", TestNextTokenizer1); - pTest = CU_add_test(pGroup, "testNextTokenizer_2", TestNextTokenizer2); + pTest = CU_add_test(pGroup, "testNextTokenizer_EndWithEmpty", TestNextTokenizer_EndWithEmpty); + pTest = CU_add_test(pGroup, "testNextTokenizer_StartWithEmpty", TestNextTokenizer_StartWithEmpty); + pTest = CU_add_test(pGroup, "testNextTokenizer_StartWithEmpty", TestNextTokenizer_StartWithEmpty); + pTest = CU_add_test(pGroup, "testNextTokenizer_Empty", TestNextTokenizer_Empty); + pTest = CU_add_test(pGroup, "testNextTokenizer_TwoEmpty", TestNextTokenizer_TwoEmpty); + pTest = CU_add_test(pGroup, "testNextTokenizer_One", TestNextTokenizer_One); /* -- 2.39.2