Logo AND Algorithmique Numérique Distribuée

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