* add more NextToken list-tests
authorWilfried Göesgens <willi@citadel.org>
Mon, 7 Sep 2009 19:10:45 +0000 (19:10 +0000)
committerWilfried Göesgens <willi@citadel.org>
Mon, 7 Sep 2009 19:10:45 +0000 (19:10 +0000)
* make pStart ~NULL if we finished evaluating the buffer; This way we can clearly differentiate between "not started" and "finished".

libcitadel/lib/stringbuf.c
libcitadel/tests/stringbuf_test.c

index 9a6ef47859fda87fabc4f6931dc0a2b5768c6bef..5d6d4125b0922250c64214b4c2393791b03ae80c 100644 (file)
@@ -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 */
index 76385ab8a2d49034deeabbe984efbaafa30d9b19..4552e9ae9b29184adb630ba711c8a1b668e7ccd9 100644 (file)
@@ -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);
 
 
 /*