3 /* A few basic tests for the graphxml library */
5 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand. All rights reserved.*/
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/module.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/graph.h"
13 #include "xbt/graphxml.h"
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to graphxml test");
18 static void *node_label_and_data(xbt_node_t node, const char *label,
21 char *lbl = xbt_strdup(label);
25 static const char *node_name(xbt_node_t n)
27 return xbt_graph_node_get_data(n);
30 void test(char *graph_file);
31 void test(char *graph_file)
33 int test_node_deletion = 1;
34 int test_edge_deletion = 1;
35 int test_export_xml = 1;
36 int test_export_dot = 1;
37 int test_export_length = 1;
38 int test_shortest_paths = 1;
39 int test_topo_sort = 1;
44 xbt_dynar_t edges = NULL;
45 xbt_dynar_t nodes = NULL;
46 xbt_node_t *sorted = NULL;
47 xbt_node_t *route = NULL;
50 xbt_graph_read(graph_file, &node_label_and_data, NULL);
53 n = xbt_dynar_length(xbt_graph_get_nodes(graph));
55 if (test_export_xml) {
56 INFO0("---- Testing XML export. Exporting to testgraph.xml ----");
57 xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
60 if (test_export_dot) {
61 INFO0("---- Testing GraphViz export. Exporting to testgraph.dot ----");
62 xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
65 if (test_export_length) {
67 INFO0("---- Dumping Edge lengths ----");
68 adj = xbt_graph_get_length_matrix(graph);
69 buf = calloc(n * 20, sizeof(char));
70 for (i = 0; i < n; i++) {
71 for (j = 0; j < n; j++) {
72 sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
81 if (test_shortest_paths) {
83 INFO0("---- Testing Shortest Paths ----");
84 route = xbt_graph_shortest_paths(graph);
85 buf = calloc(n * 40, sizeof(char));
86 for (i = 0; i < n; i++) {
87 for (j = 0; j < n; j++) {
89 snprintf(buf+strlen(buf), 40, "%s\t",
90 node_name(route[i * n + j]));
100 INFO0("---- Testing Topological Sort ----");
101 sorted = xbt_graph_topo_sort(graph);
102 for (i = 0; i < n; i++) {
104 INFO3("sorted[%d] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
111 if (test_node_deletion) {
112 INFO0("---- Testing Node Deletion ----");
113 nodes = xbt_graph_get_nodes(graph);
114 edges = xbt_graph_get_edges(graph);
115 INFO2("Before Node deletion: %lu nodes, %lu edges",
116 xbt_dynar_length(nodes), xbt_dynar_length(edges));
118 while (xbt_dynar_length(nodes))
119 xbt_graph_free_node(graph,
120 *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
122 INFO2("After Node deletion: %lu nodes, %lu edges",
123 xbt_dynar_length(nodes), xbt_dynar_length(edges));
126 if (test_edge_deletion) {
127 INFO0("---- Testing Edge Deletion ----");
128 nodes = xbt_graph_get_nodes(graph);
129 edges = xbt_graph_get_edges(graph);
130 INFO2("Before Edge deletion: %lu nodes, %lu edges",
131 xbt_dynar_length(nodes), xbt_dynar_length(edges));
133 while (xbt_dynar_length(edges))
134 xbt_graph_free_edge(graph,
135 *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
138 INFO2("After Edge deletion: %lu nodes, %lu edges",
139 xbt_dynar_length(nodes), xbt_dynar_length(edges));
142 xbt_graph_free_graph(graph, NULL, NULL, NULL);
145 int main(int argc, char **argv) {
146 xbt_init(&argc, argv);
148 fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);