Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e6450c04dfb41024f01946996fbc87a496e092ff
[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 <stdio.h>
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,
24                                  const char *data)
25 {
26   char *lbl = xbt_strdup(label);
27   return lbl;
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     INFO0("---- Testing XML export. Exporting to testgraph.xml ----");
60     xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
61                               NULL);
62   }
63   if (test_export_dot) {
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     char *buf = NULL;
70     double *adj = NULL;
71
72     INFO0("---- Dumping Edge lengths ----");
73     adj = xbt_graph_get_length_matrix(graph);
74     buf = calloc(n * 20, sizeof(char));
75     for (i = 0; i < n; i++) {
76       for (j = 0; j < n; j++) {
77         sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
78       }
79       INFO1("%s", buf);
80       buf[0] = '\000';
81     }
82     free(buf);
83     free(adj);
84   }
85
86   if (test_shortest_paths) {
87     char *buf = NULL;
88     xbt_node_t *route = NULL;
89   
90     INFO0("---- Testing Shortest Paths ----");
91     route = xbt_graph_shortest_paths(graph);
92     buf = calloc(n * 40, sizeof(char));
93     for (i = 0; i < n; i++) {
94       for (j = 0; j < n; j++) {
95         if (route[i * n + j])
96           snprintf(buf+strlen(buf), 40, "%s\t", 
97                    node_name(route[i * n + j]));
98       }
99       INFO1("%s", buf);
100       buf[0] = '\000';
101     }
102     free(buf);
103     free(route);
104   }
105
106   if (test_topo_sort) {
107     xbt_node_t *sorted = NULL;
108
109     INFO0("---- Testing Topological Sort ----");
110     sorted = xbt_graph_topo_sort(graph);
111     for (i = 0; i < n; i++) {
112       if (sorted[i]) {
113         INFO3("sorted[%d] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
114       }
115     }
116     free(sorted);
117   }
118
119
120   if (test_node_deletion) {
121     INFO0("---- Testing Node Deletion ----");
122     nodes = xbt_graph_get_nodes(graph);
123     edges = xbt_graph_get_edges(graph);
124     INFO2("Before Node deletion: %lu nodes, %lu edges",
125           xbt_dynar_length(nodes), xbt_dynar_length(edges));
126
127     while (xbt_dynar_length(nodes))
128       xbt_graph_free_node(graph,
129                           *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
130                           free_label, NULL);
131     INFO2("After Node deletion:  %lu nodes, %lu edges",
132           xbt_dynar_length(nodes), xbt_dynar_length(edges));
133   }
134
135   if (test_edge_deletion) {
136     INFO0("---- Testing Edge Deletion ----");
137     nodes = xbt_graph_get_nodes(graph);
138     edges = xbt_graph_get_edges(graph);
139     INFO2("Before Edge deletion: %lu nodes, %lu edges",
140           xbt_dynar_length(nodes), xbt_dynar_length(edges));
141
142     while (xbt_dynar_length(edges))
143       xbt_graph_free_edge(graph,
144                           *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
145                           NULL);
146
147     INFO2("After Edge deletion:  %lu nodes, %lu edges",
148           xbt_dynar_length(nodes), xbt_dynar_length(edges));
149   }
150
151   xbt_graph_free_graph(graph, free_label, NULL, NULL);
152 }
153
154 #ifdef __BORLANDC__
155 #pragma argsused
156 #endif 
157
158 int main(int argc, char **argv) {
159   xbt_init(&argc, argv);
160   if (argc == 1) {
161     fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);
162
163     return 1;
164   }
165   test(argv[1]);
166
167   return 0;
168 }