From 25adc7cc329866f761a239e7a1500198419a1ff9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sun, 11 Oct 2009 00:04:55 +0000 Subject: [PATCH] * fix hashlist iterator * add parameter so we can determine whether we're behind the end of the list --- libcitadel/lib/hash.c | 24 ++--- libcitadel/lib/libcitadel.h | 2 +- libcitadel/tests/Makefile.in | 8 +- libcitadel/tests/hashlist_test.c | 165 +++++++++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 13 deletions(-) create mode 100644 libcitadel/tests/hashlist_test.c diff --git a/libcitadel/lib/hash.c b/libcitadel/lib/hash.c index 67ea9332d..e3be28402 100644 --- a/libcitadel/lib/hash.c +++ b/libcitadel/lib/hash.c @@ -588,8 +588,13 @@ HashPos *GetNewHashPos(HashList *Hash, int StepWidth) * \param the Iterator to analyze * \returns the n'th hashposition we point at */ -int GetHashPosCounter(HashPos *At) +int GetHashPosCounter(HashList *Hash, HashPos *At) { + if ((Hash == NULL) || + (At->Position >= Hash->nMembersUsed) || + (At->Position < 0) || + (At->Position > Hash->nMembersUsed)) + return 0; return At->Position; } @@ -619,7 +624,10 @@ int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKe long PayloadPos; long offset = 0; - if ((Hash == NULL) || (At->Position >= Hash->nMembersUsed) || (At->Position < 0)) + if ((Hash == NULL) || + (At->Position >= Hash->nMembersUsed) || + (At->Position < 0) || + (At->Position > Hash->nMembersUsed)) return 0; *HKLen = Hash->LookupTable[At->Position]->HKLen; *HashKey = Hash->LookupTable[At->Position]->HashKey; @@ -633,14 +641,6 @@ int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKe else At->Position += ((At->Position) % abs(At->StepWidth)) * (At->StepWidth / abs(At->StepWidth)); - - if (At->Position > Hash->nMembersUsed) { - At->Position = Hash->nMembersUsed - 1; - return 0; - } else if (At->Position <= 0) { - At->Position = 0; - return 0; - } return 1; } @@ -657,7 +657,9 @@ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void ** { long PayloadPos; - if ((Hash == NULL) || (At >= Hash->nMembersUsed)) + if ((Hash == NULL) || + (At < 0) || + (At > Hash->nMembersUsed)) return 0; *HKLen = Hash->LookupTable[At]->HKLen; *HashKey = Hash->LookupTable[At]->HashKey; diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index aca897882..cf628fe96 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -446,7 +446,7 @@ int GetHashKeys(HashList *Hash, char ***List); int dbg_PrintHash(HashList *Hash, PrintHashContent first, PrintHashContent Second); int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry); HashPos *GetNewHashPos(HashList *Hash, int StepWidth); -int GetHashPosCounter(HashPos *At); +int GetHashPosCounter(HashList *Hash, HashPos *At); void DeleteHashPos(HashPos **DelMe); int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data); int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data); diff --git a/libcitadel/tests/Makefile.in b/libcitadel/tests/Makefile.in index 95808e7d0..5b01bda73 100644 --- a/libcitadel/tests/Makefile.in +++ b/libcitadel/tests/Makefile.in @@ -16,7 +16,7 @@ top_builddir=`pwd` # End of configuration section -TARGETS=stringbuf_test stringbuf_IO_test stringbuf_conversion_test +TARGETS=stringbuf_test stringbuf_IO_test stringbuf_conversion_test hashlist_test all: $(TARGETS) @@ -48,6 +48,12 @@ stringbuf_conversion_test: $(LIBOBJS) stringbuf_conversion.o ../.libs/libcitadel.a \ -o stringbuf_conversion_test +hashlist_test: $(LIBOBJS) hashlist_test.o + $(CC) $(LDFLAGS) $(LIBOBJS) $(LIBS) \ + hashlist_test.o \ + ../.libs/libcitadel.a \ + -o hashlist_test + .c.o: $(CC) $(CFLAGS) $(DEFS) -c $< diff --git a/libcitadel/tests/hashlist_test.c b/libcitadel/tests/hashlist_test.c new file mode 100644 index 000000000..6bf1897d6 --- /dev/null +++ b/libcitadel/tests/hashlist_test.c @@ -0,0 +1,165 @@ + +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * + * This library is free 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. + * + * This library is distributed in the hope that it will be useful, + * 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 +#include +#include + +#include "stringbuf_test.h" +#include "../lib/libcitadel.h" + + +static HashList *GetFilledHash(int n, int stepwidth) +{ + HashList* TestHash; + int i; + int *val; + + TestHash = NewHash(1, Flathash); + + for (i = 0; i < n; i+=stepwidth) + { + val = (int*) malloc(sizeof(int)); + *val = i; + Put(TestHash, IKEY(i), val, NULL); + } + return TestHash; +} + + + +static void test_iterate_hash(HashList *testh, int forward, int stepwidth) +{ + int i = 0; + HashPos *it; + void *vTest; + long len = 0; + const char *Key; + int dir = 1; + + if (forward == 0) + dir = -1; + it = GetNewHashPos(testh, dir * stepwidth); + while (GetNextHashPos(testh, it, &len, &Key, &vTest) && + (vTest != NULL)) { + + printf("i: %d c: %d\n", i, *(int*) vTest); + i+=stepwidth; + } + +} + +static void TestHashlistIteratorForward (void) +{ + HashList *H; + + H = GetFilledHash (10, 1); + + test_iterate_hash(H, 1, 1); + printf("\n"); + + test_iterate_hash(H, 0, 1); + printf("\n"); + + test_iterate_hash(H, 1, 2); + printf("\n"); + + test_iterate_hash(H, 0, 2); + printf("\n"); + + test_iterate_hash(H, 1, 3); + printf("\n"); + + test_iterate_hash(H, 0, 3); + printf("\n"); + + DeleteHash(&H); +} +/* +Some samples from the original... + CU_ASSERT_EQUAL(10, 10); + CU_ASSERT_EQUAL(0, -0); + CU_ASSERT_EQUAL(-12, -12); + CU_ASSERT_NOT_EQUAL(10, 11); + CU_ASSERT_NOT_EQUAL(0, -1); + CU_ASSERT_NOT_EQUAL(-12, -11); + CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100); + CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101); + CU_ASSERT_PTR_NULL(NULL); + CU_ASSERT_PTR_NULL(0x0); + CU_ASSERT_PTR_NOT_NULL((void*)0x23); + CU_ASSERT_STRING_EQUAL(str1, str2); + CU_ASSERT_STRING_NOT_EQUAL(str1, str2); + CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1)); + CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1)); + CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1); + CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3); + CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1); + CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001); + CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001); + CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001); + CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001); + CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001); + CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001); + CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001); + CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001); +*/ + + + + + +static void AddHashlistTests(void) +{ + CU_pSuite pGroup = NULL; + CU_pTest pTest = NULL; + + pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL); + pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward); + +} + + +int main(int argc, char* argv[]) +{ + setvbuf(stdout, NULL, _IONBF, 0); + + StartLibCitadel(8); + CU_BOOL Run = CU_FALSE ; + + CU_set_output_filename("TestAutomated"); + if (CU_initialize_registry()) { + printf("\nInitialize of test Registry failed."); + } + + Run = CU_TRUE ; + AddHashlistTests(); + + if (CU_TRUE == Run) { + //CU_console_run_tests(); + printf("\nTests completed with return value %d.\n", CU_basic_run_tests()); + + ///CU_automated_run_tests(); + } + + CU_cleanup_registry(); + + return 0; +} -- 2.30.2