int test_create() { char *test_name = "test_create"; struct llist *list_p; list_p = create_llist(); if (NULL == list_p) { printf ("%s: llist should not be NULL.\n", test_name); return 1; } if (NULL != list_p->head) { printf ("%s: list head should be NULL.\n", test_name); return 1; } if (NULL != list_p->tail) { printf ("%s: list tail should be NULL.\n", test_name); return 1; } if (0 != list_p->count) { printf ("%s: list count should be 0.\n", test_name); return 1; } printf ("%s ok.\n", test_name); return 0; }
int test_append_element() { char *test_name = "test_append_element"; struct llist *list_p; char *data = "blah"; list_p = create_llist(); append_element(list_p, data); if (NULL == list_p->head) { printf ("%s: head should not be NULL.\n", test_name); return 1; } if (list_p->head != list_p->tail) { printf("%s: tail should be the same as head", test_name); return 1; } if (strcmp(list_p->head->data, data) != 0) { printf ("%s: data should be '%s'.\n", test_name, data); return 1; } if(list_p->count != 1) { printf ("%s: count should be 1.\n", test_name); return 1; } printf ("%s ok.\n", test_name); return 0; }
struct llist * get_outgroup_nodes(struct rooted_tree *tree, struct llist *labels) { struct hash *map; struct llist *outgroup_nodes; struct list_elem *el; map = create_label2node_map(tree->nodes_in_order); outgroup_nodes = create_llist(); if (NULL == outgroup_nodes) { perror(NULL); exit(EXIT_FAILURE); } for (el = labels->head; NULL != el; el = el->next) { struct rnode *desc; desc = hash_get(map, (char *) el->data); if (NULL == desc) { fprintf (stderr, "WARNING: label '%s' does not occur in tree\n", (char *) el->data); } else { if (! append_element(outgroup_nodes, desc)) { perror(NULL); exit(EXIT_FAILURE); } } } destroy_hash(map); return outgroup_nodes; }
int test_clear() { const char *test_name = "test_clear"; const int num_elements = 100000; struct llist *list_p; int i; list_p = create_llist(); for (i = 0; i < num_elements; i++) prepend_element(list_p, &i); if (list_p->count != num_elements) { printf("count wrong: %d, expected %d.\n", list_p->count, num_elements); return 1; } clear_llist(list_p); if (0 != list_p->count ) { printf ("%s: count wrong: expected 0, got %d\n", test_name, list_p->count); return 1; } if (NULL != list_p->head) { printf ("%s: expected NULL head.\n", test_name); return 1; } if (NULL != list_p->tail) { printf ("%s: expected NULL tail.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_add_struct() { const int num_edges = 10; struct llist *list_p; int i; struct test_data *datap; struct list_elem *elem_p; list_p = create_llist(); /* populate list */ for (i = 0; i < num_edges; i++) { if (NULL == (datap = malloc(sizeof(struct test_data)))) { perror(NULL); return 1; } datap->height = (double) 2.5 * i; datap->length = (double) i; snprintf(datap->name, 10, "elem %d", i); prepend_element(list_p, (void *) datap); } /* check elements */ for (elem_p=list_p->head, i=9; NULL != elem_p; elem_p = elem_p->next, i--) { datap = (struct test_data *) elem_p->data; if (datap->length != (double) i) { printf ("Expected edge length %.2f, got %.2f.", (double) i, datap->length); return 1; } } printf("test_add_struct ok.\n"); return 0; }
int test_reduce() { char *test_name = "test_reduce"; struct llist *list_p; int a = 1, b = 2, c = 3, d = 4, e = 5; void * result; list_p = create_llist(); append_element(list_p, &a); append_element(list_p, &b); append_element(list_p, &c); append_element(list_p, &d); append_element(list_p, &e); result = reduce(list_p, sum); if (*((int *)result) != 15) { printf ("%s: expected 15, got %d.\n", test_name, *((int *)result)); return 1; } printf("%s ok.\n", test_name); return 0; }
int main( ) { struct sparse s1, s2 ; system ( "cls" ) ; initsparse ( &s1 ) ; initsparse ( &s2 ) ; create_array ( &s1 ) ; printf ( "Elements in sparse matrix:" ) ; display ( s1 ) ; create_triplet ( &s2, s1 ) ; create_llist ( &s2 ) ; printf ( "Information stored in linked list :" ) ; show_llist ( s2 ) ; delsparse ( &s1 ) ; delsparse ( &s2 ) ; return 0 ; }
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_delete_last() { const char *test_name = __func__; struct llist *list1; struct llist *list2; struct list_elem *el; list1 = create_llist(); append_element(list1, "omega"); list2 = delete_after(list1, -1, 1); /* check list1 */ if (0 != list1->count) { printf ("%s: expected count of 0, got %d.\n", test_name, list1->count); return 1; } if (NULL != list1->head) { printf ("%s: expected NULL head, got '%s'.\n", test_name, (char *) list1->head->data); return 1; } if (NULL != list1->tail) { printf ("%s: expected NULL tail, got '%s'.\n", test_name, (char *) list1->tail->data); return 1; } /* check list2 */ if (1 != list2->count) { printf ("%s: expected count of 1, got %d.\n", test_name, list2->count); return 1; } el = list2->head; if (strcmp("omega", (char *) el->data) != 0) { printf ("%s: expected 'omega' at head, got '%s'.\n", test_name, (char *) el->data); return 1; } el = list2->tail; if (strcmp("omega", (char *) el->data) != 0) { printf ("%s: expected 'omega' at tail, got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (NULL != el) { printf ("%s: expected NULL next, got '%s'.\n", test_name, (char *) el->data); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_reverse() { char *test_name = "test_reverse"; struct llist *list_p, *revlist_p; struct list_elem *elem; list_p = create_llist(); append_element(list_p, "one"); append_element(list_p, "two"); append_element(list_p, "three"); append_element(list_p, "four"); append_element(list_p, "five"); revlist_p = llist_reverse(list_p); elem = revlist_p->head; if (strcmp(elem->data, "five") != 0) { printf ("%s: expected 'five', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "four") != 0) { printf ("%s: expected 'four', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "three") != 0) { printf ("%s: expected 'three', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "two") != 0) { printf ("%s: expected 'two', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "one") != 0) { printf ("%s: expected 'one', got %s.\n", test_name, (char *) elem->data); return 1; } if (NULL != elem->next) { printf ("%s: elem->next shoudl be NULL.\n", test_name); return 1; } if(list_p->count != 5) { printf ("%s: count should be 5.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_to_array() { const char *test_name = "test_to_array"; struct llist *list = create_llist(); append_element(list, "Archaea"); append_element(list, "Bacteria"); append_element(list, "Columbiformes"); append_element(list, "Diptera"); append_element(list, "Eleagnaceae"); append_element(list, "Fagales"); append_element(list, "Gastropoda"); char ** list_array = (char **) llist_to_array(list); if (strcmp(list_array[0], "Archaea")) { printf ("%s: expected 'Archaea' as element 0 (got '%s')\n", test_name, list_array[0]); return 1; } if (strcmp(list_array[1], "Bacteria")) { printf ("%s: expected 'Bacteria' as element 1 (got '%s')\n", test_name, list_array[1]); return 1; } if (strcmp(list_array[2], "Columbiformes")) { printf ("%s: expected 'Columbiformes' as element 2 (got '%s')\n", test_name, list_array[2]); return 1; } if (strcmp(list_array[3], "Diptera")) { printf ("%s: expected 'Diptera' as element 3 (got '%s')\n", test_name, list_array[3]); return 1; } if (strcmp(list_array[4], "Eleagnaceae")) { printf ("%s: expected 'Eleagnaceae' as element 4 (got '%s')\n", test_name, list_array[4]); return 1; } if (strcmp(list_array[5], "Fagales")) { printf ("%s: expected 'Fagales' as element 5 (got '%s')\n", test_name, list_array[5]); return 1; } if (strcmp(list_array[6], "Gastropoda")) { printf ("%s: expected 'Gastropoda' as element 6 (got '%s')\n", test_name, list_array[6]); return 1; } printf("%s ok.\n", test_name); return 0; }
struct llist *array_to_llist(void **array, int count) { struct llist *list = create_llist(); if (NULL == list) return NULL; int i; for (i = 0; i < count; i++) if (! append_element(list, array[i])) return NULL; return list; }
struct llist *shallow_copy(struct llist *orig) { struct llist *copy = create_llist(); if (NULL == copy) return NULL; struct list_elem *elem; for (elem = orig->head; NULL != elem; elem = elem->next) { if (! append_element(copy, elem->data)) return NULL; } return copy; }
struct llist *delete_after(struct llist *target, int pos, int length) { struct llist *result; struct list_elem *elem; int i, n; result = create_llist(); if (NULL == result) return NULL; if (-1 == pos) { elem = target->head; result->head = target->head; for (n = length; n > 1; n--) /* not 0 */ elem = elem->next; result->tail = elem; elem = elem->next; /* new head of target */ target->head = elem; if (result->tail == target->tail) target->tail = NULL; result->tail->next = NULL; /* target's tail does not change */ } else if ((pos >= 0) && (pos < target->count - length -1)) { struct list_elem *last_before_del; elem = target->head; for (i = pos; i > 0; i--) elem = elem->next; last_before_del = elem; elem = elem->next; result->head = elem; for (n = length; n > 1; n--) /* not 0 */ elem = elem->next; result->tail = elem; elem = elem->next; last_before_del->next = elem; result->tail->next = NULL; } else if (pos + length + 1 == target->count) { elem = target->head; result->tail = target->tail; for (i = pos; i > 0; i--) /* not 0 */ elem = elem->next; target->tail = elem; elem = elem->next; target->tail->next = NULL; result->head = elem; } target->count -= length; result->count = length; return result; }
int test_index() { const char *test_name = "test_index"; 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"); if (0 != llist_index_of(list1, "yksi")) { printf ("%s: expected index 0 for 'yksi', got %d.\n", test_name, llist_index_of(list1, "yksi")); return 1; } if (9 != llist_index_of(list1, "kymmenen")) { printf ("%s: expected index 9 for 'kymmenen', got %d.\n", test_name, llist_index_of(list1, "yksi")); return 1; } if (-1 != llist_index_of(list1, "roku")) { printf ("%s: expected index -1 (not found) for 'roku', got %d.\n", test_name, llist_index_of(list1, "roku")); return 1; } /* NOTE: In general, looking for strings will NOT work! This has worked * because we're using constants. But look at this: */ s = malloc(6 * sizeof(char)); if (NULL == s) { perror(NULL); exit(EXIT_FAILURE); } strncpy(s, "kuusi", 6); if (-1 != llist_index_of(list1, s)) { printf ("%s: something very weird happened.\n", test_name); return 1; } /* See test_llist_index_of_f(). */ printf("%s ok.\n", test_name); return 0; }
int main() //@ : main //@ requires emp; //@ ensures emp; { struct llist *l1 = create_llist(); struct llist *l2 = create_llist(); llist_add(l1, 10); llist_add(l1, 20); llist_add(l1, 30); llist_add(l2, 40); llist_add(l2, 50); llist_add(l2, 60); int x = llist_removeFirst(l2); assert(x == 40); llist_append(l1, l2); int n = llist_length(l1); assert(n == 5); int e0 = llist_lookup(l1, 0); assert(e0 == 10); int e1 = llist_lookup(l1, 1); assert(e1 == 20); int e2 = llist_lookup(l1, 2); assert(e2 == 30); int e3 = llist_lookup(l1, 3); assert(e3 == 50); int e4 = llist_lookup(l1, 4); assert(e4 == 60); llist_dispose(l1); return 0; }
struct llist *llist_reverse(struct llist *list) { struct llist *result; struct list_elem *elem; result = create_llist(); if (NULL == result) return NULL; for (elem = list->head; NULL != elem; elem = elem->next) if (! prepend_element(result, elem->data)) return NULL; return result; }
int test_prepend_five() { char *test_name = "test_prepend_five"; struct llist *list_p; struct list_elem *elem; list_p = create_llist(); prepend_element(list_p, "one"); prepend_element(list_p, "two"); prepend_element(list_p, "three"); prepend_element(list_p, "four"); prepend_element(list_p, "five"); /* note that adding to head reverses list */ elem = list_p->head; if (strcmp(elem->data, "five") != 0) { printf ("%s: expected 'five', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "four") != 0) { printf ("%s: expected 'five', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "three") != 0) { printf ("%s: expected 'five', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "two") != 0) { printf ("%s: expected 'five', got %s.\n", test_name, (char *) elem->data); return 1; } elem = elem->next; if (strcmp(elem->data, "one") != 0) { printf ("%s: expected 'five', got %s.\n", test_name, (char *) elem->data); return 1; } if(list_p->count != 5) { printf ("%s: count should be 5.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }
void main0() //@ requires emp; //@ ensures emp; { struct llist *l = create_llist(); llist_add(l, 10); llist_add(l, 20); llist_add(l, 30); llist_add(l, 40); int x1 = llist_removeFirst(l); assert(x1 == 10); int x2 = llist_removeFirst(l); assert(x2 == 20); llist_dispose(l); }
int test_create_label2node_map() { const char *test_name = "test_create_label2node_map"; struct rnode *n1, *n2, *n3; struct llist *node_list; struct hash *map; n1 = create_rnode("n1", ""); n2 = create_rnode("n2", ""); n3 = create_rnode("n3", ""); node_list = create_llist(); append_element(node_list, n1); append_element(node_list, n2); append_element(node_list, n3); map = create_label2node_map(node_list); if (NULL == map) { printf ("%s: map must not be NULL.\n", test_name); return 1; } if (NULL != hash_get(map, "not there")) { printf ("%s: inexistent label should return NULL.\n", test_name); return 1; } if (n1 != hash_get(map, "n1")) { printf ("%s: node with label 'n1' should be %p, not %p.\n", test_name, n1, hash_get(map, "n1")); return 1; } if (n2 != hash_get(map, "n2")) { printf ("%s: node with label 'n2' should be %p, not %p.\n", test_name, n2, hash_get(map, "n2")); return 1; } if (n3 != hash_get(map, "n3")) { printf ("%s: node with label 'n3' should be %p, not %p.\n", test_name, n3, hash_get(map, "n3")); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_add_many() { const int num_elements = 100000; struct llist *list_p; int i; list_p = create_llist(); for (i = 0; i < num_elements; i++) prepend_element(list_p, &i); if (list_p->count != num_elements) { printf("count wrong: %d, expected %d.\n", list_p->count, num_elements); return 1; } printf("test_add_many ok.\n"); return 0; }
int test_destroy() { char *test_name = "test_destroy"; struct llist *list_p; list_p = create_llist(); prepend_element(list_p, "one"); prepend_element(list_p, "two"); prepend_element(list_p, "three"); prepend_element(list_p, "four"); prepend_element(list_p, "five"); destroy_llist(list_p); printf("%s ok.\n", test_name); return 0; }
int test_nodes_from_labels() { const char *test_name = "test_nodes_from_labels"; struct rooted_tree tree = tree_3(); struct llist *labels = create_llist(); append_element(labels, "C"); append_element(labels, "f"); append_element(labels, "D"); append_element(labels, "A"); struct llist *nodes = nodes_from_labels(&tree, labels); struct list_elem *el = nodes->head; if (strcmp(((struct rnode *) el->data)->label, "C") != 0) { printf ("%s: expected label 'C', got '%s'\n", test_name, ((struct rnode *) el->data)->label); return 1; } el = el->next; if (strcmp(((struct rnode *) el->data)->label, "f") != 0) { printf ("%s: expected label 'f', got '%s'\n", test_name, ((struct rnode *) el->data)->label); return 1; } el = el->next; if (strcmp(((struct rnode *) el->data)->label, "D") != 0) { printf ("%s: expected label 'D', got '%s'\n", test_name, ((struct rnode *) el->data)->label); return 1; } el = el->next; if (strcmp(((struct rnode *) el->data)->label, "A") != 0) { printf ("%s: expected label 'A', got '%s'\n", test_name, ((struct rnode *) el->data)->label); return 1; } el = el->next; if (NULL != el) { printf ("%s: nodes list not terminated.\n", test_name); return 1; } printf ("%s: ok.\n", test_name); return 0; }
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 hash *excluded_lbl_hash = create_hash(excluded_labels->count); if (NULL == excluded_lbl_hash) { perror(NULL); exit(EXIT_FAILURE); } struct list_elem *el; /* Make a hash with all excluded labels. */ for (el = excluded_labels->head; NULL != el; el = el->next) { if (! hash_set(excluded_lbl_hash, (char *) el->data, (void *) "member")) { perror(NULL); exit(EXIT_FAILURE); } } /* add nodes to result iff i) node is a leaf, ii) node's label is not * among 'excluded_lbl_hash' */ for (el = tree->nodes_in_order->head; NULL != el; el = el->next) { struct rnode *current = (struct rnode *) el->data; if (is_leaf(current)) { bool add = false; if (strcmp ("", current->label) == 0) add = true; else { if (NULL == hash_get(excluded_lbl_hash, current->label)) add = true; } if (add) { if (! append_element(result, current)) { perror(NULL); exit(EXIT_FAILURE); } } } } destroy_hash(excluded_lbl_hash); 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; }
int test_shift() { char *test_name = "test_shift"; struct llist *list_p; struct list_elem *elem; list_p = create_llist(); append_element(list_p, "one"); append_element(list_p, "two"); append_element(list_p, "three"); append_element(list_p, "four"); append_element(list_p, "five"); elem = list_p->head; if (strcmp(elem->data, "one") != 0) { printf ("%s: expected 'one', got '%s'.\n", test_name, (char *) elem->data); return 1; } if(list_p->count != 5) { printf ("%s: count should be 5.\n", test_name); return 1; } elem = shift(list_p); if (strcmp((char *) elem, "one") != 0) { printf ("%s: expected 'one', got %s.\n", test_name, (char *) elem->data); return 1; } elem = list_p->head; if (strcmp(elem->data, "two") != 0) { printf ("%s: expected 'two', got %s.\n", test_name, (char *) elem->data); return 1; } if(list_p->count != 4) { printf ("%s: count should be 4.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }
int main2() //@ requires emp; //@ ensures emp; { struct llist *l = create_llist(); llist_add(l, 5); llist_add(l, 10); llist_add(l, 15); struct iter *i1 = llist_create_iter(l); struct iter *i2 = llist_create_iter(l); int i1e1 = iter_next(i1); assert(i1e1 == 5); int i2e1 = iter_next(i2); assert(i2e1 == 5); int i1e2 = iter_next(i1); assert(i1e2 == 10); int i2e2 = iter_next(i2); assert(i2e2 == 10); iter_dispose(i1); iter_dispose(i2); llist_dispose(l); return 0; }
int test_create_label2node_list_map() { const char *test_name = "test_create_label2node_list_map"; struct rnode *a1, *a2, *a3, *b1, *b2, *c1, *d1, *d2; struct llist *node_list; struct hash *map; struct llist *nodes_of_label; struct list_elem *el; a1 = create_rnode("a", ""); a2 = create_rnode("a", ""); a3 = create_rnode("a", ""); b1 = create_rnode("b", ""); b2 = create_rnode("b", ""); c1 = create_rnode("c", ""); d1 = create_rnode("d", ""); d2 = create_rnode("d", ""); node_list = create_llist(); /* The order in which the nodes appear is unimportant, but it should be * preserved among nodes of the same label - which is why we test them * below in the same order (in a given label) */ append_element(node_list, d2); append_element(node_list, b2); append_element(node_list, a2); append_element(node_list, a3); append_element(node_list, c1); append_element(node_list, d1); append_element(node_list, b1); append_element(node_list, a1); map = create_label2node_list_map(node_list); if (NULL == map) { printf ("%s: map must not be NULL.\n", test_name); return 1; } if (NULL != hash_get(map, "not there")) { printf ("%s: inexistent label should return NULL.\n", test_name); return 1; } nodes_of_label = hash_get(map, "a"); if (NULL == nodes_of_label) { printf ("%s: there should be a list of nodes with label 'a'.\n", test_name); return 1; } if (nodes_of_label->count != 3) { printf ("%s: list of nodes with label 'a' should" " have length 3\n", test_name); return 1; } el = nodes_of_label->head; if (el->data != a2) { printf ("%s: expected node a2\n", test_name); return 1; } el = el->next; if (el->data != a3) { printf ("%s: expected node a3\n", test_name); return 1; } el = el->next; if (el->data != a1) { printf ("%s: expected node a1\n", test_name); return 1; } el = el->next; if (NULL != el) { printf ("%s: list of nodes with label 'a' is not terminated.\n",test_name); return 1; } nodes_of_label = hash_get(map, "b"); if (NULL == nodes_of_label) { printf ("%s: there should be a list of nodes with label 'b'.\n", test_name); return 1; } if (nodes_of_label->count != 2) { printf ("%s: list of nodes with label 'b' should" " have length 2\n", test_name); return 1; } el = nodes_of_label->head; if (el->data != b2) { printf ("%s: expected node b2\n", test_name); return 1; } el = el->next; if (el->data != b1) { printf ("%s: expected node b1\n", test_name); return 1; } el = el->next; if (NULL != el) { printf ("%s: list of nodes with label 'b' is not terminated.\n",test_name); return 1; } nodes_of_label = hash_get(map, "c"); if (NULL == nodes_of_label) { printf ("%s: there should be a list of nodes with label 'c'.\n", test_name); return 1; } if (nodes_of_label->count != 1) { printf ("%s: list of nodes with label 'c' should" " have length 1\n", test_name); return 1; } el = nodes_of_label->head; if (el->data != c1) { printf ("%s: expected node c1\n", test_name); return 1; } el = el->next; if (NULL != el) { printf ("%s: list of nodes with label 'b' is not terminated.\n",test_name); return 1; } nodes_of_label = hash_get(map, "d"); if (NULL == nodes_of_label) { printf ("%s: there should be a list of nodes with label 'd'.\n", test_name); return 1; } if (nodes_of_label->count != 2) { printf ("%s: list of nodes with label 'd' should" " have length 2\n", test_name); return 1; } el = nodes_of_label->head; if (el->data != d2) { printf ("%s: expected node d2\n", test_name); return 1; } el = el->next; if (el->data != d1) { printf ("%s: expected node d1\n", test_name); return 1; } el = el->next; if (NULL != el) { printf ("%s: list of nodes with label 'b' is not terminated.\n",test_name); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_insert_at_head() { const char *test_name = "test_insert_at_head"; struct llist *list1; struct llist *list2; list1 = create_llist(); append_element(list1, "yeoseot"); append_element(list1, "ilgop"); append_element(list1, "yeodeol"); append_element(list1, "ahob"); append_element(list1, "yeol"); list2 = create_llist(); append_element(list2, "hana"); append_element(list2, "dul"); append_element(list2, "set"); append_element(list2, "net"); append_element(list2, "daseot"); insert_after(list1, -1, list2); struct list_elem *el; if (10 != list1->count) { printf ("%s: expected count of 10, got %d.\n", test_name, list1->count); return 1; } el = list1->head; if (strcmp("hana", (char *) el->data) != 0) { printf ("%s: expected 'hana', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("dul", (char *) el->data) != 0) { printf ("%s: expected 'dul', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("set", (char *) el->data) != 0) { printf ("%s: expected 'set', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("net", (char *) el->data) != 0) { printf ("%s: expected 'net', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("daseot", (char *) el->data) != 0) { printf ("%s: expected 'daseot', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("yeoseot", (char *) el->data) != 0) { printf ("%s: expected 'yeoseot', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("ilgop", (char *) el->data) != 0) { printf ("%s: expected 'ilgop', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("yeodeol", (char *) el->data) != 0) { printf ("%s: expected 'yeodeol', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("ahob", (char *) el->data) != 0) { printf ("%s: expected 'ahob', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("yeol", (char *) el->data) != 0) { printf ("%s: expected 'yeol', got '%s'.\n", test_name, (char *) el->data); return 1; } if (strcmp("yeol", (char *) list1->tail->data) != 0) { printf ("%s: expexted list tail to be 'yeol', got '%s'.\n", test_name, (char *) list1->tail->data); return 1; } if (NULL != list1->tail->next) { printf ("%s: list1 is not terminated.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_insert() { const char *test_name = "test_insert"; struct llist *list1; struct llist *list2; list1 = create_llist(); append_element(list1, "yksi"); append_element(list1, "kaksi"); append_element(list1, "kolme"); append_element(list1, "yhdeksän"); append_element(list1, "kymmenen"); list2 = create_llist(); append_element(list2, "neljä"); append_element(list2, "viisi"); append_element(list2, "kuusi"); append_element(list2, "seitsemän"); append_element(list2, "kahdeksan"); insert_after(list1, 2, list2); struct list_elem *el; if (10 != list1->count) { printf ("%s: expected count of 10, got %d.\n", test_name, list1->count); return 1; } el = list1->head; if (strcmp("yksi", (char *) el->data) != 0) { printf ("%s: expected 'yksi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kaksi", (char *) el->data) != 0) { printf ("%s: expected 'kaksi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kolme", (char *) el->data) != 0) { printf ("%s: expected 'kolme', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("neljä", (char *) el->data) != 0) { printf ("%s: expected 'neljä', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("viisi", (char *) el->data) != 0) { printf ("%s: expected 'viisi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kuusi", (char *) el->data) != 0) { printf ("%s: expected 'kuusi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("seitsemän", (char *) el->data) != 0) { printf ("%s: expected 'seitsemän', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kahdeksan", (char *) el->data) != 0) { printf ("%s: expected 'kahdeksan', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("yhdeksän", (char *) el->data) != 0) { printf ("%s: expected 'yhdeksän', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kymmenen", (char *) el->data) != 0) { printf ("%s: expected 'kymmenen', got '%s'.\n", test_name, (char *) el->data); return 1; } if (strcmp("kymmenen", (char *) list1->tail->data) != 0) { printf ("%s: expexted list tail to be 'kymmenen', got '%s'.\n", test_name, (char *) list1->tail->data); return 1; } if (NULL != list1->tail->next) { printf ("%s: list1 is not terminated.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }