removed StartLibCitadel()
[citadel.git] / libcitadel / tests / stringbuf_test.c
index d98c00e7cfc69b46fc2ca675e8243ba8e1e7af9a..2ca5c254fc717b85d6a595f3d2d83bd2b7750706 100644 (file)
@@ -1,9 +1,8 @@
-
 /*
  *  CUnit - A Unit testing framework library for C.
  *  Copyright (C) 2001  Anil Kumar
  *  
- *  This library is free software; you can redistribute it and/or
+ *  This library is open source software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
  *  License as published by the Free Software Foundation; either
  *  version 2 of the License, or (at your option) any later version.
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *  Library General Public License for more details.
- *
- *  You should have received a copy of the GNU Library General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 
 #include "stringbuf_test.h"
+#define SHOW_ME_VAPPEND_PRINTF
 #include "../lib/libcitadel.h"
 
+int Quiet = 0;
+/*
+ * Stolen from wc_printf; we need to test that other printf too... 
+ */
+static void TEST_StrBufAppendPrintf(StrBuf *WBuf, const char *format,...)
+{
+       va_list arg_ptr;
+
+       if (WBuf == NULL)
+               return;
+
+       va_start(arg_ptr, format);
+       StrBufVAppendPrintf(WBuf, format, arg_ptr);
+       va_end(arg_ptr);
+}
 
 static void TestRevalidateStrBuf(StrBuf *Buf)
 {
@@ -38,7 +50,6 @@ static void TestCreateBuf(void)
        StrBuf *Buf2;
        long len;
        long i;
-       char *ch;
 
        Buf = NewStrBuf();
        CU_ASSERT(Buf != NULL);
@@ -48,6 +59,11 @@ static void TestCreateBuf(void)
        CU_ASSERT(Buf != NULL);
        FreeStrBuf(&Buf);
 
+       /* make it alloc a bigger buffer... */
+       Buf = NewStrBufPlain(NULL, SIZ);
+       CU_ASSERT(Buf != NULL);
+       FreeStrBuf(&Buf);
+
 
        Buf = NewStrBufDup(NULL);
        CU_ASSERT(Buf != NULL);
@@ -93,6 +109,40 @@ static void TestCreateBuf(void)
        TestRevalidateStrBuf(Buf);
        StrBufShrinkToFit(Buf, 1);
        TestRevalidateStrBuf(Buf);
+       FreeStrBuf(&Buf);
+
+
+       Buf = NewStrBufPlain(HKEY("ABC"));
+       Buf2 = NewStrBufPlain(HKEY("------"));
+       TestRevalidateStrBuf(Buf);
+       len = StrLength(Buf);
+       for (i=0; i< 50; i ++)
+       {
+               StrBufPrintf(Buf, "%s", ChrPtr(Buf2));
+               CU_ASSERT(StrLength(Buf) == StrLength(Buf2));           
+
+               StrBufAppendBufPlain(Buf2, HKEY("ABCDEFG"), 0);
+       }
+       TestRevalidateStrBuf(Buf);
+       StrBufShrinkToFit(Buf, 1);
+       TestRevalidateStrBuf(Buf);
+       FreeStrBuf(&Buf);
+       FreeStrBuf(&Buf2);      
+
+
+       Buf = NewStrBufPlain(HKEY("ABC"));
+       TestRevalidateStrBuf(Buf);
+       len = StrLength(Buf);
+       for (i=0; i< 500; i ++)
+       {
+               TEST_StrBufAppendPrintf(Buf, "%s", "ABC");
+               len += 3;
+               CU_ASSERT(StrLength(Buf) == len);               
+       }
+       TestRevalidateStrBuf(Buf);
+       StrBufShrinkToFit(Buf, 1);
+       TestRevalidateStrBuf(Buf);
+
 
        Buf2 = NewStrBufDup(Buf);
        CU_ASSERT(StrLength(Buf) == StrLength(Buf2));           
@@ -107,17 +157,33 @@ static void TestCreateBuf(void)
        FLUSHStrBuf(Buf);
        CU_ASSERT(StrLength(Buf) == 0);
 
-       FreeStrBuf(&Buf);
+       HFreeStrBuf(NULL);
        HFreeStrBuf(Buf2);
-       CU_ASSERT(Buf == NULL);
        CU_ASSERT(Buf2 != NULL);
 
+       FreeStrBuf(&Buf);
+       CU_ASSERT(Buf == NULL);
+       
+}
+
+
+
+
+static void TestBufNumbers(void)
+{
+       StrBuf *Buf;
+       StrBuf *Buf2;
+       StrBuf *Buf3;
+       char *ch;
+       int i;
 
        Buf2 = NewStrBuf();
+       Buf3 = NewStrBufPlain(HKEY("abcd"));
        Buf = NewStrBufPlain(HKEY("123456"));
-       CU_ASSERT(StrBufIsNumber(Buf) == 1); ///Todo: this is buggy.
+       CU_ASSERT(StrBufIsNumber(Buf) == 1);
        CU_ASSERT(StrBufIsNumber(NULL) == 0);
        CU_ASSERT(StrBufIsNumber(Buf2) == 0);
+       CU_ASSERT(StrBufIsNumber(Buf3) == 0);
 
        CU_ASSERT(StrTol(Buf) == 123456);
        CU_ASSERT(StrTol(NULL) == 0);
@@ -133,19 +199,151 @@ static void TestCreateBuf(void)
        CU_ASSERT(strlen(ch) == i);
        free(ch);
        FreeStrBuf(&Buf2);
+       FreeStrBuf(&Buf3);
+}
+
+static void TestStrBufPeek(void)
+{
+       StrBuf *Buf;
+       const char *pch;
+
+       Buf = NewStrBufPlain(HKEY("0123456"));
+       pch = ChrPtr(Buf);
+
+       CU_ASSERT(StrBufPeek(NULL, pch + 4, -1, 'A') == -1);
+
+       CU_ASSERT(StrBufPeek(Buf, pch + 4, -1, 'A') == 4);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "0123A56");
+
+       CU_ASSERT(StrBufPeek(Buf, pch - 1, -1, 'A') == -1);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "0123A56");
+
+       CU_ASSERT(StrBufPeek(Buf, pch + 10, -1, 'A') == -1);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "0123A56");
+
+       CU_ASSERT(StrBufPeek(Buf, NULL, -1, 'A') == -1);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "0123A56");
+
+       CU_ASSERT(StrBufPeek(Buf, NULL, 10, 'A') == -1);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "0123A56");
+
+       CU_ASSERT(StrBufPeek(Buf, NULL, 5, 'A') == 5);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "0123AA6");
+       FreeStrBuf(&Buf);
+}
+
+static void TestBufStringManipulation(void)
+{
+       long len, i = 0;
+       StrBuf *dest = NewStrBuf ();
+       StrBuf *Buf = NewStrBufPlain(HKEY("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"));
+
+       StrBufSub(dest, Buf, -5, i);
+       len = StrLength(Buf);
+       for (i = 0; i < len + 10; i++)
+       {
+               StrBufSub(dest, Buf, 5, i);
+               if (i + 5 < len)
+               {
+                       CU_ASSERT(StrLength(dest) == i);
+               }
+               else
+               {
+                       CU_ASSERT(StrLength(dest) == len - 5);
+               }
+       }
+       FreeStrBuf(&dest);
+       dest = NewStrBuf ();
+       StrBufSub(dest, Buf, -5, 200);
+
+       StrBufCutLeft(Buf, 5);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
+       CU_ASSERT(StrLength(Buf) == 95);
+
+       StrBufCutRight(Buf, 5);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345");
+       CU_ASSERT(StrLength(Buf) == 90);
+
+       StrBufCutAt(Buf, 80, NULL);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"67890123456789012345678901234567890123456789012345678901234567890123456789012345");
+       CU_ASSERT(StrLength(Buf) == 80);
+
+       StrBufCutAt(Buf, -1, ChrPtr(Buf) + 70);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"6789012345678901234567890123456789012345678901234567890123456789012345");
+       CU_ASSERT(StrLength(Buf) == 70);
+
+
+       StrBufCutAt(Buf, 0, ChrPtr(Buf) + 60);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"678901234567890123456789012345678901234567890123456789012345");
+       CU_ASSERT(StrLength(Buf) == 60);
+
+       StrBufCutAt(Buf, 0, ChrPtr(Buf) + 70);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"678901234567890123456789012345678901234567890123456789012345");
+       CU_ASSERT(StrLength(Buf) == 60);
+
+       StrBufCutAt(Buf, 70, NULL);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"678901234567890123456789012345678901234567890123456789012345");
+       CU_ASSERT(StrLength(Buf) == 60);
+
+
+       StrBufCutLeft(Buf, 70);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"");
+       CU_ASSERT(StrLength(Buf) == 0);
+
+       StrBufPlain(Buf, HKEY("678901234567890123456789012345678901234567890123456789012345"));
+       StrBufCutRight(Buf, 70);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"");
+       CU_ASSERT(StrLength(Buf) == 0);
+
+       FreeStrBuf(&dest);
+       FreeStrBuf(&Buf);
+
+       Buf = NewStrBufPlain(HKEY(" \tabc\t "));
+       StrBufTrim(Buf);
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf),"abc");
+       CU_ASSERT(StrLength(Buf) == 3);
+
+       StrBufUpCase(NULL);
+       FlushStrBuf(Buf);
+       StrBufUpCase(Buf);
+       StrBufPlain(Buf, HKEY("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
+       StrBufUpCase(Buf);
+
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
+
+
+       StrBufLowerCase(NULL);
+       FlushStrBuf(Buf);
+       StrBufLowerCase(Buf);
+       StrBufPlain(Buf, HKEY("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
+       StrBufLowerCase(Buf);
+
+       CU_ASSERT_STRING_EQUAL(ChrPtr(Buf), "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789");
+
+
+       FreeStrBuf(&Buf);
+
 }
 
 static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens)
 {
+       long FoundTokens;
        const char *pCh = NULL;
        StrBuf *Buf2;
        long CountTokens = 0;
        long HaveNextToken = 0;
        long HaveNextTokenF = 0;
 
-       printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
        TestRevalidateStrBuf(Buf);
-                            
+       FoundTokens = StrBufNum_tokens(Buf, ',');
+       if (!Quiet) 
+               printf("\n\nTemplate: >%s< %d, %ld\n", 
+                      ChrPtr(Buf), 
+                      NTokens, 
+                      FoundTokens);
+
+       CU_ASSERT(FoundTokens == NTokens);
+
        Buf2 = NewStrBuf();
        while (HaveNextToken = StrBufHaveNextToken(Buf, &pCh),
               HaveNextTokenF = StrBufExtract_NextToken(Buf2, Buf, &pCh, ','),
@@ -153,11 +351,11 @@ static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens)
        {
                CountTokens++;
                
-               printf("Token: >%s< >%s< %ld:%ld\n", 
-                      ChrPtr(Buf2), 
-                      ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A", 
-                      HaveNextToken, 
-                      HaveNextTokenF);
+               if (!Quiet) printf("Token: >%s< >%s< %ld:%ld\n", 
+                                  ChrPtr(Buf2), 
+                                  ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A", 
+                                  HaveNextToken, 
+                                  HaveNextTokenF);
                TestRevalidateStrBuf(Buf2);
 
                CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0));
@@ -175,7 +373,7 @@ static void TestNextTokenizer_EndWithEmpty(void)
        StrBuf *Buf;
 
        Buf = NewStrBufPlain(HKEY("abc,abc, 1, ,,"));
-       NextTokenizerIterateBuf(Buf, 7);
+       NextTokenizerIterateBuf(Buf, 6);
        FreeStrBuf(&Buf);
 }
 
@@ -184,7 +382,7 @@ static void TestNextTokenizer_StartWithEmpty(void)
        StrBuf *Buf;
 
        Buf = NewStrBufPlain(HKEY(",cde,abc, 1, ,,bbb"));
-       NextTokenizerIterateBuf(Buf, 8);
+       NextTokenizerIterateBuf(Buf, 7);
        FreeStrBuf(&Buf);
 }
 
@@ -193,7 +391,7 @@ static void TestNextTokenizer_Empty(void)
        StrBuf *Buf;
 
        Buf = NewStrBufPlain(HKEY(""));
-       NextTokenizerIterateBuf(Buf, 8);
+       NextTokenizerIterateBuf(Buf, 0);
        FreeStrBuf(&Buf);
 }
 
@@ -202,7 +400,7 @@ static void TestNextTokenizer_TwoEmpty(void)
        StrBuf *Buf;
 
        Buf = NewStrBufPlain(HKEY(","));
-       NextTokenizerIterateBuf(Buf, 8);
+       NextTokenizerIterateBuf(Buf, 2);
        FreeStrBuf(&Buf);
 }
 
@@ -211,7 +409,7 @@ static void TestNextTokenizer_One(void)
        StrBuf *Buf;
 
        Buf = NewStrBufPlain(HKEY("one"));
-       NextTokenizerIterateBuf(Buf, 8);
+       NextTokenizerIterateBuf(Buf, 1);
        FreeStrBuf(&Buf);
 }
 
@@ -239,7 +437,7 @@ static void NextLineterateBuf(StrBuf *Buf, int NLines)
        OneLine = NewStrBuf();
        ConcatenatedLines = NewStrBuf();
 
-       printf("\n");
+       if (!Quiet) printf("\n");
 
        if (StrLength(Buf) > 0) 
                do 
@@ -248,9 +446,9 @@ static void NextLineterateBuf(StrBuf *Buf, int NLines)
                        
                        CountTokens++;
                        
-                       printf("Line: >%s< >%s<\n", 
-                              ChrPtr(OneLine), 
-                              ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A");
+                       if (!Quiet) printf("Line: >%s< >%s<\n", 
+                                          ChrPtr(OneLine), 
+                                          ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A");
                        TestRevalidateStrBuf(OneLine);
                        CU_ASSERT(CountTokens <= NLines);
                        StrBufAppendBuf(ConcatenatedLines, OneLine, 0);
@@ -267,8 +465,8 @@ static void NextLineterateBuf(StrBuf *Buf, int NLines)
                       (pCh != NULL));
        
 
-       printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
-       printf("\n\nAfter: >%s<\n", ChrPtr(ConcatenatedLines));
+       if (!Quiet) printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
+       if (!Quiet) printf("\n\nAfter: >%s<\n", ChrPtr(ConcatenatedLines));
        CU_ASSERT_NSTRING_EQUAL(ChrPtr(ConcatenatedLines), 
                                ChrPtr(Buf), 
                                StrLength(Buf));
@@ -344,7 +542,7 @@ static void TestStrBufUrlescAppend(void)
        StrBuf *Out = NewStrBuf();
 
        StrBufUrlescAppend (Out, In, NULL);
-       printf ("%s<\n%s<\n%s\n", ChrPtr(In), ChrPtr(Out), expect);
+       if (!Quiet) printf ("%s<\n%s<\n%s\n", ChrPtr(In), ChrPtr(Out), expect);
        CU_ASSERT_STRING_EQUAL(ChrPtr(Out), expect);
        FreeStrBuf(&In);
        FreeStrBuf(&Out);
@@ -384,13 +582,17 @@ Some samples from the original...
 
 
 
-static void AddStrBufSimlpeTests(void)
+static void AddStrBufSimpleTests(void)
 {
        CU_pSuite pGroup = NULL;
        CU_pTest pTest = NULL;
 
        pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
        pTest = CU_add_test(pGroup, "testCreateBuf", TestCreateBuf);
+       pTest = CU_add_test(pGroup, "TestBufNumbers", TestBufNumbers);
+       pTest = CU_add_test(pGroup, "TestStrBufPeek", TestStrBufPeek);
+       pTest = CU_add_test(pGroup, "TestBufStringManipulation", TestBufStringManipulation);
+
 
        pGroup = CU_add_suite("TestStringTokenizer", NULL, NULL);
        pTest = CU_add_test(pGroup, "testNextTokenizer_EndWithEmpty", TestNextTokenizer_EndWithEmpty);
@@ -419,27 +621,30 @@ static void AddStrBufSimlpeTests(void)
 
 int main(int argc, char* argv[])
 {
+       ///int i;
        setvbuf(stdout, NULL, _IONBF, 0);
 
-       StartLibCitadel(8);
        CU_BOOL Run = CU_FALSE ;
-       
+
+       if (argc > 0)
+               Quiet = 1; // todo: -q ;-)
+//     for (i=0; i< 100000; i++) {
        CU_set_output_filename("TestAutomated");
        if (CU_initialize_registry()) {
                printf("\nInitialize of test Registry failed.");
-       }
+//     }
        
        Run = CU_TRUE ;
-       AddStrBufSimlpeTests();
+       AddStrBufSimpleTests();
        
        if (CU_TRUE == Run) {
                //CU_console_run_tests();
-    printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
+               printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
     
     ///CU_automated_run_tests();
        }
        
        CU_cleanup_registry();
-
+       }
        return 0;
 }