Logo AND Algorithmique Numérique Distribuée

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