struct llist *get_ingroup_leaves(struct rooted_tree *tree, struct llist *excluded_labels) { struct llist *result = create_llist(); if (NULL == result) { perror(NULL); exit(EXIT_FAILURE); } struct list_elem *el; /* add nodes to result iff i) node is a leaf, ii) node's label is not * among 'excluded_labels' */ for (el = tree->nodes_in_order->head; NULL != el; el = el->next) { struct rnode *current = (struct rnode *) el->data; if (is_leaf(current)) { /* Can't use llist_index_of(), because it compares the * addresses of the 'data' members of elements. Instead * we must check string equality, which is why we use * llist_index_of_f(), and pass it string_eq(). */ if (llist_index_of_f(excluded_labels, string_eq, current->label) == -1) if (! append_element(result, current)) { perror(NULL); exit(EXIT_FAILURE); } } } return result; }
int test_llist_index_of_f() { const char *test_name = "test_index_of_f"; struct llist *list1; char *s; list1 = create_llist(); append_element(list1, "yksi"); append_element(list1, "kaksi"); append_element(list1, "kolme"); append_element(list1, "neljä"); append_element(list1, "viisi"); append_element(list1, "kuusi"); append_element(list1, "seitsemän"); append_element(list1, "kahdeksan"); append_element(list1, "yhdeksän"); append_element(list1, "kymmenen"); s = malloc(10 * sizeof(char)); if (NULL == s) { perror(NULL); exit(EXIT_FAILURE); } strcpy(s, "yksi"); if (0 != llist_index_of_f(list1, string_eql, s)) { printf ("%s: expected index 0 for 'yksi', got %d.\n", test_name, llist_index_of(list1, "yksi")); return 1; } strcpy(s, "kuusi"); if (5 != llist_index_of_f(list1, string_eql, s)) { printf ("%s: expected index 9 for 'kymmenen', got %d.\n", test_name, llist_index_of(list1, "yksi")); return 1; } strcpy(s, "taseot"); if (-1 != llist_index_of_f(list1, string_eql, s)) { printf ("%s: expected index -1 (not found) for 'roku', got %d.\n", test_name, llist_index_of(list1, "roku")); return 1; } printf("%s ok.\n", test_name); return 0; }