summaryrefslogtreecommitdiffstats
path: root/src/tests/slinklist-tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/slinklist-tests.c')
-rw-r--r--src/tests/slinklist-tests.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/tests/slinklist-tests.c b/src/tests/slinklist-tests.c
new file mode 100644
index 00000000..4cbd90aa
--- /dev/null
+++ b/src/tests/slinklist-tests.c
@@ -0,0 +1,119 @@
+/*
+ Authors:
+ Pavel Březina <pbrezina@redhat.com>
+
+ Copyright (C) 2013 Red Hat
+
+ 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 3 of the License, or
+ (at your option) any later version.
+
+ 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.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <check.h>
+#include <stdlib.h>
+#include "tests/common_check.h"
+#include "util/slinklist.h"
+
+struct slinklist {
+ int id;
+ struct slinklist *next;
+};
+
+START_TEST(test_slinklist_add_end)
+{
+ struct slinklist nodes[] = {{0, NULL}, {1, NULL}, {2, NULL}, {3, NULL}};
+ struct slinklist *list = NULL;
+ struct slinklist *node = NULL;
+ int len = sizeof(nodes) / sizeof(struct slinklist);
+ int i;
+
+ for (i = 0; i != len; i++) {
+ SLIST_ADD_END(list, &nodes[i], struct slinklist);
+ }
+
+ for (i = 0, node = list;
+ i != len && node != NULL;
+ i++, node = node->next) {
+ fail_if(node->id != i, "Expected %d, got %d", i, node->id);
+ }
+
+ fail_if(i != len);
+ fail_if(node != NULL);
+}
+END_TEST
+
+START_TEST(test_slinklist_concatenate)
+{
+ struct slinklist nodes_a[] = {{0, NULL}, {1, NULL}};
+ struct slinklist nodes_b[] = {{2, NULL}, {3, NULL}};
+ struct slinklist *node = NULL;
+ struct slinklist *list = NULL;
+ struct slinklist *list_a = NULL;
+ struct slinklist *list_b = NULL;
+ int len = (sizeof(nodes_a) + sizeof(nodes_b)) / sizeof(struct slinklist);
+ int i;
+
+ list_a = &nodes_a[0];
+ list_a->next = &nodes_a[1];
+
+ list_b = &nodes_b[0];
+ list_b->next = &nodes_b[1];
+
+ SLIST_CONCATENATE(list, list_a, struct slinklist);
+ fail_if(list != list_a);
+
+ SLIST_CONCATENATE(list, list_b, struct slinklist);
+
+ for (i = 0, node = list;
+ i != len && node != NULL;
+ i++, node = node->next) {
+ fail_if(node->id != i, "Expected %d, got %d", i, node->id);
+ }
+
+ fail_if(i != len);
+ fail_if(node != NULL);
+}
+END_TEST
+
+Suite *slinklist_suite(void)
+{
+ Suite *s = suite_create("slinklist");
+
+ TCase *tc_slinklist = tcase_create("slinklist");
+
+ tcase_add_test(tc_slinklist, test_slinklist_add_end);
+ tcase_add_test(tc_slinklist, test_slinklist_concatenate);
+ tcase_set_timeout(tc_slinklist, 60);
+
+ suite_add_tcase(s, tc_slinklist);
+
+ return s;
+}
+
+int main(int argc, const char *argv[])
+{
+ int number_failed;
+
+ tests_set_cwd();
+
+ Suite *s = slinklist_suite();
+ SRunner *sr = srunner_create(s);
+
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+
+ if (number_failed == 0)
+ return EXIT_SUCCESS;
+
+ return EXIT_FAILURE;
+}