X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=libcitadel%2Ftests%2Fhashlist_test.c;h=69ee47d27aec64beb247ee0ca1dc44abfa7d10fa;hb=b826c3117bb7ddf1386a4811cb2eb47ea4e1097c;hp=6bf1897d65d1242fc23edd54e114306b5198092f;hpb=25adc7cc329866f761a239e7a1500198419a1ff9;p=citadel.git diff --git a/libcitadel/tests/hashlist_test.c b/libcitadel/tests/hashlist_test.c index 6bf1897d6..69ee47d27 100644 --- a/libcitadel/tests/hashlist_test.c +++ b/libcitadel/tests/hashlist_test.c @@ -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. @@ -12,10 +11,6 @@ * 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 @@ -63,15 +58,11 @@ static void test_iterate_hash(HashList *testh, int forward, int stepwidth) printf("i: %d c: %d\n", i, *(int*) vTest); i+=stepwidth; } - + DeleteHashPos(&it); } -static void TestHashlistIteratorForward (void) +static void ValidateBackAndForth(HashList *H) { - HashList *H; - - H = GetFilledHash (10, 1); - test_iterate_hash(H, 1, 1); printf("\n"); @@ -89,9 +80,65 @@ static void TestHashlistIteratorForward (void) test_iterate_hash(H, 0, 3); printf("\n"); +} + +static void TestHashlistIteratorForward (void) +{ + HashList *H; + + H = GetFilledHash (10, 1); + + ValidateBackAndForth(H); + + DeleteHash(&H); +} + + +static void TestHashlistAddDelete (void) +{ + HashList *H; + HashPos *at; + int *val, i; + + H = GetFilledHash (10, 1); + + at = GetNewHashPos(H, 0); + + printf("Remove first\n"); + DeleteEntryFromHash(H, at); + DeleteHashPos(&at); + ValidateBackAndForth(H); + + printf("Insert 15\n"); + i = 15; + val = (int*) malloc(sizeof(int)); + *val = i; + Put(H, IKEY(i), val, NULL); + + ValidateBackAndForth(H); + + printf("Remove third\n"); + at = GetNewHashPos(H, 0); + NextHashPos(H, at); + NextHashPos(H, at); + NextHashPos(H, at); + DeleteEntryFromHash(H, at); + + ValidateBackAndForth(H); + printf("Insert -15\n"); + + i = -15; + val = (int*) malloc(sizeof(int)); + *val = i; + Put(H, IKEY(i), val, NULL); + + ValidateBackAndForth(H); + + DeleteHashPos(&at); DeleteHash(&H); } + /* Some samples from the original... CU_ASSERT_EQUAL(10, 10); @@ -124,6 +171,129 @@ Some samples from the original... +const char *MSetStrings[] = { + "11:63", + "65", + "1,65,77", + "1:65,77,80:*", + NULL +}; + +#define InMSet 0 +#define NotInMSet 1 +const long MessageNumbers[5][2][10] = { +/* First MSet */ + { + {11, 59, 63, 0, 0, 0, 0, 0, 0, 0}, /* In */ + {2, 10, 64, 65, 0, 0, 0, 0, 0, 0} /* NotIn */ + }, + { + {65, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */ + {1, 64, 66, 0, 0, 0, 0, 0, 0, 0} /* NotIn */ + }, + + { + {1, 65, 77, 0, 0, 0, 0, 0, 0, 0}, /* In */ + {2, 64, 66, 76, 78, 0, 0, 0, 0, 0} /* NotIn */ + }, + { + {1, 2, 30, 64, 65, 77, 80, 81, 222222, 0}, /* In */ + {66, 76, 78, 79, 0, 0, 0, 0, 0, 0} /* NotIn */ + }, + { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */ + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* NotIn */ + } +}; + + + +static void TestMSetHashlist (void) +{ + int nTest = 0; + int j; + StrBuf *MSetStr; + StrBuf *Assert; + MSet *OneMSet; + + + MSetStr = NewStrBuf(); + Assert = NewStrBuf(); + while (MSetStrings[nTest] != NULL) + { + StrBufPlain(MSetStr, MSetStrings[nTest], -1); + ParseMSet(&OneMSet, MSetStr); + +//#ifdef VERBOSE_TEST + printf("---%s---\n", ChrPtr(MSetStr)); + { + const char *HashKey; + long HKLen; + HashList *ScanMe = (HashList*) OneMSet; + HashPos *at; + void *vMsg; + long *end; + + at = GetNewHashPos(ScanMe, 0); + while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) { + /* Are you a new message, or an old message? */ + end = (long*) vMsg; + printf("[%ld][%ld]\n", *(long*)HashKey, *end); + } + DeleteHashPos(&at); + } +//#endif + + j = 0; + while (MessageNumbers[nTest][InMSet][j] != 0) + { + if (!IsInMSetList(OneMSet, MessageNumbers[nTest][InMSet][j])) + { + StrBufPrintf(Assert, "InFail: %s <-> %ld\n", + ChrPtr(MSetStr), + MessageNumbers[nTest][InMSet][j]); + CU_FAIL(ChrPtr(Assert)); + printf("%s", ChrPtr(Assert)); + } + else + { + StrBufPrintf(Assert, "InPass: %s <-> %ld\n", + ChrPtr(MSetStr), + MessageNumbers[nTest][InMSet][j]); + CU_PASS(ChrPtr(Assert)); + } + j++; + } + j = 0; + while (MessageNumbers[nTest][NotInMSet][j] != 0) + { + if (IsInMSetList(OneMSet, MessageNumbers[nTest][NotInMSet][j])) + { + StrBufPrintf(Assert, "NOT-InFail: %s <-> %ld\n", + ChrPtr(MSetStr), + MessageNumbers[nTest][NotInMSet][j]); + CU_FAIL(ChrPtr(Assert)); + printf("%s", ChrPtr(Assert)); + } + else + { + StrBufPrintf(Assert, "NOT-InPass: %s <-> %ld\n", + ChrPtr(MSetStr), + MessageNumbers[nTest][InMSet][j]); + CU_PASS(ChrPtr(Assert)); + } + j++; + } + + + DeleteMSet(&OneMSet); + nTest++; + + } + FreeStrBuf(&MSetStr); + FreeStrBuf(&Assert); +} + static void AddHashlistTests(void) @@ -133,7 +303,8 @@ static void AddHashlistTests(void) pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL); pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward); - + pTest = CU_add_test(pGroup, "TestHashlistAddDelete", TestHashlistAddDelete); + pTest = CU_add_test(pGroup, "TestMSetHashlist", TestMSetHashlist); } @@ -141,7 +312,6 @@ int main(int argc, char* argv[]) { setvbuf(stdout, NULL, _IONBF, 0); - StartLibCitadel(8); CU_BOOL Run = CU_FALSE ; CU_set_output_filename("TestAutomated");