1 /* A few basic tests for the graphxml library */
3 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand. All rights reserved.*/
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #define snprintf _snprintf
17 #include "xbt/module.h"
18 #include "xbt/sysdep.h"
19 #include "xbt/graph.h"
20 #include "xbt/graphxml.h"
23 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to graphxml test");
26 static void *node_label_and_data(xbt_node_t node, const char *label,
29 char *lbl = xbt_strdup(label);
33 #define free_label free
35 static const char *node_name(xbt_node_t n)
37 return xbt_graph_node_get_data(n);
40 void test(char *graph_file);
41 void test(char *graph_file)
43 int test_node_deletion = 0;
44 int test_edge_deletion = 0;
45 int test_export_xml = 1;
46 int test_export_dot = 1;
47 int test_export_length = 1;
48 int test_shortest_paths = 1;
49 int test_topo_sort = 1;
54 xbt_dynar_t edges = NULL;
55 xbt_dynar_t nodes = NULL;
57 xbt_graph_t graph = xbt_graph_read(graph_file, &node_label_and_data, NULL);
59 n = xbt_dynar_length(xbt_graph_get_nodes(graph));
61 if (test_export_xml) {
62 INFO0("---- Testing XML export. Exporting to testgraph.xml ----");
63 xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL, NULL);
65 if (test_export_dot) {
66 INFO0("---- Testing GraphViz export. Exporting to testgraph.dot ----");
67 xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
70 if (test_export_length) {
74 INFO0("---- Dumping Edge lengths ----");
75 adj = xbt_graph_get_length_matrix(graph);
76 buf = xbt_new0(char, n * 20);
77 for (i = 0; i < n; i++) {
78 for (j = 0; j < n; j++) {
79 sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
88 if (test_shortest_paths) {
90 xbt_node_t *route = NULL;
92 INFO0("---- Testing Shortest Paths ----");
93 route = xbt_graph_shortest_paths(graph);
94 buf = xbt_new0(char, n * 40);
95 for (i = 0; i < n; i++) {
96 for (j = 0; j < n; j++) {
98 snprintf(buf + strlen(buf), 40, "%s\t",
99 node_name(route[i * n + j]));
108 if (test_topo_sort) {
109 xbt_node_t *sorted = NULL;
111 INFO0("---- Testing Topological Sort ----");
112 sorted = xbt_graph_topo_sort(graph);
113 for (i = 0; i < n; i++) {
115 INFO3("sorted[%lu] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
122 if (test_node_deletion) {
123 INFO0("---- Testing Node Deletion ----");
124 nodes = xbt_graph_get_nodes(graph);
125 edges = xbt_graph_get_edges(graph);
126 INFO2("Before Node deletion: %lu nodes, %lu edges",
127 xbt_dynar_length(nodes), xbt_dynar_length(edges));
129 while (xbt_dynar_length(nodes))
130 xbt_graph_free_node(graph,
131 *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
133 INFO2("After Node deletion: %lu nodes, %lu edges",
134 xbt_dynar_length(nodes), xbt_dynar_length(edges));
137 if (test_edge_deletion) {
138 INFO0("---- Testing Edge Deletion ----");
139 nodes = xbt_graph_get_nodes(graph);
140 edges = xbt_graph_get_edges(graph);
141 INFO2("Before Edge deletion: %lu nodes, %lu edges",
142 xbt_dynar_length(nodes), xbt_dynar_length(edges));
144 while (xbt_dynar_length(edges))
145 xbt_graph_free_edge(graph,
146 *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
149 INFO2("After Edge deletion: %lu nodes, %lu edges",
150 xbt_dynar_length(nodes), xbt_dynar_length(edges));
153 xbt_graph_free_graph(graph, free_label, NULL, NULL);
160 int main(int argc, char **argv)
162 xbt_init(&argc, argv);
164 fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);