Logo AND Algorithmique Numérique Distribuée

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