Logo AND Algorithmique Numérique Distribuée

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