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;
48 xbt_graph_read(graph_file, &node_label_and_data, NULL);
50 n = xbt_dynar_length(xbt_graph_get_nodes(graph));
52 if (test_export_xml) {
53 INFO0("---- Testing XML export. Exporting to testgraph.xml ----");
54 xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
57 if (test_export_dot) {
58 INFO0("---- Testing GraphViz export. Exporting to testgraph.dot ----");
59 xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
62 if (test_export_length) {
66 INFO0("---- Dumping Edge lengths ----");
67 adj = xbt_graph_get_length_matrix(graph);
68 buf = calloc(n * 20, sizeof(char));
69 for (i = 0; i < n; i++) {
70 for (j = 0; j < n; j++) {
71 sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
80 if (test_shortest_paths) {
82 xbt_node_t *route = NULL;
84 INFO0("---- Testing Shortest Paths ----");
85 route = xbt_graph_shortest_paths(graph);
86 buf = calloc(n * 40, sizeof(char));
87 for (i = 0; i < n; i++) {
88 for (j = 0; j < n; j++) {
90 snprintf(buf+strlen(buf), 40, "%s\t",
91 node_name(route[i * n + j]));
100 if (test_topo_sort) {
101 xbt_node_t *sorted = NULL;
103 INFO0("---- Testing Topological Sort ----");
104 sorted = xbt_graph_topo_sort(graph);
105 for (i = 0; i < n; i++) {
107 INFO3("sorted[%d] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
114 if (test_node_deletion) {
115 INFO0("---- Testing Node Deletion ----");
116 nodes = xbt_graph_get_nodes(graph);
117 edges = xbt_graph_get_edges(graph);
118 INFO2("Before Node deletion: %lu nodes, %lu edges",
119 xbt_dynar_length(nodes), xbt_dynar_length(edges));
121 while (xbt_dynar_length(nodes))
122 xbt_graph_free_node(graph,
123 *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
125 INFO2("After Node deletion: %lu nodes, %lu edges",
126 xbt_dynar_length(nodes), xbt_dynar_length(edges));
129 if (test_edge_deletion) {
130 INFO0("---- Testing Edge Deletion ----");
131 nodes = xbt_graph_get_nodes(graph);
132 edges = xbt_graph_get_edges(graph);
133 INFO2("Before Edge deletion: %lu nodes, %lu edges",
134 xbt_dynar_length(nodes), xbt_dynar_length(edges));
136 while (xbt_dynar_length(edges))
137 xbt_graph_free_edge(graph,
138 *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
141 INFO2("After Edge deletion: %lu nodes, %lu edges",
142 xbt_dynar_length(nodes), xbt_dynar_length(edges));
145 xbt_graph_free_graph(graph, NULL, NULL, NULL);
148 int main(int argc, char **argv) {
149 xbt_init(&argc, argv);
151 fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);