Commit d0f3e5e5 authored by Pekka Pessi's avatar Pekka Pessi

sofia-sip/htable.h: do not fail assert in htable_remove() if entry is not found

Changed htable_remove() prototype, it now returns -1.

darcs-hash:20070321104004-55b16-3d8724e5b6a643f9cbecf390cff1c65a24fb7213.gz
parent bcade973
......@@ -106,7 +106,7 @@ HTABLE_SCOPE entry_t **prefix##_hash(prefix##_t const *, hash_value_t hv); \
HTABLE_SCOPE entry_t **prefix##_next(prefix##_t const *, entry_t * const *ee); \
HTABLE_SCOPE void prefix##_append(prefix##_t *pr, entry_t const *e); \
HTABLE_SCOPE void prefix##_insert(prefix##_t *pr, entry_t const *e); \
HTABLE_SCOPE void prefix##_remove(prefix##_t *, entry_t const *e)
HTABLE_SCOPE int prefix##_remove(prefix##_t *, entry_t const *e)
/** Hash table implementation.
*
......@@ -220,13 +220,13 @@ void prefix##_insert(prefix##_t *pr, entry_t const *e) \
} \
\
HTABLE_SCOPE \
void prefix##_remove(prefix##_t *pr, entry_t const *e) \
int prefix##_remove(prefix##_t *pr, entry_t const *e) \
{ \
size_t i, j, k; \
size_t size = pr->pr##_size; \
entry_t **htable = pr->pr##_table; \
\
if (!e) return; \
if (!e) return -1; \
\
/* Search for entry */ \
for (i = hfun(e) % size; htable[i]; i = (i + 1) % size) \
......@@ -234,7 +234,7 @@ void prefix##_remove(prefix##_t *pr, entry_t const *e) \
break; \
\
/* Entry is not in table? */ \
assert(htable[i]); if (!e) return; \
if (!htable[i]) return -1; \
\
/* Move table entries towards their primary place */ \
for (j = (i + 1) % size; htable[j]; j = (j + 1) % size) { \
......@@ -252,6 +252,8 @@ void prefix##_remove(prefix##_t *pr, entry_t const *e) \
pr->pr##_used--; \
\
htable[i] = NULL; \
\
return 0; \
} \
extern int prefix##_dummy
......
......@@ -240,7 +240,7 @@ int prefix##_remove(type *pr, entrytype const e) \
if (is_equal(e, htable[i])) \
break; \
\
assert(is_used(htable[i])); if (!is_used(htable[i])) return -1; \
if (!is_used(htable[i])) return -1; \
\
/* Move table entries towards their primary place */ \
for (j = (i + 1) % size; is_used(htable[j]); j = (j + 1) % size) { \
......
......@@ -181,44 +181,46 @@ int table_test(int flags)
BEGIN();
TEST0(c = context_create());
TEST0(add(c, 0, 1)); TEST0(c->c_hash->ht_table[0]);
TEST0(add(c, 1, 2)); TEST0(c->c_hash->ht_table[1]);
TEST0(add(c, 2, 3)); TEST0(c->c_hash->ht_table[2]);
TEST0(add(c, 0, 4)); TEST0(c->c_hash->ht_table[3]);
TEST0(add(c, 2, 5)); TEST0(c->c_hash->ht_table[4]);
TEST0(e = search(c, 1, 2, 0));
zap(c, e);
TEST0(c->c_hash->ht_table[0]);
TEST0(c->c_hash->ht_table[1]);
TEST0(c->c_hash->ht_table[2]);
TEST0(c->c_hash->ht_table[3]);
TEST0(c->c_hash->ht_table[4] == NULL);
TEST_1(c = context_create());
TEST_1(add(c, 0, 1)); TEST_1(c->c_hash->ht_table[0]);
TEST_1(add(c, 1, 2)); TEST_1(c->c_hash->ht_table[1]);
TEST_1(add(c, 2, 3)); TEST_1(c->c_hash->ht_table[2]);
TEST_1(add(c, 0, 4)); TEST_1(c->c_hash->ht_table[3]);
TEST_1(add(c, 2, 5)); TEST_1(c->c_hash->ht_table[4]);
TEST_1(e = search(c, 1, 2, 0));
TEST(htable_remove(c->c_hash, e), 0);
TEST(htable_remove(c->c_hash, e), -1);
su_free(c->c_home, e);
TEST_1(c->c_hash->ht_table[0]);
TEST_1(c->c_hash->ht_table[1]);
TEST_1(c->c_hash->ht_table[2]);
TEST_1(c->c_hash->ht_table[3]);
TEST_1(c->c_hash->ht_table[4] == NULL);
zap(c, c->c_hash->ht_table[0]);
TEST0(c->c_hash->ht_table[0]);
TEST0(c->c_hash->ht_table[1] == NULL);
TEST0(c->c_hash->ht_table[2]);
TEST0(c->c_hash->ht_table[3]);
TEST0(c->c_hash->ht_table[4] == NULL);
TEST_1(c->c_hash->ht_table[0]);
TEST_1(c->c_hash->ht_table[1] == NULL);
TEST_1(c->c_hash->ht_table[2]);
TEST_1(c->c_hash->ht_table[3]);
TEST_1(c->c_hash->ht_table[4] == NULL);
TEST0(add(c, 0, 6)); TEST0(c->c_hash->ht_table[1]);
TEST0(add(c, 1, 7)); TEST0(c->c_hash->ht_table[4]);
TEST_1(add(c, 0, 6)); TEST_1(c->c_hash->ht_table[1]);
TEST_1(add(c, 1, 7)); TEST_1(c->c_hash->ht_table[4]);
/* Test that zapping entry 0 does not move 2 and 3 */
zap(c, c->c_hash->ht_table[0]);
TEST0(c->c_hash->ht_table[4] == NULL);
TEST_1(c->c_hash->ht_table[4] == NULL);
{
/* Insert entries at the end of hash, then resize and check
for correct ordering */
hash_value_t size = c->c_hash->ht_size, h = size - 1;
TEST0(add(c, h, 0)); TEST0(add(c, h, 1)); TEST0(add(c, h, 2));
TEST0(add(c, h, 3)); TEST0(add(c, h, 4)); TEST0(add(c, h, 5));
TEST0(add(c, h, 6)); TEST0(add(c, h, 7)); TEST0(add(c, h, 8));
TEST_1(add(c, h, 0)); TEST_1(add(c, h, 1)); TEST_1(add(c, h, 2));
TEST_1(add(c, h, 3)); TEST_1(add(c, h, 4)); TEST_1(add(c, h, 5));
TEST_1(add(c, h, 6)); TEST_1(add(c, h, 7)); TEST_1(add(c, h, 8));
TEST(count(c, h), 9);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment