From e253a2509ae648a358991103fafbd18d21bb06cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 18 Nov 2014 20:12:25 +0100 Subject: [PATCH] tests: add hashtable tests with multiple items giving same hashed key --- tests/unit/core/test-hashtable.cpp | 59 +++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/unit/core/test-hashtable.cpp b/tests/unit/core/test-hashtable.cpp index e92ea328f..d34e5cf07 100644 --- a/tests/unit/core/test-hashtable.cpp +++ b/tests/unit/core/test-hashtable.cpp @@ -264,9 +264,66 @@ TEST(Hashtable, SetGetRemove) hashtable_remove_all (hashtable); LONGS_EQUAL(0, hashtable->items_count); - /* free hashtable */ + /* free hashtables */ hashtable_free (hashtable); hashtable_free (hashtable2); + + /* + * create a hashtable with size 8, and add 6 items, + * to check if many items with same hashed key work fine, + * the expected htable inside hashtable is: + * +-----+ + * | 0 | + * +-----+ + * | 1 | + * +-----+ + * | 2 | --> "extensible" + * +-----+ + * | 3 | --> "fast" --> "light" + * +-----+ + * | 4 | + * +-----+ + * | 5 | --> "chat" + * +-----+ + * | 6 | --> "client" + * +-----+ + * | 7 | --> "weechat" + * +-----+ + */ + hashtable = hashtable_new (8, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, + NULL); + LONGS_EQUAL(8, hashtable->size); + LONGS_EQUAL(0, hashtable->items_count); + + item = hashtable_set (hashtable, "weechat", NULL); + CHECK(item); + POINTERS_EQUAL(item, hashtable->htable[7]); + + item = hashtable_set (hashtable, "fast", NULL); + CHECK(item); + POINTERS_EQUAL(item, hashtable->htable[3]); + + item = hashtable_set (hashtable, "light", NULL); + CHECK(item); + POINTERS_EQUAL(item, hashtable->htable[3]->next_item); + + item = hashtable_set (hashtable, "extensible", NULL); + CHECK(item); + POINTERS_EQUAL(item, hashtable->htable[2]); + + item = hashtable_set (hashtable, "chat", NULL); + CHECK(item); + POINTERS_EQUAL(item, hashtable->htable[5]); + + item = hashtable_set (hashtable, "client", NULL); + CHECK(item); + POINTERS_EQUAL(item, hashtable->htable[6]); + + /* free hashtable */ + hashtable_free (hashtable); } /*