Logo AND Algorithmique Numérique Distribuée

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