} END_TEST START_TEST(test_graph_get_node) { kld_graph_t * g = (kld_graph_t *) new_graph(); kld_graph_node_t * n1 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n2 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * gn1 = (kld_graph_node_t *) graph_get_node(g, 0); kld_graph_node_t * gn2 = (kld_graph_node_t *) graph_get_node(g, 1); fail_if(gn1 != n1, "Incorrect node returned."); fail_if(gn2 != n2, "Incorrect node returned."); } END_TEST
} END_TEST START_TEST(test_graph_remove_edge) { kld_graph_t * g = (kld_graph_t *) new_graph(); int data = 1; int * data_ref = &data; kld_graph_node_t * n1 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n2 = (kld_graph_node_t *) new_graph_node(g); graph_insert_edge(g, n1, n2, data_ref); graph_remove_edge(g, n1, n2); kld_graph_edge_t * e = (kld_graph_edge_t *) graph_get_edge(g, n1, n2); fail_if(e != NULL, "Edge should be NULL after inserting and then removing it."); } END_TEST
} END_TEST START_TEST(test_graph_insert_edge) { kld_graph_t * g = (kld_graph_t *) new_graph(); int data = 1; int * data_ref = &data; kld_graph_node_t * n1 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n2 = (kld_graph_node_t *) new_graph_node(g); graph_insert_edge(g, n1, n2, data_ref); kld_graph_edge_t * e = (kld_graph_edge_t *) graph_get_edge(g, n1, n2); fail_if(e == NULL, "Edge is NULL after inserting it."); fail_if(e->data == NULL, "Edge data is NULL after inserting it."); fail_if(e->data != data_ref, "Edge data is not the expected value."); } END_TEST
} END_TEST START_TEST(test_graph_get_edge) { kld_graph_t * g = (kld_graph_t *) new_graph(); int data = 1; int * data_ref = &data; kld_graph_node_t * n1 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n2 = (kld_graph_node_t *) new_graph_node(g); graph_insert_edge(g, n1, n2, data_ref); kld_graph_edge_t * e = (kld_graph_edge_t *) graph_get_edge(g, n1, n2); kld_graph_edge_t * e2 = (kld_graph_edge_t *) graph_get_edge(g, n2, n1); fail_if(e == NULL, "Incorrect edge returned."); fail_if(e->data == NULL, "Incorrect edge data returned."); fail_if(e->data != data_ref, "Incorrect edge data returned."); fail_if(e2 != NULL, "Incorrect edge returned."); } END_TEST
} END_TEST START_TEST(test_graph_node_neighbors) { kld_graph_t * g = (kld_graph_t *) new_graph(); int data = 1; kld_graph_node_t * n1 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n2 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n3 = (kld_graph_node_t *) new_graph_node(g); kld_vector_t * v = new_vector(); vector_append(v, NULL); vector_append(v, &data); vector_append(v, NULL); matrix_append_row(g->adj_matrix, v); kld_vector_t * v2 = new_vector(); vector_append(v2, &data); vector_append(v2, NULL); vector_append(v2, NULL); matrix_append_row(g->adj_matrix, v2); kld_vector_t * v3 = new_vector(); vector_append(v3, NULL); vector_append(v3, NULL); vector_append(v3, NULL); matrix_append_row(g->adj_matrix, v3); fail_if(graph_is_empty(g), "Graph should not be empty upon initialization"); kld_vector_t * nbrs1 = (kld_vector_t *) graph_node_neighbors(g, n1); kld_vector_t * nbrs2 = (kld_vector_t *) graph_node_neighbors(g, n2); kld_vector_t * nbrs3 = (kld_vector_t *) graph_node_neighbors(g, n3); fail_if(vector_get(nbrs1, 0) != n2, "After appending an edge from n1 to n2 in the graph, it should mark n2 as a neighbor of n1."); fail_if(vector_get(nbrs2, 0) != n1, "After appending an edge from n2 to n1 in the graph, it should mark n1 as a neighbor of n2."); fail_if(!vector_is_empty(nbrs3), "After appending null edges to the graph, it should have no neighbors"); } END_TEST
} END_TEST START_TEST(test_graph_node_is_adjacent) { kld_graph_t * g = (kld_graph_t *) new_graph(); int data = 1; kld_graph_node_t * n1 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n2 = (kld_graph_node_t *) new_graph_node(g); kld_vector_t * v = new_vector(); vector_append(v, NULL); vector_append(v, &data); matrix_append_row(g->adj_matrix, v); kld_vector_t * v2 = new_vector(); vector_append(v2, &data); vector_append(v2, NULL); matrix_append_row(g->adj_matrix, v2); fail_if(graph_is_empty(g), "Graph should not be empty upon initialization"); fail_if(!graph_node_is_adjacent(g, n1, n2), "Nodes are not adjacent when expected to be"); } END_TEST
void read_file(FILE * inputfile){ char * line; int block_ended = 1, line_count = 0, total_lines = 0, i = 1, j; float next_perc; unsigned int next_article_id = 0, next_author_id = 0; article * temp_article = NULL; author * temp_author; list_node * temp_author_node; graph_node * temp_gnode = NULL; line = (char *) malloc(MAX_LINE_LENGTH*sizeof(char)); while(fgets(line, MAX_LINE_LENGTH, inputfile) != NULL) ++total_lines; rewind(inputfile); line = fgets(line, MAX_LINE_LENGTH, inputfile); ++line_count; while(line_is_blank(line)){ /*remove leading white lines*/ line = fgets(line, MAX_LINE_LENGTH, inputfile); ++line_count; } printf("Creating article graph: "); fflush(stdout); while(line != NULL){ next_perc = (float) line_count / (float) total_lines; if(next_perc > ((float) i /10.0)){ printf("%d%%...",10*i); fflush(stdout); ++i; } if(block_ended && !line_is_blank(line)){ /*a new article/authors block begins*/ remove_ending_newline(line); block_ended = 0; temp_article = new_article(line, next_article_id); temp_gnode = new_graph_node(temp_article, article_node); ++next_article_id; } else { if(line_is_blank(line)){ /*we have just terminated processing an article/authors block */ if(!block_ended){ block_ended = 1; add_node_to_graph(temp_gnode, artcl_graph); } } else{ /*there's a new author for the current article/authors block*/ remove_ending_newline(line); if((temp_author_node = search_in_hash(line, authors_dict)) == NULL){ temp_author = new_author(line, next_author_id); ++next_author_id; insert_in_hash(temp_author, author_node, authors_dict); } else{ temp_author = (author *) temp_author_node->key; /*properly update the article's edges in the graph*/ for(j=0;j<temp_author->n_articles; ++j){ if(temp_author->articles_id[j] < artcl_graph->n_nodes){ /*if there is the same author repeated twice we would be asking for an article id not yet in the graph*/ add_edge(temp_gnode, artcl_graph->nodes[temp_author->articles_id[j]]); /*add edge or increase its weight*/ } } } add_article_to_author(temp_article, temp_author); add_author_to_article(temp_author, temp_article); } } line = fgets(line, MAX_LINE_LENGTH, inputfile); ++line_count; } free(line); }