Back to index

libcitadel  8.12
hashlist_test.c
Go to the documentation of this file.
00001 /*
00002  *  CUnit - A Unit testing framework library for C.
00003  *  Copyright (C) 2001  Anil Kumar
00004  *  
00005  *  This library is open source software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  */
00015 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 
00020 #include "stringbuf_test.h"
00021 #include "../lib/libcitadel.h"
00022 
00023 
00024 static HashList *GetFilledHash(int n, int stepwidth)
00025 {
00026        HashList* TestHash;
00027        int i;
00028        int *val;
00029 
00030        TestHash = NewHash(1, Flathash);
00031 
00032        for (i = 0; i < n;  i+=stepwidth)
00033        {
00034               val = (int*) malloc(sizeof(int));
00035               *val = i;
00036               Put(TestHash, IKEY(i), val, NULL);
00037        }
00038        return TestHash;
00039 }
00040 
00041 
00042 
00043 static void test_iterate_hash(HashList *testh, int forward, int stepwidth)
00044 {
00045        int i = 0;
00046        HashPos  *it;
00047        void *vTest;
00048        long len = 0;
00049        const char *Key;
00050        int dir = 1;
00051 
00052        if (forward == 0)
00053               dir = -1;
00054        it = GetNewHashPos(testh, dir * stepwidth);
00055        while (GetNextHashPos(testh, it, &len, &Key, &vTest) &&
00056                (vTest != NULL)) {
00057 
00058               printf("i: %d c: %d\n", i, *(int*) vTest);
00059               i+=stepwidth;
00060        }
00061        DeleteHashPos(&it);
00062 }
00063 
00064 static void ValidateBackAndForth(HashList *H)
00065 {
00066        test_iterate_hash(H, 1, 1);
00067        printf("\n");
00068 
00069        test_iterate_hash(H, 0, 1);
00070        printf("\n");
00071 
00072        test_iterate_hash(H, 1, 2);
00073        printf("\n");
00074 
00075        test_iterate_hash(H, 0, 2);
00076        printf("\n");
00077 
00078        test_iterate_hash(H, 1, 3);
00079        printf("\n");
00080 
00081        test_iterate_hash(H, 0, 3);
00082        printf("\n");
00083 }
00084 
00085 static void TestHashlistIteratorForward (void)
00086 {
00087        HashList *H;
00088 
00089        H = GetFilledHash (10, 1);
00090 
00091        ValidateBackAndForth(H);
00092 
00093        DeleteHash(&H);
00094 }
00095 
00096 
00097 static void TestHashlistAddDelete (void)
00098 {
00099        HashList *H;
00100        HashPos *at;
00101        int *val, i;
00102 
00103        H = GetFilledHash (10, 1);
00104 
00105        at = GetNewHashPos(H, 0);
00106 
00107        printf("Remove first\n");
00108        DeleteEntryFromHash(H, at);
00109        DeleteHashPos(&at);
00110 
00111        ValidateBackAndForth(H);
00112 
00113        printf("Insert 15\n");
00114        i = 15;
00115        val = (int*) malloc(sizeof(int));
00116        *val = i;
00117        Put(H, IKEY(i), val, NULL);
00118 
00119        ValidateBackAndForth(H);
00120 
00121        printf("Remove third\n");
00122        at = GetNewHashPos(H, 0);
00123        NextHashPos(H, at);
00124        NextHashPos(H, at);
00125        NextHashPos(H, at);
00126        DeleteEntryFromHash(H, at);
00127 
00128        ValidateBackAndForth(H);
00129        printf("Insert -15\n");
00130 
00131        i = -15;
00132        val = (int*) malloc(sizeof(int));
00133        *val = i;
00134        Put(H, IKEY(i), val, NULL);
00135 
00136        ValidateBackAndForth(H);
00137 
00138        DeleteHashPos(&at);
00139        DeleteHash(&H);
00140 }
00141 
00142 /*
00143 Some samples from the original...
00144        CU_ASSERT_EQUAL(10, 10);
00145        CU_ASSERT_EQUAL(0, -0);
00146        CU_ASSERT_EQUAL(-12, -12);
00147        CU_ASSERT_NOT_EQUAL(10, 11);
00148        CU_ASSERT_NOT_EQUAL(0, -1);
00149        CU_ASSERT_NOT_EQUAL(-12, -11);
00150        CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
00151        CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
00152        CU_ASSERT_PTR_NULL(NULL);
00153        CU_ASSERT_PTR_NULL(0x0);
00154        CU_ASSERT_PTR_NOT_NULL((void*)0x23);
00155        CU_ASSERT_STRING_EQUAL(str1, str2);
00156        CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
00157        CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
00158        CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
00159        CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
00160        CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
00161        CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
00162        CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
00163        CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
00164        CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
00165        CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
00166        CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
00167        CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
00168        CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
00169        CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
00170 */
00171 
00172 
00173 
00174 const char *MSetStrings[] = {
00175        "11:63",
00176        "65",
00177        "1,65,77",
00178        "1:65,77,80:*",
00179        NULL
00180 };
00181 
00182 #define InMSet 0
00183 #define NotInMSet 1
00184 const long MessageNumbers[5][2][10] = {
00185 /* First MSet */
00186        {
00187               {11, 59, 63, 0, 0, 0, 0, 0, 0, 0}, /* In */
00188               {2, 10, 64, 65, 0, 0, 0, 0, 0, 0} /* NotIn */
00189        },
00190        {
00191               {65, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
00192               {1, 64, 66, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
00193        },
00194 
00195        {
00196               {1, 65, 77, 0, 0, 0, 0, 0, 0, 0}, /* In */
00197               {2, 64, 66, 76, 78, 0, 0, 0, 0, 0} /* NotIn */
00198        },
00199        {
00200               {1, 2, 30, 64, 65, 77, 80, 81, 222222, 0}, /* In */
00201               {66, 76, 78, 79, 0, 0, 0, 0, 0, 0} /* NotIn */
00202        },
00203        {
00204               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
00205               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
00206        }
00207 };
00208 
00209 
00210 
00211 static void TestMSetHashlist (void)
00212 {
00213        int nTest = 0;
00214        int j;
00215        StrBuf *MSetStr;
00216        StrBuf *Assert;
00217        MSet *OneMSet;
00218        
00219 
00220        MSetStr = NewStrBuf();
00221        Assert = NewStrBuf();
00222        while (MSetStrings[nTest] != NULL)
00223        {
00224               StrBufPlain(MSetStr, MSetStrings[nTest], -1);
00225               ParseMSet(&OneMSet, MSetStr);
00226 
00227 //#ifdef VERBOSE_TEST
00228               printf("---%s---\n", ChrPtr(MSetStr));
00229               {
00230                      const char *HashKey;
00231                      long HKLen;
00232                      HashList *ScanMe = (HashList*) OneMSet;
00233                      HashPos *at;
00234                      void *vMsg;
00235                      long *end;
00236 
00237                      at = GetNewHashPos(ScanMe, 0);
00238                      while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
00239                             /* Are you a new message, or an old message? */
00240                             end = (long*) vMsg;
00241                             printf("[%ld][%ld]\n", *(long*)HashKey, *end);
00242                      }
00243                      DeleteHashPos(&at);
00244               }
00245 //#endif
00246 
00247               j = 0;
00248               while (MessageNumbers[nTest][InMSet][j] != 0)
00249               {
00250                      if (!IsInMSetList(OneMSet, MessageNumbers[nTest][InMSet][j]))
00251                      {
00252                             StrBufPrintf(Assert, "InFail: %s <-> %ld\n", 
00253                                         ChrPtr(MSetStr), 
00254                                         MessageNumbers[nTest][InMSet][j]);
00255                             CU_FAIL(ChrPtr(Assert));
00256                             printf(ChrPtr(Assert));
00257                      }
00258                      else
00259                      {
00260                             StrBufPrintf(Assert, "InPass: %s <-> %ld\n", 
00261                                         ChrPtr(MSetStr), 
00262                                         MessageNumbers[nTest][InMSet][j]);
00263                             CU_PASS(ChrPtr(Assert));
00264                      }
00265                      j++;   
00266               }
00267               j = 0;
00268               while (MessageNumbers[nTest][NotInMSet][j] != 0)
00269               {
00270                      if (IsInMSetList(OneMSet, MessageNumbers[nTest][NotInMSet][j]))
00271                      {
00272                             StrBufPrintf(Assert, "NOT-InFail: %s <-> %ld\n", 
00273                                         ChrPtr(MSetStr), 
00274                                         MessageNumbers[nTest][NotInMSet][j]);
00275                             CU_FAIL(ChrPtr(Assert));
00276                             printf(ChrPtr(Assert));
00277                      }
00278                      else
00279                      {
00280                             StrBufPrintf(Assert, "NOT-InPass: %s <-> %ld\n", 
00281                                         ChrPtr(MSetStr), 
00282                                         MessageNumbers[nTest][InMSet][j]);
00283                             CU_PASS(ChrPtr(Assert));
00284                      }
00285                      j++;
00286               }
00287               
00288 
00289               DeleteMSet(&OneMSet);
00290               nTest++;
00291               
00292        }
00293        FreeStrBuf(&MSetStr);
00294        FreeStrBuf(&Assert);
00295 }
00296 
00297 
00298 
00299 static void AddHashlistTests(void)
00300 {
00301        CU_pSuite pGroup = NULL;
00302        CU_pTest pTest = NULL;
00303 
00304        pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
00305        pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward);
00306        pTest = CU_add_test(pGroup, "TestHashlistAddDelete", TestHashlistAddDelete);
00307        pTest = CU_add_test(pGroup, "TestMSetHashlist", TestMSetHashlist);
00308 }
00309 
00310 
00311 int main(int argc, char* argv[])
00312 {
00313        setvbuf(stdout, NULL, _IONBF, 0);
00314 
00315        StartLibCitadel(8);
00316        CU_BOOL Run = CU_FALSE ;
00317        
00318        CU_set_output_filename("TestAutomated");
00319        if (CU_initialize_registry()) {
00320               printf("\nInitialize of test Registry failed.");
00321        }
00322        
00323        Run = CU_TRUE ;
00324        AddHashlistTests();
00325        
00326        if (CU_TRUE == Run) {
00327               //CU_console_run_tests();
00328     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
00329     
00331        }
00332        
00333        CU_cleanup_registry();
00334 
00335        return 0;
00336 }