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");
19 static void *node_label_and_data(xbt_node_t node, const char *label,
22 char *lbl = xbt_strdup(label);
25 #define free_label free
27 static const char *node_name(xbt_node_t n)
29 return xbt_graph_node_get_data(n);
32 void test(char *graph_file);
33 void test(char *graph_file)
35 int test_node_deletion = 0;
36 int test_edge_deletion = 0;
37 int test_export_xml = 1;
38 int test_export_dot = 1;
39 int test_export_length = 1;
40 int test_shortest_paths = 1;
41 int test_topo_sort = 1;
46 xbt_dynar_t edges = NULL;
47 xbt_dynar_t nodes = NULL;
50 xbt_graph_read(graph_file, &node_label_and_data, NULL);
52 n = xbt_dynar_length(xbt_graph_get_nodes(graph));
54 if (test_export_xml) {
55 INFO0("---- Testing XML export. Exporting to testgraph.xml ----");
56 xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
59 if (test_export_dot) {
60 INFO0("---- Testing GraphViz export. Exporting to testgraph.dot ----");
61 xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
64 if (test_export_length) {
68 INFO0("---- Dumping Edge lengths ----");
69 adj = xbt_graph_get_length_matrix(graph);
70 buf = calloc(n * 20, sizeof(char));
71 for (i = 0; i < n; i++) {
72 for (j = 0; j < n; j++) {
73 sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
82 if (test_shortest_paths) {
84 xbt_node_t *route = NULL;
86 INFO0("---- Testing Shortest Paths ----");
87 route = xbt_graph_shortest_paths(graph);
88 buf = calloc(n * 40, sizeof(char));
89 for (i = 0; i < n; i++) {
90 for (j = 0; j < n; j++) {
92 snprintf(buf+strlen(buf), 40, "%s\t",
93 node_name(route[i * n + j]));
102 if (test_topo_sort) {
103 xbt_node_t *sorted = NULL;
105 INFO0("---- Testing Topological Sort ----");
106 sorted = xbt_graph_topo_sort(graph);
107 for (i = 0; i < n; i++) {
109 INFO3("sorted[%d] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
116 if (test_node_deletion) {
117 INFO0("---- Testing Node Deletion ----");
118 nodes = xbt_graph_get_nodes(graph);
119 edges = xbt_graph_get_edges(graph);
120 INFO2("Before Node deletion: %lu nodes, %lu edges",
121 xbt_dynar_length(nodes), xbt_dynar_length(edges));
123 while (xbt_dynar_length(nodes))
124 xbt_graph_free_node(graph,
125 *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
127 INFO2("After Node deletion: %lu nodes, %lu edges",
128 xbt_dynar_length(nodes), xbt_dynar_length(edges));
131 if (test_edge_deletion) {
132 INFO0("---- Testing Edge Deletion ----");
133 nodes = xbt_graph_get_nodes(graph);
134 edges = xbt_graph_get_edges(graph);
135 INFO2("Before Edge deletion: %lu nodes, %lu edges",
136 xbt_dynar_length(nodes), xbt_dynar_length(edges));
138 while (xbt_dynar_length(edges))
139 xbt_graph_free_edge(graph,
140 *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
143 INFO2("After Edge deletion: %lu nodes, %lu edges",
144 xbt_dynar_length(nodes), xbt_dynar_length(edges));
147 xbt_graph_free_graph(graph, free_label, NULL, NULL);
150 int main(int argc, char **argv) {
151 xbt_init(&argc, argv);
153 fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);