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 append_element (command_t parent, command_t ele) { if (parent->type == SIMPLE_COMMAND) { return 0; } if (parent->type == SUBSHELL_COMMAND) { if (parent->u.subshell_command) { return append_element(parent->u.subshell_command, ele); } else { parent->u.subshell_command = ele; } } if (parent->u.command[1] == NULL) { parent->u.command[1] = ele; return 1; } int r = append_element(parent->u.command[1], ele); if (r == 1) { return r; } if (parent->u.command[0] == NULL) { parent->u.command[0] = ele; return 1; } return append_element(parent->u.command[0], ele); }
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_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 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; }
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_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_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; }
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_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; }
void handle_get_sports_data(uint16_t *data, uint8_t numofdata) { stlv_packet p = create_packet(); if (p == NULL) return; element_handle h = append_element(p, NULL, "A", 1); element_append_data(p, h, (uint8_t*)&data, sizeof(uint16_t) * numofdata); send_packet(p, 0, 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; }
void handle_get_sports_grid() { stlv_packet p = create_packet(); if (p == NULL) return; element_handle h = append_element(p, NULL, "R", 1); ui_config* config = window_readconfig(); element_append_data(p, h, (uint8_t*)&config->sports_grid_data, sizeof(config->sports_grid_data)); log_info("send_sports_grid(%d)\n", sizeof(config->sports_grid_data)); send_packet(p, 0, 0); }
int add_child(struct rnode *parent, struct rnode *child) { struct llist *children_list; child->parent = parent; children_list = parent->children; if (! append_element(children_list, child)) return FAILURE; return SUCCESS; }
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; }
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 *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; }
static GtkStyleContext * get_style (GtkStyleContext *parent, const char *selector) { GtkWidgetPath *path; if (parent) path = gtk_widget_path_copy (gtk_style_context_get_path (parent)); else path = gtk_widget_path_new (); append_element (path, selector); return create_context_for_path (path, parent); }
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; }
/* * This method is used to implement the "hdu headings" subcommand. * It returns the table headings of the current FITS table as a * Tcl list. An error is returned if the current HDU is not a FITS * table. */ int RtdImage::getHDUHeadings(FitsIO* fits) { // return a list of table headings for the current FITS table char* type = (char*)fits->getHDUType(); if (!type || *type == 'i') return error("HDU is not a FITS table"); long nrows = 0; int ncols = 0; if (fits->getTableDims(nrows, ncols) != 0) return TCL_ERROR; reset_result(); for(int col = 1; col <= ncols; col++) { char* s = fits->getTableHead(col); if (!s) return TCL_ERROR; append_element(s); } return TCL_OK; }
static GtkStyleContext * get_style (GtkStyleContext *parent, const char *selector) { GtkWidgetPath *path; GtkStyleContext *context; if (parent) path = gtk_widget_path_copy (gtk_style_context_get_path (parent)); else path = gtk_widget_path_new (); append_element (path, selector); context = gtk_style_context_new (); gtk_style_context_set_path (context, path); gtk_style_context_set_parent (context, parent); gtk_widget_path_unref (path); return context; }
static GtkStyleContext * get_style_with_siblings (GtkStyleContext *parent, const char *selector, const char **siblings, gint position) { GtkWidgetPath *path, *siblings_path; guint i; if (parent) path = gtk_widget_path_copy (gtk_style_context_get_path (parent)); else path = gtk_widget_path_new (); siblings_path = gtk_widget_path_new (); for (i = 0; siblings[i]; i++) append_element (siblings_path, siblings[i]); gtk_widget_path_append_with_siblings (path, siblings_path, position); gtk_widget_path_unref (siblings_path); return create_context_for_path (path, parent); }
int test_prepend_list_empty() { const char *test_name = "test_prepend_list_empty"; struct llist *list1; struct llist *list2; struct list_elem *el; /* Case 1: list 2 is empty */ list1 = create_llist(); append_element(list1, "yksi"); list2 = create_llist(); prepend_list(list1, list2); if (1 != list1->count) { printf ("%s: expected count of 1, got %d.\n", test_name, list1->count); return 1; } if (strcmp("yksi", (char *) list1->head->data) != 0) { printf ("%s: expected 'yksi', got '%s'.\n", test_name, (char *) list1->head->data); return 1; } if (strcmp("yksi", (char *) list1->tail->data) != 0) { printf ("%s: expexted list tail to be 'yksi', 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; } /* Case 2: list 1 is empty */ list1 = create_llist(); list2 = create_llist(); append_element(list2, "yksi"); prepend_list(list1, list2); if (1 != list1->count) { printf ("%s: expected count of 1, 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; } if (strcmp("yksi", (char *) list1->tail->data) != 0) { printf ("%s: expexted list tail to be 'yksi', 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; } /* Case 3: both lists are empty */ list1 = create_llist(); list2 = create_llist(); prepend_list(list1, list2); 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.\n", test_name); return 1; } if (NULL != list1->tail) { printf ("%s: expected NULL tail.\n", test_name); return 1; } printf("%s ok.\n", test_name); 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_delete_at_tail() { const char *test_name = "test_delete_at_tail"; struct llist *list1; struct llist *list2; struct list_elem *el; 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"); list2 = delete_after(list1, 6, 3); /* check list1 */ if (7 != list1->count) { printf ("%s: expected count of 7, 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; } if (strcmp("seitsemän", (char *) list1->tail->data) != 0) { printf ("%s: expexted list tail to be 'seitsemän', 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; } /* check list2 */ if (3 != list2->count) { printf ("%s: expected count of 3, got %d.\n", test_name, list2->count); return 1; } el = list2->head; 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 *) list2->tail->data) != 0) { printf ("%s: expexted list tail to be 'kymmenen', got '%s'.\n", test_name, (char *) list2->tail->data); return 1; } if (NULL != list2->tail->next) { printf ("%s: list2 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; }
int test_delete_last_few() { const char *test_name = __func__; struct llist *list1; struct llist *list2; struct list_elem *el; list1 = create_llist(); append_element(list1, "ichi"); append_element(list1, "ni"); append_element(list1, "san"); append_element(list1, "shi"); append_element(list1, "go"); list2 = delete_after(list1, -1, 5); /* 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 (5 != list2->count) { printf ("%s: expected count of 5, got %d.\n", test_name, list2->count); return 1; } el = list2->head; if (strcmp("ichi", (char *) el->data) != 0) { printf ("%s: expected 'ichi' at head, got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("ni", (char *) el->data) != 0) { printf ("%s: expected 'ni', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("san", (char *) el->data) != 0) { printf ("%s: expected 'san', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("shi", (char *) el->data) != 0) { printf ("%s: expected 'shi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("go", (char *) el->data) != 0) { printf ("%s: expected 'go', got '%s'.\n", test_name, (char *) el->data); return 1; } el = list2->tail; if (strcmp("go", (char *) el->data) != 0) { printf ("%s: expected 'go' 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_shallow_copy() { char *test_name = "test_shallow_copy"; struct llist *list_p, *list_copy_p; struct list_elem *elem; char label_two[] = {'t', 'w', 'o', '\0'}; list_p = create_llist(); append_element(list_p, "one"); append_element(list_p, label_two); /* see below */ append_element(list_p, "three"); append_element(list_p, "four"); append_element(list_p, "five"); list_copy_p = shallow_copy(list_p); /* test list membership and count */ elem = list_copy_p->head; if (strcmp(elem->data, "one") != 0) { printf ("%s: expected 'one', 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, "three") != 0) { printf ("%s: expected 'three', 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, "five") != 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; } /* Check that copy is shallow, i.e., original list members are the * same a s copy members */ /* String constants cannot be modified (SIGSEGV!), which is why we * used an array for label #2 */ elem = list_p->head->next; /* should work with any of them */ label_two[0] = 'z'; elem = list_copy_p->head->next; if (strcmp(elem->data, "zwo") != 0) { printf ("%s: expected element in copy to be 'zwo' (got '%s').\n", test_name, (char *) elem->data); return 1; } printf("%s ok.\n", test_name); return 0; }