Add basic tests for linked_list_t
authorTobias Brunner <tobias@strongswan.org>
Tue, 26 Mar 2013 11:18:44 +0000 (12:18 +0100)
committerTobias Brunner <tobias@strongswan.org>
Tue, 11 Jun 2013 09:03:10 +0000 (11:03 +0200)
src/libstrongswan/tests/Makefile.am
src/libstrongswan/tests/test_linked_list.c [new file with mode: 0644]
src/libstrongswan/tests/test_runner.c
src/libstrongswan/tests/test_runner.h

index b16180b..bf0f02a 100644 (file)
@@ -4,7 +4,7 @@ check_PROGRAMS = $(TESTS)
 
 test_runner_SOURCES = \
   test_runner.c test_runner.h \
-  test_enumerator.c
+  test_linked_list.c test_enumerator.c
 
 
 test_runner_CFLAGS = \
diff --git a/src/libstrongswan/tests/test_linked_list.c b/src/libstrongswan/tests/test_linked_list.c
new file mode 100644 (file)
index 0000000..d2f6147
--- /dev/null
@@ -0,0 +1,285 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include <check.h>
+
+#include <collections/linked_list.h>
+
+/*******************************************************************************
+ * test fixture
+ */
+
+static linked_list_t *list;
+
+static void setup_list()
+{
+       void *x = NULL;
+
+       list = linked_list_create();
+       ck_assert_int_eq(list->get_count(list), 0);
+       ck_assert(list->get_first(list, &x) == NOT_FOUND);
+       ck_assert(list->get_last(list, &x) == NOT_FOUND);
+}
+
+static void teardown_list()
+{
+       list->destroy(list);
+}
+
+/*******************************************************************************
+ * insert first/last
+ */
+
+START_TEST(test_insert_first)
+{
+       void *a = (void*)1, *b = (void*)2, *x = NULL;
+
+       list->insert_first(list, a);
+       ck_assert_int_eq(list->get_count(list), 1);
+       ck_assert(list->get_first(list, &x) == SUCCESS);
+       ck_assert(x == a);
+       ck_assert(list->get_last(list, &x) == SUCCESS);
+       ck_assert(x == a);
+
+       list->insert_first(list, b);
+       ck_assert_int_eq(list->get_count(list), 2);
+       ck_assert(list->get_first(list, &x) == SUCCESS);
+       ck_assert(x == b);
+       ck_assert(list->get_last(list, &x) == SUCCESS);
+       ck_assert(x == a);
+}
+END_TEST
+
+START_TEST(test_insert_last)
+{
+       void *a = (void*)1, *b = (void*)2, *x = NULL;
+
+       list->insert_last(list, a);
+       ck_assert_int_eq(list->get_count(list), 1);
+       ck_assert(list->get_first(list, &x) == SUCCESS);
+       ck_assert(x == a);
+       ck_assert(list->get_last(list, &x) == SUCCESS);
+       ck_assert(x == a);
+
+       list->insert_last(list, b);
+       ck_assert_int_eq(list->get_count(list), 2);
+       ck_assert(list->get_first(list, &x) == SUCCESS);
+       ck_assert(x == a);
+       ck_assert(list->get_last(list, &x) == SUCCESS);
+       ck_assert(x == b);
+}
+END_TEST
+
+/*******************************************************************************
+ * remove first/last
+ */
+
+START_TEST(test_remove_first)
+{
+       void *a = (void*)1, *b = (void*)2, *x = NULL;
+
+       list->insert_first(list, a);
+       list->insert_first(list, b);
+       ck_assert(list->remove_first(list, &x) == SUCCESS);
+       ck_assert_int_eq(list->get_count(list), 1);
+       ck_assert(x == b);
+       ck_assert(list->remove_first(list, &x) == SUCCESS);
+       ck_assert_int_eq(list->get_count(list), 0);
+       ck_assert(x == a);
+       ck_assert(list->remove_first(list, &x) == NOT_FOUND);
+       ck_assert(list->remove_last(list, &x) == NOT_FOUND);
+}
+END_TEST
+
+START_TEST(test_remove_last)
+{
+       void *a = (void*)1, *b = (void*)2, *x = NULL;
+
+       list->insert_first(list, a);
+       list->insert_first(list, b);
+       ck_assert(list->remove_last(list, &x) == SUCCESS);
+       ck_assert_int_eq(list->get_count(list), 1);
+       ck_assert(x == a);
+       ck_assert(list->remove_last(list, &x) == SUCCESS);
+       ck_assert_int_eq(list->get_count(list), 0);
+       ck_assert(x == b);
+       ck_assert(list->remove_first(list, &x) == NOT_FOUND);
+       ck_assert(list->remove_last(list, &x) == NOT_FOUND);
+}
+END_TEST
+
+/*******************************************************************************
+ * helper function for remove and find tests
+ */
+
+static bool match_a(void *item, void *a)
+{
+       ck_assert(a == (void*)1);
+       return item == a;
+}
+
+static bool match_b(void *item, void *b)
+{
+       ck_assert(b == (void*)2);
+       return item == b;
+}
+
+/*******************************************************************************
+ * remove
+ */
+
+START_TEST(test_remove)
+{
+       void *a = (void*)1, *b = (void*)2;
+
+       list->insert_first(list, a);
+       ck_assert(list->remove(list, a, NULL) == 1);
+       ck_assert_int_eq(list->get_count(list), 0);
+
+       list->insert_last(list, a);
+       list->insert_last(list, a);
+       list->insert_last(list, a);
+       list->insert_last(list, b);
+       ck_assert(list->remove(list, a, NULL) == 3);
+       ck_assert(list->remove(list, a, NULL) == 0);
+       ck_assert_int_eq(list->get_count(list), 1);
+       ck_assert(list->remove(list, b, NULL) == 1);
+       ck_assert(list->remove(list, b, NULL) == 0);
+}
+END_TEST
+
+START_TEST(test_remove_callback)
+{
+       void *a = (void*)1, *b = (void*)2;
+
+       list->insert_last(list, a);
+       list->insert_last(list, b);
+       list->insert_last(list, a);
+       list->insert_last(list, b);
+       ck_assert(list->remove(list, a, match_a) == 2);
+       ck_assert(list->remove(list, a, match_a) == 0);
+       ck_assert_int_eq(list->get_count(list), 2);
+       ck_assert(list->remove(list, b, match_b) == 2);
+       ck_assert(list->remove(list, b, match_b) == 0);
+       ck_assert_int_eq(list->get_count(list), 0);
+}
+END_TEST
+
+/*******************************************************************************
+ * find
+ */
+
+static bool match_a_b(void *item, void *a, void *b)
+{
+       ck_assert(a == (void*)1);
+       ck_assert(b == (void*)2);
+       return item == a || item == b;
+}
+
+START_TEST(test_find)
+{
+       void *a = (void*)1, *b = (void*)2;
+
+       ck_assert(list->find_first(list, NULL, &a) == NOT_FOUND);
+       ck_assert(list->find_last(list, NULL, &a) == NOT_FOUND);
+       list->insert_last(list, a);
+       ck_assert(list->find_first(list, NULL, &a) == SUCCESS);
+       ck_assert(list->find_first(list, NULL, &b) == NOT_FOUND);
+       ck_assert(list->find_last(list, NULL, &a) == SUCCESS);
+       ck_assert(list->find_last(list, NULL, &b) == NOT_FOUND);
+       list->insert_last(list, b);
+       ck_assert(list->find_first(list, NULL, &a) == SUCCESS);
+       ck_assert(list->find_first(list, NULL, &b) == SUCCESS);
+       ck_assert(list->find_last(list, NULL, &a) == SUCCESS);
+       ck_assert(list->find_last(list, NULL, &b) == SUCCESS);
+
+       ck_assert(list->find_first(list, NULL, NULL) == NOT_FOUND);
+       ck_assert(list->find_last(list, NULL, NULL) == NOT_FOUND);
+}
+END_TEST
+
+START_TEST(test_find_callback)
+{
+       void *a = (void*)1, *b = (void*)2, *x = NULL;
+
+       ck_assert(list->find_first(list, (linked_list_match_t)match_a_b, &x, a, b) == NOT_FOUND);
+       list->insert_last(list, a);
+       ck_assert(list->find_first(list, (linked_list_match_t)match_a, NULL, a) == SUCCESS);
+       x = NULL;
+       ck_assert(list->find_first(list, (linked_list_match_t)match_a, &x, a) == SUCCESS);
+       ck_assert(a == x);
+       ck_assert(list->find_first(list, (linked_list_match_t)match_b, &x, b) == NOT_FOUND);
+       ck_assert(a == x);
+       x = NULL;
+       ck_assert(list->find_first(list, (linked_list_match_t)match_a_b, &x, a, b) == SUCCESS);
+       ck_assert(a == x);
+
+       ck_assert(list->find_last(list, (linked_list_match_t)match_a, NULL, a) == SUCCESS);
+       x = NULL;
+       ck_assert(list->find_last(list, (linked_list_match_t)match_a, &x, a) == SUCCESS);
+       ck_assert(a == x);
+       ck_assert(list->find_last(list, (linked_list_match_t)match_b, &x, b) == NOT_FOUND);
+       ck_assert(a == x);
+       x = NULL;
+       ck_assert(list->find_last(list, (linked_list_match_t)match_a_b, &x, a, b) == SUCCESS);
+       ck_assert(a == x);
+
+       list->insert_last(list, b);
+       ck_assert(list->find_first(list, (linked_list_match_t)match_a, &x, a) == SUCCESS);
+       ck_assert(a == x);
+       ck_assert(list->find_first(list, (linked_list_match_t)match_b, &x, b) == SUCCESS);
+       ck_assert(b == x);
+       ck_assert(list->find_last(list, (linked_list_match_t)match_a, &x, a) == SUCCESS);
+       ck_assert(a == x);
+       ck_assert(list->find_last(list, (linked_list_match_t)match_b, &x, b) == SUCCESS);
+       ck_assert(b == x);
+       x = NULL;
+       ck_assert(list->find_first(list, (linked_list_match_t)match_a_b, &x, a, b) == SUCCESS);
+       ck_assert(a == x);
+       x = NULL;
+       ck_assert(list->find_last(list, (linked_list_match_t)match_a_b, &x, a, b) == SUCCESS);
+       ck_assert(b == x);
+}
+END_TEST
+
+Suite *linked_list_suite_create()
+{
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("linked list");
+
+       tc = tcase_create("insert/get");
+       tcase_add_checked_fixture(tc, setup_list, teardown_list);
+       tcase_add_test(tc, test_insert_first);
+       tcase_add_test(tc, test_insert_last);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("remove");
+       tcase_add_checked_fixture(tc, setup_list, teardown_list);
+       tcase_add_test(tc, test_remove_first);
+       tcase_add_test(tc, test_remove_last);
+       tcase_add_test(tc, test_remove);
+       tcase_add_test(tc, test_remove_callback);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("find");
+       tcase_add_checked_fixture(tc, setup_list, teardown_list);
+       tcase_add_test(tc, test_find);
+       tcase_add_test(tc, test_find_callback);
+       suite_add_tcase(s, tc);
+
+       return s;
+}
index e097357..9baad46 100644 (file)
@@ -33,6 +33,7 @@ int main()
 
        sr = srunner_create(NULL);
        srunner_add_suite(sr, enumerator_suite_create());
+       srunner_add_suite(sr, linked_list_suite_create());
 
        srunner_run_all(sr, CK_NORMAL);
        nf = srunner_ntests_failed(sr);
index 1bf9a5f..9c0aba4 100644 (file)
@@ -19,5 +19,6 @@
 #include <check.h>
 
 Suite *enumerator_suite_create();
+Suite *linked_list_suite_create();
 
 #endif /** TEST_RUNNER_H_ */