Logo AND Algorithmique Numérique Distribuée

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