Logo AND Algorithmique Numérique Distribuée

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