Logo AND Algorithmique Numérique Distribuée

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