Logo AND Algorithmique Numérique Distribuée

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