Logo AND Algorithmique Numérique Distribuée

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