1 /* A few basic tests for the graphxml library */
3 /* Copyright (c) 2006-2014. The SimGrid Team.
4 * All rights reserved. */
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. */
16 #define snprintf _snprintf
19 #include "xbt/module.h"
20 #include "xbt/sysdep.h"
21 #include "xbt/graph.h"
22 #include "xbt/graphxml.h"
25 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to graphxml test");
28 static void *node_label_and_data(xbt_node_t node, const char *label,
31 char *lbl = xbt_strdup(label);
35 #define free_label free
37 static const char *node_name(xbt_node_t n)
39 return xbt_graph_node_get_data(n);
42 void test(char *graph_file);
43 void test(char *graph_file)
45 int test_node_deletion = 0;
46 int test_edge_deletion = 0;
47 int test_export_xml = 1;
48 int test_export_dot = 1;
49 int test_export_length = 1;
50 int test_shortest_paths = 1;
51 int test_topo_sort = 1;
56 xbt_dynar_t edges = NULL;
57 xbt_dynar_t nodes = NULL;
60 xbt_graph_read(graph_file, &node_label_and_data, NULL);
62 n = xbt_dynar_length(xbt_graph_get_nodes(graph));
64 if (test_export_xml) {
65 XBT_INFO("---- Testing XML export. Exporting to testgraph.xml ----");
66 xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
69 if (test_export_dot) {
70 XBT_INFO("---- Testing GraphViz export. Exporting to testgraph.dot ----");
71 xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
74 if (test_export_length) {
78 XBT_INFO("---- Dumping Edge lengths ----");
79 adj = xbt_graph_get_length_matrix(graph);
80 buf = xbt_new0(char, n * 20);
81 for (i = 0; i < n; i++) {
82 for (j = 0; j < n; j++) {
83 sprintf(buf + strlen(buf), "%6.3f\t", adj[i * n + j]);
92 if (test_shortest_paths) {
94 xbt_node_t *route = NULL;
96 XBT_INFO("---- Testing Shortest Paths ----");
97 route = xbt_graph_shortest_paths(graph);
98 buf = xbt_new0(char, n * 40);
99 for (i = 0; i < n; i++) {
100 for (j = 0; j < n; j++) {
101 if (route[i * n + j])
102 snprintf(buf + strlen(buf), 40, "%s\t",
103 node_name(route[i * n + j]));
112 if (test_topo_sort) {
113 xbt_node_t *sorted = NULL;
115 XBT_INFO("---- Testing Topological Sort ----");
116 sorted = xbt_graph_topo_sort(graph);
117 for (i = 0; i < n; i++) {
119 XBT_INFO("sorted[%lu] = %s", i, node_name(sorted[i]));
126 if (test_node_deletion) {
127 XBT_INFO("---- Testing Node Deletion ----");
128 nodes = xbt_graph_get_nodes(graph);
129 edges = xbt_graph_get_edges(graph);
130 XBT_INFO("Before Node deletion: %lu nodes, %lu edges",
131 xbt_dynar_length(nodes), xbt_dynar_length(edges));
133 while (!xbt_dynar_is_empty(nodes))
134 xbt_graph_free_node(graph,
135 *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
137 XBT_INFO("After Node deletion: %lu nodes, %lu edges",
138 xbt_dynar_length(nodes), xbt_dynar_length(edges));
141 if (test_edge_deletion) {
142 XBT_INFO("---- Testing Edge Deletion ----");
143 nodes = xbt_graph_get_nodes(graph);
144 edges = xbt_graph_get_edges(graph);
145 XBT_INFO("Before Edge deletion: %lu nodes, %lu edges",
146 xbt_dynar_length(nodes), xbt_dynar_length(edges));
148 while (!xbt_dynar_is_empty(edges))
149 xbt_graph_free_edge(graph,
150 *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
153 XBT_INFO("After Edge deletion: %lu nodes, %lu edges",
154 xbt_dynar_length(nodes), xbt_dynar_length(edges));
157 xbt_graph_free_graph(graph, free_label, NULL, NULL);
165 int main(int argc, char **argv)
167 xbt_init(&argc, argv);
169 fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);