Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add pragmas for builder6 compatibility
[simgrid.git] / testsuite / xbt / graphxml_usage.c
1 /*      $Id$      */
2
3 /* A few basic tests for the graphxml library                               */
4
5 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand. All rights reserved.*/
6
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. */
9 #ifdef __BORLANDC__
10  #pragma hdrstop
11  #endif
12  
13 #include "xbt/module.h"
14 #include "xbt/sysdep.h"
15 #include "xbt/graph.h"
16 #include "xbt/graphxml.h"
17 #include "xbt/log.h"
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to graphxml test");
20
21
22 static void *node_label_and_data(xbt_node_t node, const char *label,
23                                  const char *data)
24 {
25   char *lbl = xbt_strdup(label);
26   return lbl;
27 }
28 #define free_label free
29
30 static const char *node_name(xbt_node_t n)
31 {
32   return xbt_graph_node_get_data(n);
33 }
34
35 void test(char *graph_file);
36 void test(char *graph_file)
37 {
38   int test_node_deletion = 0;
39   int test_edge_deletion = 0;
40   int test_export_xml = 1;
41   int test_export_dot = 1;
42   int test_export_length = 1;
43   int test_shortest_paths = 1;
44   int test_topo_sort = 1;
45
46   int i, j;
47   unsigned long n;
48
49   xbt_dynar_t edges = NULL;
50   xbt_dynar_t nodes = NULL;
51
52   xbt_graph_t graph =
53       xbt_graph_read(graph_file, &node_label_and_data, NULL);
54
55   n = xbt_dynar_length(xbt_graph_get_nodes(graph));
56
57   if (test_export_xml) {
58     INFO0("---- Testing XML export. Exporting to testgraph.xml ----");
59     xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
60                               NULL);
61   }
62   if (test_export_dot) {
63     INFO0("---- Testing GraphViz export. Exporting to testgraph.dot ----");
64     xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
65   }
66
67   if (test_export_length) {
68     char *buf = NULL;
69     double *adj = NULL;
70
71     INFO0("---- Dumping Edge lengths ----");
72     adj = xbt_graph_get_length_matrix(graph);
73     buf = calloc(n * 20, sizeof(char));
74     for (i = 0; i < n; i++) {
75       for (j = 0; j < n; j++) {
76         sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
77       }
78       INFO1("%s", buf);
79       buf[0] = '\000';
80     }
81     free(buf);
82     free(adj);
83   }
84
85   if (test_shortest_paths) {
86     char *buf = NULL;
87     xbt_node_t *route = NULL;
88   
89     INFO0("---- Testing Shortest Paths ----");
90     route = xbt_graph_shortest_paths(graph);
91     buf = calloc(n * 40, sizeof(char));
92     for (i = 0; i < n; i++) {
93       for (j = 0; j < n; j++) {
94         if (route[i * n + j])
95           snprintf(buf+strlen(buf), 40, "%s\t", 
96                    node_name(route[i * n + j]));
97       }
98       INFO1("%s", buf);
99       buf[0] = '\000';
100     }
101     free(buf);
102     free(route);
103   }
104
105   if (test_topo_sort) {
106     xbt_node_t *sorted = NULL;
107
108     INFO0("---- Testing Topological Sort ----");
109     sorted = xbt_graph_topo_sort(graph);
110     for (i = 0; i < n; i++) {
111       if (sorted[i]) {
112         INFO3("sorted[%d] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
113       }
114     }
115     free(sorted);
116   }
117
118
119   if (test_node_deletion) {
120     INFO0("---- Testing Node Deletion ----");
121     nodes = xbt_graph_get_nodes(graph);
122     edges = xbt_graph_get_edges(graph);
123     INFO2("Before Node deletion: %lu nodes, %lu edges",
124           xbt_dynar_length(nodes), xbt_dynar_length(edges));
125
126     while (xbt_dynar_length(nodes))
127       xbt_graph_free_node(graph,
128                           *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
129                           free_label, NULL);
130     INFO2("After Node deletion:  %lu nodes, %lu edges",
131           xbt_dynar_length(nodes), xbt_dynar_length(edges));
132   }
133
134   if (test_edge_deletion) {
135     INFO0("---- Testing Edge Deletion ----");
136     nodes = xbt_graph_get_nodes(graph);
137     edges = xbt_graph_get_edges(graph);
138     INFO2("Before Edge deletion: %lu nodes, %lu edges",
139           xbt_dynar_length(nodes), xbt_dynar_length(edges));
140
141     while (xbt_dynar_length(edges))
142       xbt_graph_free_edge(graph,
143                           *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
144                           NULL);
145
146     INFO2("After Edge deletion:  %lu nodes, %lu edges",
147           xbt_dynar_length(nodes), xbt_dynar_length(edges));
148   }
149
150   xbt_graph_free_graph(graph, free_label, NULL, NULL);
151 }
152
153 #ifdef __BORLANDC__
154 #pragma argsused
155 #endif 
156
157 int main(int argc, char **argv) {
158   xbt_init(&argc, argv);
159   if (argc == 1) {
160     fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);
161
162     return 1;
163   }
164   test(argv[1]);
165
166   return 0;
167 }