From 3791869c575887c5052f3a6ee650218a4bc3ca32 Mon Sep 17 00:00:00 2001 From: Pavel Březina Date: Thu, 8 Aug 2013 11:54:05 +0200 Subject: Add macros to manipulate with single linked list --- Makefile.am | 13 ++++- src/tests/slinklist-tests.c | 119 ++++++++++++++++++++++++++++++++++++++++++++ src/util/slinklist.h | 52 +++++++++++++++++++ 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 src/tests/slinklist-tests.c create mode 100644 src/util/slinklist.h diff --git a/Makefile.am b/Makefile.am index 4dd8ed35..d81b7507 100644 --- a/Makefile.am +++ b/Makefile.am @@ -132,7 +132,8 @@ if HAVE_CHECK debug-tests \ ipa_hbac-tests \ sss_idmap-tests \ - responder_socket_access-tests + responder_socket_access-tests \ + slinklist-tests if BUILD_SSH non_interactive_check_based_tests += sysdb_ssh-tests @@ -403,6 +404,7 @@ dist_noinst_HEADERS = \ src/util/auth_utils.h \ src/util/authtok.h \ src/util/util_safealign.h \ + src/util/slinklist.h \ src/monitor/monitor.h \ src/monitor/monitor_interfaces.h \ src/responder/common/responder.h \ @@ -1203,6 +1205,15 @@ krb5_child_test_LDADD = \ $(CHECK_LIBS) \ $(SSSD_INTERNAL_LTLIBS) \ libsss_test_common.la + +slinklist_tests_SOURCES = \ + src/tests/slinklist-tests.c +slinklist_tests_CFLAGS = \ + $(AM_CFLAGS) \ + $(CHECK_CFLAGS) +slinklist_tests_LDADD = \ + $(CHECK_LIBS) \ + libsss_test_common.la if HAVE_CMOCKA 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 + + 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 . +*/ + +#include +#include +#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; +} diff --git a/src/util/slinklist.h b/src/util/slinklist.h new file mode 100644 index 00000000..eae74433 --- /dev/null +++ b/src/util/slinklist.h @@ -0,0 +1,52 @@ +/* + Authors: + Pavel Březina + + Based on dlinklist.h + + 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 . +*/ + +#ifndef _SLINKLIST_H +#define _SLINKLIST_H + +/* hook into the end of the list - needs a tmp pointer */ +#define SLIST_ADD_END(list, p, type) \ +do { \ + if (!(list)) { \ + (list) = (p); \ + } else { \ + type *tmp; \ + for (tmp = (list); tmp->next; tmp = tmp->next) ; \ + tmp->next = (p); \ + } \ + (p)->next = NULL; \ +} while (0) + +/* concatenate two lists - putting all elements of the 2nd list at the + end of the first list */ +#define SLIST_CONCATENATE(list1, list2, type) \ +do { \ + if (!(list1)) { \ + (list1) = (list2); \ + } else { \ + type *tmp; \ + for (tmp = (list1); tmp->next; tmp = tmp->next) ; \ + tmp->next = (list2); \ + } \ +} while (0) + +#endif /* _SLINKLIST_H */ -- cgit