Logo AND Algorithmique Numérique Distribuée

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