1 /* A few basic tests for the graphxml library */
3 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. 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. */
15 #define snprintf _snprintf
18 #include "xbt/module.h"
19 #include "xbt/sysdep.h"
20 #include "xbt/graph.h"
21 #include "xbt/graphxml.h"
24 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to graphxml test");
27 static void *node_label_and_data(xbt_node_t node, const char *label,
30 char *lbl = xbt_strdup(label);
34 #define free_label free
36 static const char *node_name(xbt_node_t n)
38 return xbt_graph_node_get_data(n);
41 void test(char *graph_file);
42 void test(char *graph_file)
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;
55 xbt_dynar_t edges = NULL;
56 xbt_dynar_t nodes = NULL;
59 xbt_graph_read(graph_file, &node_label_and_data, NULL);
61 n = xbt_dynar_length(xbt_graph_get_nodes(graph));
63 if (test_export_xml) {
64 XBT_INFO("---- Testing XML export. Exporting to testgraph.xml ----");
65 xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
68 if (test_export_dot) {
69 XBT_INFO("---- Testing GraphViz export. Exporting to testgraph.dot ----");
70 xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
73 if (test_export_length) {
77 XBT_INFO("---- Dumping Edge lengths ----");
78 adj = xbt_graph_get_length_matrix(graph);
79 buf = xbt_new0(char, n * 20);
80 for (i = 0; i < n; i++) {
81 for (j = 0; j < n; j++) {
82 sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
91 if (test_shortest_paths) {
93 xbt_node_t *route = NULL;
95 XBT_INFO("---- Testing Shortest Paths ----");
96 route = xbt_graph_shortest_paths(graph);
97 buf = xbt_new0(char, n * 40);
98 for (i = 0; i < n; i++) {
99 for (j = 0; j < n; j++) {
100 if (route[i * n + j])
101 snprintf(buf + strlen(buf), 40, "%s\t",
102 node_name(route[i * n + j]));
111 if (test_topo_sort) {
112 xbt_node_t *sorted = NULL;
114 XBT_INFO("---- Testing Topological Sort ----");
115 sorted = xbt_graph_topo_sort(graph);
116 for (i = 0; i < n; i++) {
118 XBT_INFO("sorted[%lu] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
125 if (test_node_deletion) {
126 XBT_INFO("---- Testing Node Deletion ----");
127 nodes = xbt_graph_get_nodes(graph);
128 edges = xbt_graph_get_edges(graph);
129 XBT_INFO("Before Node deletion: %lu nodes, %lu edges",
130 xbt_dynar_length(nodes), xbt_dynar_length(edges));
132 while (!xbt_dynar_is_empty(nodes))
133 xbt_graph_free_node(graph,
134 *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
136 XBT_INFO("After Node deletion: %lu nodes, %lu edges",
137 xbt_dynar_length(nodes), xbt_dynar_length(edges));
140 if (test_edge_deletion) {
141 XBT_INFO("---- Testing Edge Deletion ----");
142 nodes = xbt_graph_get_nodes(graph);
143 edges = xbt_graph_get_edges(graph);
144 XBT_INFO("Before Edge deletion: %lu nodes, %lu edges",
145 xbt_dynar_length(nodes), xbt_dynar_length(edges));
147 while (!xbt_dynar_is_empty(edges))
148 xbt_graph_free_edge(graph,
149 *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
152 XBT_INFO("After Edge deletion: %lu nodes, %lu edges",
153 xbt_dynar_length(nodes), xbt_dynar_length(edges));
156 xbt_graph_free_graph(graph, free_label, NULL, NULL);
164 int main(int argc, char **argv)
166 xbt_init(&argc, argv);
168 fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);