* DeleteEntryFromHash(): don't skip indirection via the lookuptable, else we free...
authorWilfried Göesgens <willi@citadel.org>
Sat, 24 Apr 2010 12:49:16 +0000 (12:49 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sat, 24 Apr 2010 12:49:16 +0000 (12:49 +0000)
* DeleteEntryFromHash(): NULL the end of the list; This is the entry we just removed.
* hashlist_test: add some tests with removing items from the list

libcitadel/lib/hash.c
libcitadel/tests/hashlist_test.c

index f9ceeced3d82fe94cd282c56d7d970af5f24ff81..ac6a377ff16ff188a62d2bfc9bea993b8ac63042 100644 (file)
@@ -651,8 +651,8 @@ int DeleteEntryFromHash(HashList *Hash, HashPos *At)
                return 0;
        }
 
-       FreeMe = Hash->Members[At->Position];
-       Hash->Members[At->Position] = NULL;
+       FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position];
+       Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL;
 
 
        /** delete our hashing data */
@@ -667,6 +667,7 @@ int DeleteEntryFromHash(HashList *Hash, HashPos *At)
                                (Hash->nLookupTableItems - At->Position - 1) * 
                                sizeof(HashKey*));
 
+                       Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL;
                }
                else 
                        Hash->LookupTable[At->Position] = NULL;
index 7c7a9ad6ed83f15b7ab505de6e6cd9b4fed84607..fc638ccbcf59a1c3c917d1d8c5da8b188c87437a 100644 (file)
@@ -66,12 +66,8 @@ static void test_iterate_hash(HashList *testh, int forward, int 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 +85,64 @@ 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);
+
+       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);
+
 
        DeleteHash(&H);
 }
+
 /*
 Some samples from the original...
        CU_ASSERT_EQUAL(10, 10);
@@ -133,6 +184,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);
+
 
 }