summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2015-02-13 08:00:09 +0100
committerSébastien Helleu <flashcode@flashtux.org>2015-02-13 08:00:09 +0100
commit07c7ecb6797eddaca3bd834520e112248743feb9 (patch)
treefc753e8217e99b57d9baf04de7cb3666997ade8c /tests
parent094abc57ed53e550d8aaca15ceec09d3e5f0928f (diff)
downloadweechat-07c7ecb6797eddaca3bd834520e112248743feb9.zip
tests: add tests on weelist functions
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/core/test-list.cpp290
1 files changed, 282 insertions, 8 deletions
diff --git a/tests/unit/core/test-list.cpp b/tests/unit/core/test-list.cpp
index edcea4a2d..2f067306b 100644
--- a/tests/unit/core/test-list.cpp
+++ b/tests/unit/core/test-list.cpp
@@ -24,30 +24,117 @@
extern "C"
{
#include "src/core/wee-list.h"
+#include "src/plugins/plugin.h"
}
+#define LIST_VALUE_TEST "test"
+#define LIST_VALUE_XYZ "xyz"
+#define LIST_VALUE_ZZZ "zzz"
+
TEST_GROUP(List)
{
};
/*
+ * Creates a list for tests.
+ */
+
+struct t_weelist *
+test_list_new ()
+{
+ struct t_weelist *list;
+
+ list = weelist_new ();
+
+ weelist_add (list, LIST_VALUE_ZZZ, WEECHAT_LIST_POS_END, NULL);
+ weelist_add (list, LIST_VALUE_TEST, WEECHAT_LIST_POS_BEGINNING, NULL);
+ weelist_add (list, LIST_VALUE_XYZ, WEECHAT_LIST_POS_SORT, NULL);
+
+ return list;
+}
+
+/*
* Tests functions:
* weelist_new
*/
TEST(List, New)
{
- /* TODO: write tests */
+ struct t_weelist *list;
+
+ list = weelist_new();
+ CHECK(list);
+
+ /* check initial values */
+ POINTERS_EQUAL(NULL, list->items);
+ POINTERS_EQUAL(NULL, list->last_item);
+ LONGS_EQUAL(0, list->size);
+
+ /* free list */
+ weelist_free (list);
}
/*
* Tests functions:
* weelist_add
+ * weelist_free
*/
TEST(List, Add)
{
- /* TODO: write tests */
+ struct t_weelist *list;
+ struct t_weelist_item *item1, *item2, *item3;
+ const char *str_user_data = "some user data";
+
+ list = weelist_new();
+
+ POINTERS_EQUAL(NULL, weelist_add (NULL, NULL, NULL, NULL));
+ POINTERS_EQUAL(NULL, weelist_add (list, NULL, NULL, NULL));
+ POINTERS_EQUAL(NULL, weelist_add (NULL, LIST_VALUE_TEST, NULL, NULL));
+ POINTERS_EQUAL(NULL, weelist_add (NULL, NULL, WEECHAT_LIST_POS_END, NULL));
+ POINTERS_EQUAL(NULL, weelist_add (list, LIST_VALUE_TEST, NULL, NULL));
+ POINTERS_EQUAL(NULL, weelist_add (list, NULL, WEECHAT_LIST_POS_END, NULL));
+ POINTERS_EQUAL(NULL, weelist_add (NULL, LIST_VALUE_TEST,
+ WEECHAT_LIST_POS_END, NULL));
+
+ /* add an element at the end */
+ item1 = weelist_add (list, LIST_VALUE_ZZZ, WEECHAT_LIST_POS_END,
+ (void *)str_user_data);
+ CHECK(item1);
+ CHECK(item1->data);
+ CHECK(item1->user_data);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, item1->data);
+ POINTERS_EQUAL(str_user_data, item1->user_data);
+ LONGS_EQUAL(1, list->size); /* list is now: ["zzz"] */
+ POINTERS_EQUAL(item1, list->items);
+
+ /* add an element at the beginning */
+ item2 = weelist_add (list, LIST_VALUE_TEST, WEECHAT_LIST_POS_BEGINNING,
+ (void *)str_user_data);
+ CHECK(item2);
+ CHECK(item2->data);
+ CHECK(item2->user_data);
+ STRCMP_EQUAL(LIST_VALUE_TEST, item2->data);
+ POINTERS_EQUAL(str_user_data, item2->user_data);
+ LONGS_EQUAL(2, list->size); /* list is now: ["test", "zzz"] */
+ POINTERS_EQUAL(item2, list->items);
+ POINTERS_EQUAL(item1, list->items->next_item);
+
+ /* add an element, using sort */
+ item3 = weelist_add (list, LIST_VALUE_XYZ, WEECHAT_LIST_POS_SORT,
+ (void *)str_user_data);
+ CHECK(item3);
+ CHECK(item3->data);
+ CHECK(item3->user_data);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, item3->data);
+ POINTERS_EQUAL(str_user_data, item3->user_data);
+ LONGS_EQUAL(3, list->size); /* list is now: ["test", "xyz", "zzz"] */
+ POINTERS_EQUAL(item2, list->items);
+ POINTERS_EQUAL(item3, list->items->next_item);
+ POINTERS_EQUAL(item1, list->items->next_item->next_item);
+
+ /* free list */
+ weelist_free (list);
}
/*
@@ -60,7 +147,84 @@ TEST(List, Add)
TEST(List, Search)
{
- /* TODO: write tests */
+ struct t_weelist *list;
+ struct t_weelist_item *ptr_item;
+
+ list = test_list_new ();
+
+ /* search an element */
+
+ POINTERS_EQUAL(NULL, weelist_search (NULL, NULL));
+ POINTERS_EQUAL(NULL, weelist_search (list, NULL));
+ POINTERS_EQUAL(NULL, weelist_search (NULL, LIST_VALUE_TEST));
+
+ POINTERS_EQUAL(NULL, weelist_search (list, "not found"));
+ POINTERS_EQUAL(NULL, weelist_search (list, "TEST"));
+
+ ptr_item = weelist_search (list, LIST_VALUE_TEST);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_TEST, ptr_item->data);
+
+ ptr_item = weelist_search (list, LIST_VALUE_XYZ);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, ptr_item->data);
+
+ ptr_item = weelist_search (list, LIST_VALUE_ZZZ);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, ptr_item->data);
+
+ /* search the position of an element */
+
+ LONGS_EQUAL(-1, weelist_search_pos (NULL, NULL));
+ LONGS_EQUAL(-1, weelist_search_pos (list, NULL));
+ LONGS_EQUAL(-1, weelist_search_pos (NULL, LIST_VALUE_TEST));
+
+ LONGS_EQUAL(-1, weelist_search_pos (list, "not found"));
+ LONGS_EQUAL(-1, weelist_search_pos (list, "TEST"));
+
+ LONGS_EQUAL(0, weelist_search_pos (list, LIST_VALUE_TEST));
+ LONGS_EQUAL(1, weelist_search_pos (list, LIST_VALUE_XYZ));
+ LONGS_EQUAL(2, weelist_search_pos (list, LIST_VALUE_ZZZ));
+
+ /* case-insensitive search of an element */
+
+ POINTERS_EQUAL(NULL, weelist_casesearch (NULL, NULL));
+ POINTERS_EQUAL(NULL, weelist_casesearch (list, NULL));
+ POINTERS_EQUAL(NULL, weelist_casesearch (NULL, LIST_VALUE_TEST));
+
+ POINTERS_EQUAL(NULL, weelist_casesearch (list, "not found"));
+
+ ptr_item = weelist_casesearch (list, "TEST");
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_TEST, ptr_item->data);
+
+ ptr_item = weelist_casesearch (list, LIST_VALUE_TEST);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_TEST, ptr_item->data);
+
+ ptr_item = weelist_casesearch (list, LIST_VALUE_XYZ);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, ptr_item->data);
+
+ ptr_item = weelist_casesearch (list, LIST_VALUE_ZZZ);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, ptr_item->data);
+
+ /* case-insensitive search of an element position */
+
+ LONGS_EQUAL(-1, weelist_casesearch_pos (NULL, NULL));
+ LONGS_EQUAL(-1, weelist_casesearch_pos (list, NULL));
+ LONGS_EQUAL(-1, weelist_casesearch_pos (NULL, LIST_VALUE_TEST));
+
+ LONGS_EQUAL(-1, weelist_casesearch_pos (list, "not found"));
+
+ LONGS_EQUAL(0, weelist_casesearch_pos (list, "TEST"));
+ LONGS_EQUAL(0, weelist_casesearch_pos (list, LIST_VALUE_TEST));
+ LONGS_EQUAL(1, weelist_casesearch_pos (list, LIST_VALUE_XYZ));
+ LONGS_EQUAL(2, weelist_casesearch_pos (list, LIST_VALUE_ZZZ));
+
+ /* free list */
+ weelist_free (list);
}
/*
@@ -71,7 +235,46 @@ TEST(List, Search)
TEST(List, Get)
{
- /* TODO: write tests */
+ struct t_weelist *list;
+ struct t_weelist_item *ptr_item;
+
+ list = test_list_new ();
+
+ /* get an element by position */
+
+ POINTERS_EQUAL(NULL, weelist_get (NULL, -1));
+ POINTERS_EQUAL(NULL, weelist_get (list, -1));
+ POINTERS_EQUAL(NULL, weelist_get (NULL, 0));
+
+ POINTERS_EQUAL(NULL, weelist_get (list, 50));
+
+ ptr_item = weelist_get (list, 0);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_TEST, ptr_item->data);
+
+ ptr_item = weelist_get (list, 1);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, ptr_item->data);
+
+ ptr_item = weelist_get (list, 2);
+ CHECK(ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, ptr_item->data);
+
+ /* get string value of an element */
+
+ POINTERS_EQUAL(NULL, weelist_string (NULL));
+
+ ptr_item = weelist_get(list, 0);
+ STRCMP_EQUAL(LIST_VALUE_TEST, weelist_string (ptr_item));
+
+ ptr_item = weelist_get(list, 1);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, weelist_string (ptr_item));
+
+ ptr_item = weelist_get(list, 2);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, weelist_string (ptr_item));
+
+ /* free list */
+ weelist_free (list);
}
/*
@@ -81,7 +284,24 @@ TEST(List, Get)
TEST(List, Set)
{
- /* TODO: write tests */
+ struct t_weelist *list;
+ struct t_weelist_item *ptr_item;
+ const char *another_test = "another test";
+
+ list = test_list_new ();
+
+ ptr_item = weelist_get (list, 0);
+ STRCMP_EQUAL(LIST_VALUE_TEST, ptr_item->data);
+
+ weelist_set (NULL, NULL);
+ weelist_set (ptr_item, NULL);
+ weelist_set (NULL, another_test);
+
+ weelist_set (ptr_item, another_test);
+ STRCMP_EQUAL(another_test, ptr_item->data);
+
+ /* free list */
+ weelist_free (list);
}
/*
@@ -92,19 +312,73 @@ TEST(List, Set)
TEST(List, Move)
{
- /* TODO: write tests */
+ struct t_weelist *list;
+ struct t_weelist_item *ptr_item;
+
+ list = test_list_new ();
+
+ /* get next item */
+
+ ptr_item = weelist_get (list, 0);
+ STRCMP_EQUAL(LIST_VALUE_TEST, ptr_item->data);
+ ptr_item = weelist_next (ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, ptr_item->data);
+ ptr_item = weelist_next (ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, ptr_item->data);
+ ptr_item = weelist_next (ptr_item);
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ /* get previous item */
+
+ ptr_item = weelist_get(list, 2);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, ptr_item->data);
+ ptr_item = weelist_prev (ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, ptr_item->data);
+ ptr_item = weelist_prev (ptr_item);
+ STRCMP_EQUAL(LIST_VALUE_TEST, ptr_item->data);
+
+ /* free list */
+ weelist_free (list);
}
/*
* Tests functions:
* weelist_remove
* weelist_remove_all
- * weelist_free
*/
TEST(List, Free)
{
- /* TODO: write tests */
+ struct t_weelist *list;
+ struct t_weelist_item *ptr_item;
+
+ list = test_list_new ();
+
+ /* remove one element */
+
+ ptr_item = weelist_get(list, 1);
+ STRCMP_EQUAL(LIST_VALUE_XYZ, ptr_item->data);
+
+ weelist_remove (NULL, NULL);
+ weelist_remove (list, NULL);
+ weelist_remove (NULL, ptr_item);
+
+ weelist_remove (list, ptr_item);
+
+ ptr_item = weelist_get(list, 1);
+ STRCMP_EQUAL(LIST_VALUE_ZZZ, ptr_item->data);
+ ptr_item = weelist_get (list, 2);
+ POINTERS_EQUAL(NULL, ptr_item);
+
+ /* remove all elements */
+
+ weelist_remove_all (list);
+ LONGS_EQUAL(0, list->size);
+ POINTERS_EQUAL(NULL, list->items);
+ POINTERS_EQUAL(NULL, list->last_item);
+
+ /* free list */
+ weelist_free (list);
}
/*