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 <stdio.h>
11 #include <xbt/module.h>
12 #include "xbt/graph.h"
13 #include "xbt/graphxml.h"
14 #include "xbt/dynar.h"
15 #include "xbt/sysdep.h"
16 #include "../src/xbt/graph_private.h"
17 void *node_label_and_data(xbt_node_t node, const char *label,
18                           const char *data);
19 void *node_label_and_data(xbt_node_t node, const char *label,
20                           const char *data)
21 {
22   char *lbl = xbt_strdup(label);
23   return lbl;
24 }
25
26 static const char *node_name(xbt_node_t n)
27 {
28   return n->data;
29 }
30
31 void test(char *graph_file);
32 void test(char *graph_file)
33 {
34   int i, j;
35   unsigned long n;
36   xbt_dynar_t dynar = NULL;
37   xbt_dynar_t dynar1 = NULL;
38   xbt_node_t *sorted = NULL;
39   xbt_node_t *route = NULL;
40
41   xbt_graph_t graph =
42       xbt_graph_read(graph_file, &node_label_and_data, NULL);
43
44   n = xbt_dynar_length(xbt_graph_get_nodes(graph));
45
46   double *adj = xbt_graph_get_length_matrix(graph);
47
48   xbt_graph_export_graphxml(graph, "testgraph.xml", NULL, NULL, NULL,
49                             NULL);
50   xbt_graph_export_graphviz(graph, "graph.dot", node_name, NULL);
51
52   for (i = 0; i < n; i++) {
53     for (j = 0; j < n; j++) {
54       fprintf(stderr, "%le\t", adj[i * n + j]);
55     }
56     fprintf(stderr, "\n");
57   }
58
59
60   route = xbt_graph_shortest_paths(graph);
61
62   /*  for(i=0;i<n;i++) */
63   /*     { */
64   /*       for(j=0;j<n;j++) */
65   /*    { */
66   /*      if( route[i*n+j]) */
67   /*      fprintf(stderr,"%s\t",(char*)(route[i*n+j])->data) ); */
68   /*    } */
69   /*       fprintf(stderr,"\n" ); */
70   /*     } */
71
72
73   sorted = xbt_graph_topo_sort(graph);
74
75   for (i = 0; i < n; i++) {
76     if (sorted[i]) {
77       /* fprintf(stderr,"sorted[%d] =%p\n",i,sorted[i] ); */
78       fprintf(stderr, "%s\t", (char *) ((sorted[i])->data)
79           );
80
81       fprintf(stderr, "\n");
82     }
83   }
84
85   /*  while(xbt_dynar_length(dynar)) */
86   /*     xbt_graph_free_node(graph,*((xbt_node_t*)xbt_dynar_get_ptr(dynar,0)),NULL,NULL); */
87
88   dynar = xbt_graph_get_edges(graph);
89   while (xbt_dynar_length(dynar))
90     xbt_graph_free_edge(graph,
91                         *((xbt_edge_t *) xbt_dynar_get_ptr(dynar, 0)),
92                         NULL);
93
94   printf("%lu edges\n", xbt_dynar_length(dynar));
95   dynar1 = xbt_graph_get_nodes(graph);
96   printf("%lu nodes\n", xbt_dynar_length(dynar1));
97
98   free(adj);
99   free(route);
100   xbt_graph_free_graph(graph, NULL, NULL, NULL);
101 }
102
103 int main(int argc, char **argv)
104 {
105   xbt_init(&argc, argv);
106   if (argc == 1) {
107     fprintf(stderr, "Usage : %s graph.xml\n", argv[0]);
108
109     return 1;
110   }
111   test(argv[1]);
112
113   return 0;
114 }