summaryrefslogtreecommitdiff
path: root/tests/unit/core/test-core-hashtable.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/core/test-core-hashtable.cpp')
-rw-r--r--tests/unit/core/test-core-hashtable.cpp404
1 files changed, 395 insertions, 9 deletions
diff --git a/tests/unit/core/test-core-hashtable.cpp b/tests/unit/core/test-core-hashtable.cpp
index abcaec2ca..2e209847c 100644
--- a/tests/unit/core/test-core-hashtable.cpp
+++ b/tests/unit/core/test-core-hashtable.cpp
@@ -25,6 +25,8 @@ extern "C"
{
#include <string.h>
#include "src/core/wee-hashtable.h"
+#include "src/core/wee-infolist.h"
+#include "src/core/wee-list.h"
#include "src/plugins/plugin.h"
}
@@ -34,8 +36,27 @@ extern "C"
#define HASHTABLE_TEST_KEY_LONG_HASH 11232856562070989738ULL
#define HASHTABLE_TEST_VALUE "this is a value"
+char *test_map_string = NULL;
+
TEST_GROUP(CoreHashtable)
{
+ struct t_hashtable *get_weechat_hashtable ()
+ {
+ struct t_hashtable *hashtable;
+
+ hashtable = hashtable_new (8,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING,
+ NULL,
+ NULL);
+ hashtable_set (hashtable, "weechat", "the first item");
+ hashtable_set (hashtable, "light", "item2");
+ hashtable_set (hashtable, "fast", "item3");
+ hashtable_set (hashtable, "extensible", "item4");
+ hashtable_set (hashtable, "chat", "item5");
+ hashtable_set (hashtable, "client", "last item");
+ return hashtable;
+ }
};
/*
@@ -121,6 +142,8 @@ TEST(CoreHashtable, New)
LONGS_EQUAL(32, hashtable->size);
CHECK(hashtable->htable);
LONGS_EQUAL(0, hashtable->items_count);
+ POINTERS_EQUAL(NULL, hashtable->oldest_item);
+ POINTERS_EQUAL(NULL, hashtable->newest_item);
LONGS_EQUAL(HASHTABLE_STRING, hashtable->type_keys);
LONGS_EQUAL(HASHTABLE_INTEGER, hashtable->type_values);
POINTERS_EQUAL(&test_hashtable_hash_key_cb, hashtable->callback_hash_key);
@@ -169,23 +192,31 @@ TEST(CoreHashtable, SetGetRemove)
item = hashtable_set (hashtable, str_key, NULL);
CHECK(item);
LONGS_EQUAL(1, hashtable->items_count);
+ CHECK(hashtable->oldest_item);
+ CHECK(hashtable->newest_item);
STRCMP_EQUAL(str_key, (const char *)item->key);
LONGS_EQUAL(strlen (str_key) + 1, item->key_size);
POINTERS_EQUAL(NULL, item->value);
LONGS_EQUAL(0, item->value_size);
POINTERS_EQUAL(NULL, item->prev_item);
POINTERS_EQUAL(NULL, item->next_item);
+ POINTERS_EQUAL(NULL, item->prev_created_item);
+ POINTERS_EQUAL(NULL, item->next_created_item);
/* set a string value for the same key */
item = hashtable_set (hashtable, str_key, str_value);
CHECK(item);
LONGS_EQUAL(1, hashtable->items_count);
+ CHECK(hashtable->oldest_item);
+ CHECK(hashtable->newest_item);
STRCMP_EQUAL(str_key, (const char *)item->key);
LONGS_EQUAL(strlen (str_key) + 1, item->key_size);
STRCMP_EQUAL(str_value, (const char *)item->value);
LONGS_EQUAL(strlen (str_value) + 1, item->value_size);
POINTERS_EQUAL(NULL, item->prev_item);
POINTERS_EQUAL(NULL, item->next_item);
+ POINTERS_EQUAL(NULL, item->prev_created_item);
+ POINTERS_EQUAL(NULL, item->next_created_item);
/* get item */
item = hashtable_get_item (hashtable, str_key, &hash);
@@ -208,6 +239,8 @@ TEST(CoreHashtable, SetGetRemove)
/* delete an item */
hashtable_remove (hashtable, str_key);
LONGS_EQUAL(0, hashtable->items_count);
+ POINTERS_EQUAL(NULL, hashtable->oldest_item);
+ POINTERS_EQUAL(NULL, hashtable->newest_item);
/* add an item with size in hashtable */
item = hashtable_set_with_size (hashtable,
@@ -215,6 +248,8 @@ TEST(CoreHashtable, SetGetRemove)
str_value, strlen (str_value) + 1);
CHECK(item);
LONGS_EQUAL(1, hashtable->items_count);
+ CHECK(hashtable->oldest_item);
+ CHECK(hashtable->newest_item);
STRCMP_EQUAL(str_key, (const char *)item->key);
LONGS_EQUAL(strlen (str_key) + 1, item->key_size);
STRCMP_EQUAL(str_value, (const char *)item->value);
@@ -223,6 +258,15 @@ TEST(CoreHashtable, SetGetRemove)
/* add another item */
hashtable_set (hashtable, "xxx", "zzz");
LONGS_EQUAL(2, hashtable->items_count);
+ CHECK(hashtable->oldest_item);
+ CHECK(hashtable->newest_item);
+ CHECK(hashtable->oldest_item != hashtable->newest_item);
+ CHECK(hashtable->oldest_item->next_created_item == hashtable->newest_item);
+ STRCMP_EQUAL(str_key, (const char *)hashtable->oldest_item->key);
+ STRCMP_EQUAL("xxx",
+ (const char *)hashtable->oldest_item->next_created_item->key);
+ STRCMP_EQUAL("xxx",
+ (const char *)hashtable->newest_item->key);
/*
* test duplication of hashtable and check that duplicated content is
@@ -273,7 +317,17 @@ TEST(CoreHashtable, SetGetRemove)
/* remove all items */
hashtable_remove_all (hashtable);
+ POINTERS_EQUAL(NULL, hashtable->htable[0]);
+ POINTERS_EQUAL(NULL, hashtable->htable[1]);
+ POINTERS_EQUAL(NULL, hashtable->htable[2]);
+ POINTERS_EQUAL(NULL, hashtable->htable[3]);
+ POINTERS_EQUAL(NULL, hashtable->htable[4]);
+ POINTERS_EQUAL(NULL, hashtable->htable[5]);
+ POINTERS_EQUAL(NULL, hashtable->htable[6]);
+ POINTERS_EQUAL(NULL, hashtable->htable[7]);
LONGS_EQUAL(0, hashtable->items_count);
+ POINTERS_EQUAL(NULL, hashtable->oldest_item);
+ POINTERS_EQUAL(NULL, hashtable->newest_item);
/* free hashtables */
hashtable_free (hashtable);
@@ -313,13 +367,13 @@ TEST(CoreHashtable, SetGetRemove)
CHECK(item);
POINTERS_EQUAL(item, hashtable->htable[7]);
- item = hashtable_set (hashtable, "fast", NULL);
+ item = hashtable_set (hashtable, "light", NULL);
CHECK(item);
POINTERS_EQUAL(item, hashtable->htable[3]);
- item = hashtable_set (hashtable, "light", NULL);
+ item = hashtable_set (hashtable, "fast", NULL);
CHECK(item);
- POINTERS_EQUAL(item, hashtable->htable[3]->next_item);
+ POINTERS_EQUAL(item, hashtable->htable[3]);
item = hashtable_set (hashtable, "extensible", NULL);
CHECK(item);
@@ -333,32 +387,335 @@ TEST(CoreHashtable, SetGetRemove)
CHECK(item);
POINTERS_EQUAL(item, hashtable->htable[6]);
+ /* check items by order of creation */
+ ptr_item = hashtable->oldest_item;
+ STRCMP_EQUAL("weechat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("light", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("fast", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("extensible", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("chat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("client", (const char *)ptr_item->key);
+ STRCMP_EQUAL("client", (const char *)hashtable->newest_item->key);
+ ptr_item = ptr_item->next_created_item;
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ /* remove items and check again by order of creation */
+ hashtable_remove (hashtable, "fast");
+ LONGS_EQUAL(5, hashtable->items_count);
+ ptr_item = hashtable->oldest_item;
+ STRCMP_EQUAL("weechat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("light", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("extensible", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("chat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("client", (const char *)ptr_item->key);
+ STRCMP_EQUAL("client", (const char *)hashtable->newest_item->key);
+ ptr_item = ptr_item->next_created_item;
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ hashtable_remove (hashtable, "light");
+ LONGS_EQUAL(4, hashtable->items_count);
+ ptr_item = hashtable->oldest_item;
+ STRCMP_EQUAL("weechat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("extensible", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("chat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("client", (const char *)ptr_item->key);
+ STRCMP_EQUAL("client", (const char *)hashtable->newest_item->key);
+ ptr_item = ptr_item->next_created_item;
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ hashtable_remove (hashtable, "weechat");
+ LONGS_EQUAL(3, hashtable->items_count);
+ ptr_item = hashtable->oldest_item;
+ STRCMP_EQUAL("extensible", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("chat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("client", (const char *)ptr_item->key);
+ STRCMP_EQUAL("client", (const char *)hashtable->newest_item->key);
+ ptr_item = ptr_item->next_created_item;
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ hashtable_remove (hashtable, "client");
+ LONGS_EQUAL(2, hashtable->items_count);
+ ptr_item = hashtable->oldest_item;
+ STRCMP_EQUAL("extensible", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("chat", (const char *)ptr_item->key);
+ STRCMP_EQUAL("chat", (const char *)hashtable->newest_item->key);
+ ptr_item = ptr_item->next_created_item;
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ /* check current content of hashtable */
+ POINTERS_EQUAL(NULL, hashtable->htable[0]);
+ POINTERS_EQUAL(NULL, hashtable->htable[1]);
+ STRCMP_EQUAL("extensible", (const char *)hashtable->htable[2]->key);
+ POINTERS_EQUAL(NULL, hashtable->htable[3]);
+ POINTERS_EQUAL(NULL, hashtable->htable[4]);
+ STRCMP_EQUAL("chat", (const char *)hashtable->htable[5]->key);
+ POINTERS_EQUAL(NULL, hashtable->htable[6]);
+ POINTERS_EQUAL(NULL, hashtable->htable[7]);
+
/* free hashtable */
hashtable_free (hashtable);
}
+void
+test_hashtable_map_string_cb (void *data,
+ struct t_hashtable *hashtable,
+ const char *key, const char *value)
+{
+ /* make C++ compiler happy */
+ (void) hashtable;
+ (void) data;
+
+ if (test_map_string[0])
+ strcat (test_map_string, ";");
+ strcat (test_map_string, key);
+ strcat (test_map_string, ":");
+ strcat (test_map_string, value);
+}
+
/*
* Tests functions:
- * hashtable_map
* hashtable_map_string
*/
-TEST(CoreHashtable, Map)
+TEST(CoreHashtable, MapString)
{
- /* TODO: write tests */
+ struct t_hashtable *hashtable;
+ int value_int;
+ void *value_ptr;
+ time_t value_time;
+ char result[1024], value_buffer[3] = { 0x01, 0x05, 0x09 };
+
+ test_map_string = (char *)malloc (1024);
+
+ /* string -> string */
+ test_map_string[0] = '\0';
+ hashtable = get_weechat_hashtable ();
+ hashtable_map_string (hashtable, &test_hashtable_map_string_cb, NULL);
+ STRCMP_EQUAL("weechat:the first item;light:item2;fast:item3;"
+ "extensible:item4;chat:item5;client:last item",
+ test_map_string);
+
+ /* integer -> pointer */
+ test_map_string[0] = '\0';
+ hashtable = hashtable_new (8,
+ WEECHAT_HASHTABLE_INTEGER,
+ WEECHAT_HASHTABLE_POINTER,
+ NULL,
+ NULL);
+ value_int = 123;
+ value_ptr = (void *)0x123abc;
+ hashtable_set (hashtable, &value_int, value_ptr);
+ value_int = 45678;
+ value_ptr = (void *)0xdef789;
+ hashtable_set (hashtable, &value_int, value_ptr);
+ hashtable_map_string (hashtable, &test_hashtable_map_string_cb, NULL);
+ STRCMP_EQUAL("123:0x123abc;45678:0xdef789", test_map_string);
+
+ /* time -> buffer */
+ test_map_string[0] = '\0';
+ hashtable = hashtable_new (8,
+ WEECHAT_HASHTABLE_TIME,
+ WEECHAT_HASHTABLE_BUFFER,
+ NULL,
+ NULL);
+ value_time = 1624693124;
+ hashtable_set_with_size (hashtable,
+ &value_time, 0,
+ value_buffer, sizeof (value_buffer));
+ hashtable_map_string (hashtable, &test_hashtable_map_string_cb, NULL);
+ snprintf (result, sizeof (result),
+ "1624693124:0x%lx",
+ (unsigned long)(hashtable->newest_item->value));
+ STRCMP_EQUAL(result, test_map_string);
+
+ free (test_map_string);
+}
+
+/*
+ * Tests functions:
+ * hashtable_map
+ * hashtable_dup
+ */
+
+TEST(CoreHashtable, Dup)
+{
+ struct t_hashtable *hashtable, *hashtable2;
+ struct t_hashtable_item *ptr_item;
+
+ hashtable = get_weechat_hashtable ();
+
+ POINTERS_EQUAL(NULL, hashtable_dup (NULL));
+
+ hashtable2 = hashtable_dup (hashtable);
+
+ LONGS_EQUAL(6, hashtable2->items_count);
+ ptr_item = hashtable2->oldest_item;
+ STRCMP_EQUAL("weechat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("light", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("fast", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("extensible", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("chat", (const char *)ptr_item->key);
+ ptr_item = ptr_item->next_created_item;
+ STRCMP_EQUAL("client", (const char *)ptr_item->key);
+ STRCMP_EQUAL("client", (const char *)hashtable2->newest_item->key);
+ ptr_item = ptr_item->next_created_item;
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ hashtable_free (hashtable);
+ hashtable_free (hashtable2);
}
/*
* Tests functions:
* hashtable_get_list_keys
+ */
+
+TEST(CoreHashtable, GetListKeys)
+{
+ struct t_hashtable *hashtable;
+ struct t_weelist *list_keys;
+
+ hashtable = get_weechat_hashtable ();
+
+ POINTERS_EQUAL(NULL, hashtable_get_list_keys (NULL));
+
+ list_keys = hashtable_get_list_keys (hashtable);
+ CHECK(list_keys);
+ STRCMP_EQUAL("chat", weelist_string (weelist_get (list_keys, 0)));
+ STRCMP_EQUAL("client", weelist_string (weelist_get (list_keys, 1)));
+ STRCMP_EQUAL("extensible", weelist_string (weelist_get (list_keys, 2)));
+ STRCMP_EQUAL("fast", weelist_string (weelist_get (list_keys, 3)));
+ STRCMP_EQUAL("light", weelist_string (weelist_get (list_keys, 4)));
+ STRCMP_EQUAL("weechat", weelist_string (weelist_get (list_keys, 5)));
+
+ hashtable_free (hashtable);
+}
+
+/*
+ * Tests functions:
* hashtable_get_integer
+ */
+
+TEST(CoreHashtable, GetInteger)
+{
+ struct t_hashtable *hashtable;
+
+ hashtable = get_weechat_hashtable ();
+
+ LONGS_EQUAL(0, hashtable_get_integer (NULL, NULL));
+ LONGS_EQUAL(0, hashtable_get_integer (hashtable, NULL));
+ LONGS_EQUAL(0, hashtable_get_integer (hashtable, ""));
+ LONGS_EQUAL(0, hashtable_get_integer (hashtable, "unknown"));
+
+ LONGS_EQUAL(8, hashtable_get_integer (hashtable, "size"));
+ LONGS_EQUAL(6, hashtable_get_integer (hashtable, "items_count"));
+
+ hashtable_free (hashtable);
+}
+
+/*
+ * Tests functions:
* hashtable_get_string
+ */
+
+TEST(CoreHashtable, GetString)
+{
+ struct t_hashtable *hashtable;
+
+ hashtable = get_weechat_hashtable ();
+
+ POINTERS_EQUAL(NULL, hashtable_get_string (NULL, NULL));
+ POINTERS_EQUAL(NULL, hashtable_get_string (hashtable, NULL));
+ POINTERS_EQUAL(NULL, hashtable_get_string (hashtable, ""));
+ POINTERS_EQUAL(NULL, hashtable_get_string (hashtable, "unknown"));
+
+ STRCMP_EQUAL("string", hashtable_get_string (hashtable, "type_keys"));
+ STRCMP_EQUAL("string", hashtable_get_string (hashtable, "type_values"));
+ STRCMP_EQUAL("weechat,light,fast,extensible,chat,client",
+ hashtable_get_string (hashtable, "keys"));
+ STRCMP_EQUAL("chat,client,extensible,fast,light,weechat",
+ hashtable_get_string (hashtable, "keys_sorted"));
+ STRCMP_EQUAL("the first item,item2,item3,item4,item5,last item",
+ hashtable_get_string (hashtable, "values"));
+ STRCMP_EQUAL("weechat:the first item,light:item2,fast:item3,"
+ "extensible:item4,chat:item5,client:last item",
+ hashtable_get_string (hashtable, "keys_values"));
+ STRCMP_EQUAL("chat:item5,client:last item,extensible:item4,fast:item3,"
+ "light:item2,weechat:the first item",
+ hashtable_get_string (hashtable, "keys_values_sorted"));
+
+ hashtable_free (hashtable);
+}
+
+/*
+ * Test callback freeing key (it does nothing).
+ */
+
+void
+test_hashtable_free_key (struct t_hashtable *hashtable, void *key)
+{
+ /* make C++ compiler happy */
+ (void) hashtable;
+ (void) key;
+}
+
+/*
+ * Test callback freeing value (it does nothing).
+ */
+
+void
+test_hashtable_free_value (struct t_hashtable *hashtable,
+ const void *key, void *value)
+{
+ /* make C++ compiler happy */
+ (void) hashtable;
+ (void) key;
+ (void) value;
+}
+
+/*
+ * Tests functions:
* hashtable_set_pointer
*/
-TEST(CoreHashtable, Properties)
+TEST(CoreHashtable, SetPointer)
{
- /* TODO: write tests */
+ struct t_hashtable *hashtable;
+
+ hashtable = get_weechat_hashtable ();
+
+ hashtable_set_pointer (NULL, NULL, NULL);
+ hashtable_set_pointer (hashtable, NULL, NULL);
+ hashtable_set_pointer (hashtable, "", NULL);
+ hashtable_set_pointer (hashtable, "unknown", NULL);
+
+ hashtable_set_pointer (hashtable,
+ "callback_free_key", (void *)&test_hashtable_free_key);
+ POINTERS_EQUAL(&test_hashtable_free_key, hashtable->callback_free_key);
+ hashtable_set_pointer (hashtable,
+ "callback_free_value", (void *)&test_hashtable_free_value);
+ POINTERS_EQUAL(&test_hashtable_free_value, hashtable->callback_free_value);
+
+ hashtable_free (hashtable);
}
/*
@@ -369,7 +726,36 @@ TEST(CoreHashtable, Properties)
TEST(CoreHashtable, Infolist)
{
- /* TODO: write tests */
+ struct t_hashtable *hashtable;
+ struct t_infolist *infolist;
+ struct t_infolist_item *infolist_item;
+
+ hashtable = get_weechat_hashtable ();
+
+ infolist = infolist_new (NULL);
+ infolist_item = infolist_new_item (infolist);
+
+ LONGS_EQUAL(0, hashtable_add_to_infolist (NULL, NULL, NULL));
+ LONGS_EQUAL(0, hashtable_add_to_infolist (hashtable, NULL, NULL));
+ LONGS_EQUAL(0, hashtable_add_to_infolist (hashtable, infolist_item, NULL));
+
+ LONGS_EQUAL(1, hashtable_add_to_infolist (hashtable, infolist_item, "test"));
+
+ infolist_reset_item_cursor (infolist);
+ infolist_next (infolist);
+
+ STRCMP_EQUAL("weechat", infolist_string (infolist, "test_name_00000"));
+ STRCMP_EQUAL("the first item", infolist_string (infolist, "test_value_00000"));
+ STRCMP_EQUAL("light", infolist_string (infolist, "test_name_00001"));
+ STRCMP_EQUAL("item2", infolist_string (infolist, "test_value_00001"));
+ STRCMP_EQUAL("fast", infolist_string (infolist, "test_name_00002"));
+ STRCMP_EQUAL("item3", infolist_string (infolist, "test_value_00002"));
+ STRCMP_EQUAL("extensible", infolist_string (infolist, "test_name_00003"));
+ STRCMP_EQUAL("item4", infolist_string (infolist, "test_value_00003"));
+ STRCMP_EQUAL("chat", infolist_string (infolist, "test_name_00004"));
+ STRCMP_EQUAL("item5", infolist_string (infolist, "test_value_00004"));
+ STRCMP_EQUAL("client", infolist_string (infolist, "test_name_00005"));
+ STRCMP_EQUAL("last item", infolist_string (infolist, "test_value_00005"));
}
/*