Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix commit f48cc395ebecc84d865ab551a672d2a2358624e5
[simgrid.git] / testsuite / xbt / graphxml_usage.c
index d55ce07..e49db05 100644 (file)
-/*      $Id$      */
-
 /* A few basic tests for the graphxml library                               */
 
-/* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand. All rights reserved.*/
+/* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
+#ifdef __BORLANDC__
+#pragma hdrstop
+#endif
 
 #include <stdio.h>
-#include <xbt/module.h>
+
+#ifdef _MSC_VER
+#define snprintf  _snprintf
+#endif
+
+#include "xbt/module.h"
+#include "xbt/sysdep.h"
 #include "xbt/graph.h"
 #include "xbt/graphxml.h"
-#include "xbt/dynar.h"
-#include "xbt/sysdep.h"
-#include "../src/xbt/graph_private.h"
-void *node_label_and_data(xbt_node_t node, const char *label,
-                         const char *data);
-void *node_label_and_data(xbt_node_t node, const char *label,
-                         const char *data)
+#include "xbt/log.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to graphxml test");
+
+
+static void *node_label_and_data(xbt_node_t node, const char *label,
+                                 const char *data)
 {
   char *lbl = xbt_strdup(label);
   return lbl;
 }
 
+#define free_label free
+
 static const char *node_name(xbt_node_t n)
 {
-  return n->data;
+  return xbt_graph_node_get_data(n);
 }
 
 void test(char *graph_file);
 void test(char *graph_file)
 {
-  int i, j;
+  int test_node_deletion = 0;
+  int test_edge_deletion = 0;
+  int test_export_xml = 1;
+  int test_export_dot = 1;
+  int test_export_length = 1;
+  int test_shortest_paths = 1;
+  int test_topo_sort = 1;
+
+  unsigned long i, j;
   unsigned long n;
-  xbt_dynar_t dynar = NULL;
-  xbt_dynar_t dynar1 = NULL;
-  xbt_node_t *sorted = NULL;
-  xbt_node_t *route = NULL;
+
+  xbt_dynar_t edges = NULL;
+  xbt_dynar_t nodes = NULL;
 
   xbt_graph_t graph =
       xbt_graph_read(graph_file, &node_label_and_data, NULL);
 
   n = xbt_dynar_length(xbt_graph_get_nodes(graph));
 
-  double *adj = xbt_graph_get_length_matrix(graph);
-
-  xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL, NULL);
-  xbt_graph_export_graphviz(graph, "graph.dot", node_name, NULL);
+  if (test_export_xml) {
+    XBT_INFO("---- Testing XML export. Exporting to testgraph.xml ----");
+    xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
+                              NULL);
+  }
+  if (test_export_dot) {
+    XBT_INFO("---- Testing GraphViz export. Exporting to testgraph.dot ----");
+    xbt_graph_export_graphviz(graph, "testgraph.dot", node_name, NULL);
+  }
 
-  for (i = 0; i < n; i++) {
-    for (j = 0; j < n; j++) {
-      fprintf(stderr, "%le\t", adj[i * n + j]);
+  if (test_export_length) {
+    char *buf = NULL;
+    double *adj = NULL;
+
+    XBT_INFO("---- Dumping Edge lengths ----");
+    adj = xbt_graph_get_length_matrix(graph);
+    buf = xbt_new0(char, n * 20);
+    for (i = 0; i < n; i++) {
+      for (j = 0; j < n; j++) {
+        sprintf(buf + strlen(buf), "%le\t", adj[i * n + j]);
+      }
+      XBT_INFO("%s", buf);
+      buf[0] = '\000';
     }
-    fprintf(stderr, "\n");
+    free(buf);
+    free(adj);
   }
 
+  if (test_shortest_paths) {
+    char *buf = NULL;
+    xbt_node_t *route = NULL;
+
+    XBT_INFO("---- Testing Shortest Paths ----");
+    route = xbt_graph_shortest_paths(graph);
+    buf = xbt_new0(char, n * 40);
+    for (i = 0; i < n; i++) {
+      for (j = 0; j < n; j++) {
+        if (route[i * n + j])
+          snprintf(buf + strlen(buf), 40, "%s\t",
+                   node_name(route[i * n + j]));
+      }
+      XBT_INFO("%s", buf);
+      buf[0] = '\000';
+    }
+    free(buf);
+    free(route);
+  }
 
-  route = xbt_graph_shortest_paths(graph);
-
-  /*  for(i=0;i<n;i++) */
-/*     { */
-/*       for(j=0;j<n;j++) */
-/*     { */
-/*       if( route[i*n+j]) */
-/*       fprintf(stderr,"%s\t",(char*)(route[i*n+j])->data) ); */
-/*     } */
-/*       fprintf(stderr,"\n" ); */
-/*     } */
+  if (test_topo_sort) {
+    xbt_node_t *sorted = NULL;
 
+    XBT_INFO("---- Testing Topological Sort ----");
+    sorted = xbt_graph_topo_sort(graph);
+    for (i = 0; i < n; i++) {
+      if (sorted[i]) {
+        XBT_INFO("sorted[%lu] = %s (%p)", i, node_name(sorted[i]), sorted[i]);
+      }
+    }
+    free(sorted);
+  }
 
-  sorted = xbt_graph_topo_sort(graph);
 
-  for (i = 0; i < n; i++) {
-    if (sorted[i]) {
-/* fprintf(stderr,"sorted[%d] =%p\n",i,sorted[i] ); */
-      fprintf(stderr, "%s\t", (char *) ((sorted[i])->data)
-         );
+  if (test_node_deletion) {
+    XBT_INFO("---- Testing Node Deletion ----");
+    nodes = xbt_graph_get_nodes(graph);
+    edges = xbt_graph_get_edges(graph);
+    XBT_INFO("Before Node deletion: %lu nodes, %lu edges",
+          xbt_dynar_length(nodes), xbt_dynar_length(edges));
 
-      fprintf(stderr, "\n");
-    }
+    while (!xbt_dynar_is_empty(nodes))
+      xbt_graph_free_node(graph,
+                          *((xbt_node_t *) xbt_dynar_get_ptr(nodes, 0)),
+                          free_label, NULL);
+    XBT_INFO("After Node deletion:  %lu nodes, %lu edges",
+          xbt_dynar_length(nodes), xbt_dynar_length(edges));
   }
 
-  /*  while(xbt_dynar_length(dynar)) */
-/*     xbt_graph_free_node(graph,*((xbt_node_t*)xbt_dynar_get_ptr(dynar,0)),NULL,NULL); */
+  if (test_edge_deletion) {
+    XBT_INFO("---- Testing Edge Deletion ----");
+    nodes = xbt_graph_get_nodes(graph);
+    edges = xbt_graph_get_edges(graph);
+    XBT_INFO("Before Edge deletion: %lu nodes, %lu edges",
+          xbt_dynar_length(nodes), xbt_dynar_length(edges));
+
+    while (!xbt_dynar_is_empty(edges))
+      xbt_graph_free_edge(graph,
+                          *((xbt_edge_t *) xbt_dynar_get_ptr(edges, 0)),
+                          NULL);
 
-  dynar = xbt_graph_get_edges(graph);
-  while (xbt_dynar_length(dynar))
-    xbt_graph_free_edge(graph,
-                       *((xbt_edge_t *) xbt_dynar_get_ptr(dynar, 0)),
-                       NULL);
+    XBT_INFO("After Edge deletion:  %lu nodes, %lu edges",
+          xbt_dynar_length(nodes), xbt_dynar_length(edges));
+  }
 
-  printf("%lu edges\n", xbt_dynar_length(dynar));
-  dynar1 = xbt_graph_get_nodes(graph);
-  printf("%lu nodes\n", xbt_dynar_length(dynar1));
+  xbt_graph_free_graph(graph, free_label, NULL, NULL);
 
-  free(adj);
-  free(route);
-  xbt_graph_free_graph(graph, NULL, NULL, NULL);
 }
 
+#ifdef __BORLANDC__
+#pragma argsused
+#endif
+
 int main(int argc, char **argv)
 {
   xbt_init(&argc, argv);
@@ -108,6 +170,5 @@ int main(int argc, char **argv)
     return 1;
   }
   test(argv[1]);
-
   return 0;
 }