* fix hashlist iterator
authorWilfried Göesgens <willi@citadel.org>
Sun, 11 Oct 2009 00:04:55 +0000 (00:04 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 11 Oct 2009 00:04:55 +0000 (00:04 +0000)
* add parameter so we can determine whether we're behind the end of the list

libcitadel/lib/hash.c
libcitadel/lib/libcitadel.h
libcitadel/tests/Makefile.in
libcitadel/tests/hashlist_test.c [new file with mode: 0644]

index 67ea9332d2ad8de73d406eb810092aadf0c281e2..e3be284023c196ddce920a4966c49adef566a9e4 100644 (file)
@@ -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;
index aca897882031539569c84953540fc149c0d169c1..cf628fe96c36ddc68ad38f38e2b491427a50f10a 100644 (file)
@@ -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);
index 95808e7d08e55229f00e7d0d93e1f2a63de5b1c3..5b01bda73621b36c54b6cc662ce651e9f0507ab6 100644 (file)
@@ -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 (file)
index 0000000..6bf1897
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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;
+}