Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ce359afa7bf103fa999d1436c94d0e3cdc90b94e
[simgrid.git] / testsuite / gras / datadesc_usage.c
1 /* $Id$ */
2
3 /* datadesc: test of data description (using file transport).               */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdio.h>
12 #include <gras.h>
13
14 #include "../DataDesc/datadesc_interface.h"
15 GRAS_LOG_NEW_DEFAULT_CATEGORY(test);
16
17 #define READ  0
18 #define WRITE 1
19 #define RW    2
20
21 int r_arch;
22 const char *filename = "datadesc_usage.out";  
23
24 gras_error_t
25 write_read(gras_datadesc_type_t *type,void *src, void *dst, 
26            gras_socket_t *sock, int direction);
27
28 gras_error_t
29 write_read(gras_datadesc_type_t *type,void *src, void *dst, 
30            gras_socket_t *sock, int direction) {
31   gras_error_t errcode;
32    
33   /* write */
34   if (direction == RW) 
35     TRY(gras_socket_client_from_file(filename,&sock));
36   if (direction == WRITE || direction == RW)
37     TRY(gras_datadesc_send(sock, type, src));
38   if (direction == RW) 
39     gras_socket_close(sock);
40    
41   /* read */
42   if (direction == RW) 
43     TRY(gras_socket_server_from_file(filename,&sock));
44
45   if (direction == READ || direction == RW)
46     TRY(gras_datadesc_recv(sock, type, r_arch, dst));
47
48   if (direction == RW) 
49     gras_socket_close(sock);
50   
51   return no_error;
52 }
53
54 gras_error_t test_int(gras_socket_t *sock, int direction);
55 gras_error_t test_float(gras_socket_t *sock, int direction);
56 gras_error_t test_array(gras_socket_t *sock, int direction);
57 gras_error_t test_intref(gras_socket_t *sock, int direction);
58 gras_error_t test_string(gras_socket_t *sock, int direction);
59
60 gras_error_t test_homostruct(gras_socket_t *sock, int direction);
61 gras_error_t test_hetestruct(gras_socket_t *sock, int direction);
62 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction);
63 gras_error_t test_chain_list(gras_socket_t *sock, int direction);
64 gras_error_t test_graph(gras_socket_t *sock, int direction);
65
66 gras_error_t test_int(gras_socket_t *sock, int direction) {
67   gras_error_t errcode;
68   int i=5,j;
69   
70   INFO0("==== Test on integer ====");
71   TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
72   if (direction == READ || direction == RW) {
73     gras_assert(i == j);
74   }
75   return no_error;
76 }
77 gras_error_t test_float(gras_socket_t *sock, int direction) {
78   gras_error_t errcode;
79   float i=5.0,j;
80   
81   INFO0("==== Test on float ====");
82   TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
83   if (direction == READ || direction == RW) {
84     gras_assert(i == j);
85   }
86   return no_error;
87 }
88
89 #define SIZE 5
90 typedef int array[SIZE];
91 gras_error_t test_array(gras_socket_t *sock, int direction) {
92   gras_error_t errcode;
93   gras_datadesc_type_t *my_type;
94   
95   array i = { 35212,-6226,74337,11414,7733};
96   array j;
97   int cpt;
98
99   INFO0("==== Test on fixed array ====");
100
101   TRY(gras_datadesc_declare_array_fixed("fixed int array", 
102                                         gras_datadesc_by_name("int"),
103                                         SIZE, &my_type));
104
105   TRY(write_read(my_type, &i,&j, sock,direction));
106   if (direction == READ || direction == RW) {
107     for (cpt=0; cpt<SIZE; cpt++) {
108       DEBUG1("Test spot %d",cpt);
109       gras_assert4(i[cpt] == j[cpt],"i[%d]=%d  !=  j[%d]=%d",
110                    cpt,i[cpt],cpt,j[cpt]);
111     }
112   }
113   return no_error;
114 }
115 gras_error_t test_intref(gras_socket_t *sock, int direction) {
116   gras_error_t errcode;
117   gras_datadesc_type_t *my_type;
118   int *i,*j;
119   
120   if (! (i=malloc(sizeof(int))) )
121     RAISE_MALLOC;
122   *i=12345;
123
124   INFO1("==== Test on a reference to an integer (%p) ====",i);
125
126   TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
127
128   TRY(write_read(my_type, &i,&j, sock,direction));
129   if (direction == READ || direction == RW) {
130     gras_assert(*i == *j);
131     free(j);
132   }
133   free(i);
134   return no_error;
135 }
136
137 /***
138  *** string (dynamic array)
139  ***/ 
140 gras_error_t test_string(gras_socket_t *sock, int direction) {
141   gras_error_t errcode;
142   char *i=strdup("Some data"), *j=NULL;
143   int cpt;
144   
145   INFO0("==== Test on string (ref to dynamic array) ====");
146   TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
147                  sock,direction));
148   if (direction == READ || direction == RW) {
149     for (cpt=0; cpt<strlen(i); cpt++) {
150       gras_assert4(i[cpt] == j[cpt],"i[%d]=%c  !=  j[%d]=%c",
151                    cpt,i[cpt],cpt,j[cpt]);
152     } 
153     free(j);
154   }
155   free(i);
156   return no_error;
157 }
158
159
160 /***
161  *** homogeneous struct
162  ***/ 
163 typedef struct {
164   int a,b,c,d;
165 } homostruct;
166 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
167   gras_error_t errcode;
168   gras_datadesc_type_t *my_type;
169   homostruct *i, *j; 
170
171   INFO0("==== Test on homogeneous structure ====");
172   /* create descriptor */
173   TRY(gras_datadesc_declare_struct("homostruct",&my_type));
174   TRY(gras_datadesc_declare_struct_append(my_type,"a",
175                                           gras_datadesc_by_name("signed int")));
176   TRY(gras_datadesc_declare_struct_append(my_type,"b",
177                                           gras_datadesc_by_name("int")));
178   TRY(gras_datadesc_declare_struct_append(my_type,"c",
179                                           gras_datadesc_by_name("int")));
180   TRY(gras_datadesc_declare_struct_append(my_type,"d",
181                                           gras_datadesc_by_name("int")));
182   TRY(gras_datadesc_declare_ref("homostruct*",
183                                 gras_datadesc_by_name("homostruct"),
184                                 &my_type));
185
186   /* init a value, exchange it and check its validity*/
187   if (! (i=malloc(sizeof(homostruct))) )
188     RAISE_MALLOC;
189   i->a = 2235;    i->b = 433425;
190   i->c = -23423;  i->d = -235235;
191
192   TRY(write_read(my_type, &i,&j, sock,direction));
193   if (direction == READ || direction == RW) {
194     gras_assert(i->a == j->a);
195     gras_assert(i->b == j->b);
196     gras_assert(i->c == j->c);
197     gras_assert(i->d == j->d);
198     free(j);
199   }
200   free(i);
201   return no_error;
202 }
203
204 /***
205  *** heterogeneous struct
206  ***/ 
207 typedef struct {
208   unsigned char c1;
209   unsigned long int l1;
210   unsigned char c2;
211   unsigned long int l2;
212 } hetestruct;
213 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
214   gras_error_t errcode;
215   gras_datadesc_type_t *my_type;
216   hetestruct *i, *j; 
217
218   INFO0("==== Test on heterogeneous structure ====");
219   /* create descriptor */
220   TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
221   TRY(gras_datadesc_declare_struct_append(my_type,"c1",
222                                           gras_datadesc_by_name("unsigned char")));
223   TRY(gras_datadesc_declare_struct_append(my_type,"l1",
224                                           gras_datadesc_by_name("unsigned long int")));
225   TRY(gras_datadesc_declare_struct_append(my_type,"c2",
226                                           gras_datadesc_by_name("unsigned char")));
227   TRY(gras_datadesc_declare_struct_append(my_type,"l2",
228                                           gras_datadesc_by_name("unsigned long int")));
229   TRY(gras_datadesc_declare_ref("hetestruct*",
230                                 gras_datadesc_by_name("hetestruct"),
231                                 &my_type));
232
233   /* init a value, exchange it and check its validity*/
234   if (! (i=malloc(sizeof(hetestruct))) )
235     RAISE_MALLOC;
236   i->c1 = 's'; i->l1 = 123455;
237   i->c2 = 'e'; i->l2 = 774531;
238
239   TRY(write_read(my_type, &i,&j, sock,direction));
240   if (direction == READ || direction == RW) {
241     gras_assert(i->c1 == j->c1);
242     gras_assert(i->c2 == j->c2);
243     gras_assert(i->l1 == j->l1);
244     gras_assert(i->l2 == j->l2);
245     free(j);
246   }
247   free(i);
248   return no_error;
249 }
250
251 /***
252  *** nested struct
253  ***/ 
254 typedef struct {
255   hetestruct hete;
256   homostruct homo;
257 } nestedstruct;
258 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
259   gras_error_t errcode;
260   gras_datadesc_type_t *my_type;
261   nestedstruct *i, *j; 
262
263   INFO0("==== Test on nested structures ====");
264   /* create descriptor */
265   TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
266
267   TRY(gras_datadesc_declare_struct_append(my_type,"hete",
268                                           gras_datadesc_by_name("hetestruct")));
269   TRY(gras_datadesc_declare_struct_append(my_type,"homo",
270                                           gras_datadesc_by_name("homostruct")));
271   TRY(gras_datadesc_declare_ref("nestedstruct*",
272                                 gras_datadesc_by_name("nestedstruct"),
273                                 &my_type));
274
275   /* init a value, exchange it and check its validity*/
276   if (! (i=malloc(sizeof(nestedstruct))) )
277     RAISE_MALLOC;
278   i->homo.a = 235231;  i->homo.b = -124151;
279   i->homo.c = 211551;  i->homo.d = -664222;
280   i->hete.c1 = 's'; i->hete.l1 = 123455;
281   i->hete.c2 = 'e'; i->hete.l2 = 774531;
282
283   TRY(write_read(my_type, &i,&j, sock,direction));
284   if (direction == READ || direction == RW) {
285     gras_assert(i->homo.a == j->homo.a);
286     gras_assert(i->homo.b == j->homo.b);
287     gras_assert(i->homo.c == j->homo.c);
288     gras_assert(i->homo.d == j->homo.d);
289     gras_assert(i->hete.c1 == j->hete.c1);
290     gras_assert(i->hete.c2 == j->hete.c2);
291     gras_assert(i->hete.l1 == j->hete.l1);
292     gras_assert(i->hete.l2 == j->hete.l2);
293     free(j);
294   }
295   free(i);
296   return no_error;
297 }
298
299 /***
300  *** chained list
301  ***/ 
302 typedef struct s_chained_list chained_list_t;
303 struct s_chained_list {
304   int          v;
305   chained_list_t *l;
306 };
307 gras_error_t declare_chained_list_type(void);
308 chained_list_t *cons(int v, chained_list_t *l);
309 void list_free(chained_list_t *l);
310 int list_eq(chained_list_t*i,chained_list_t*j);
311
312 gras_error_t declare_chained_list_type(void) {
313   gras_error_t errcode;
314   gras_datadesc_type_t *my_type,*ref_my_type;
315
316   TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
317   TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
318
319   TRY(gras_datadesc_declare_struct_append(my_type,"v",
320                                           gras_datadesc_by_name("int")));
321   TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
322
323   return no_error;
324 }
325
326 chained_list_t * cons(int v, chained_list_t *l) {
327   chained_list_t *nl = malloc(sizeof (chained_list_t));
328   
329   nl->v = v;
330   nl->l = l;
331   
332   return nl;
333 }
334 void list_free(chained_list_t*l) {
335   if (l) {
336     list_free(l->l);
337     free(l);
338   }
339 }
340 int list_eq(chained_list_t*i,chained_list_t*j) {
341   if (!i || !j) return i == j;
342   if (i->v != j->v)
343     return 0;
344   return list_eq(i->l, j->l); 
345 }
346 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
347   gras_error_t errcode;
348   chained_list_t *i, *j; 
349
350   INFO0("==== Test on chained list ====");
351
352   /* init a value, exchange it and check its validity*/
353   i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
354   j = NULL;
355
356   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
357                  &i,&j,
358                  sock,direction));
359   if (direction == READ || direction == RW) {
360     gras_assert(list_eq(i,j));    
361     list_free(j);
362   }
363
364   list_free(i);
365   return no_error;
366 }
367 /***
368  *** graph
369  ***/
370 gras_error_t test_graph(gras_socket_t *sock, int direction) {
371   gras_error_t errcode;
372   chained_list_t *i, *j; 
373
374   INFO0("==== Test on graph (cyclique chained list) ====");
375   /* init a value, exchange it and check its validity*/
376   i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
377   i->l->l->l = i;
378   j = NULL;
379
380   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
381                  &i,&j, sock,direction));
382   if (direction == READ || direction == RW) {
383     
384     DEBUG1("i=%p"         ,i);
385     DEBUG1("i->l=%p"      ,i->l);
386     DEBUG1("i->l->l=%p"   ,i->l->l);
387     DEBUG1("i->l->l->l=%p",i->l->l->l);
388     DEBUG1("j=%p"         ,j);
389     DEBUG1("j->l=%p"      ,j->l);
390     DEBUG1("j->l->l=%p"   ,j->l->l);
391     DEBUG1("j->l->l->l=%p",j->l->l->l);
392     gras_assert4(j->l->l->l == j,
393                  "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
394                  "j=%p; &j=%p",
395                  j,j->l->l->l, 
396                  j ,&j);
397     j->l->l->l = NULL;
398     i->l->l->l = NULL;
399     gras_assert(list_eq(i,j));
400
401     list_free(j);
402   }
403   i->l->l->l = NULL; /* do this even in READ mode */
404   list_free(i);
405   return no_error;
406 }
407
408 /**** PBIO *****/
409 typedef struct { /* structure presented in the IEEE article */
410   int Cnstatv;
411   double Cstatev[12];
412   int Cnprops;
413   double Cprops[110];
414   int Cndi[4], Cnshr, Cnpt;
415   double Cdtime, Ctime[2];
416   int Cntens;
417   double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
418 } KSdata1;
419
420 int main(int argc,char *argv[]) {
421   gras_error_t errcode;
422   gras_socket_t *sock;
423   int direction = RW;
424   int cpt;
425   char r_arch_char = gras_arch_selfid();
426
427   gras_init_defaultlog(&argc,argv,NULL);
428
429   for (cpt=1; cpt<argc; cpt++) {
430     if (!strcmp(argv[cpt], "--read")) {
431       direction = READ;
432     } else if (!strcmp(argv[cpt], "--write")) {
433       direction = WRITE;
434     } else {
435        filename=argv[cpt];
436     }
437   }
438     
439   if (direction == WRITE) {
440     TRYFAIL(gras_socket_client_from_file(filename,&sock));
441     TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
442                            &r_arch_char));
443   }
444   if (direction == READ) {
445     TRYFAIL(gras_socket_server_from_file(filename,&sock));
446     TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
447                            gras_arch_selfid(), &r_arch_char));
448     INFO3("This datafile was generated on %s (%d), I'm %s.",
449           gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
450           gras_datadesc_arch_name(gras_arch_selfid()));
451   }
452   r_arch = (int)r_arch_char;
453   
454   TRYFAIL(test_int(sock,direction));    
455   TRYFAIL(test_float(sock,direction));  
456   TRYFAIL(test_array(sock,direction));  
457   TRYFAIL(test_intref(sock,direction)); 
458   
459   TRYFAIL(test_string(sock,direction)); 
460
461   TRYFAIL(test_homostruct(sock,direction));
462   TRYFAIL(test_hetestruct(sock,direction));
463   TRYFAIL(test_nestedstruct(sock,direction));
464
465   TRYFAIL(declare_chained_list_type());
466   TRYFAIL(test_chain_list(sock,direction));
467   TRYFAIL(test_graph(sock,direction));
468
469   if (direction != RW) 
470     gras_socket_close(sock);
471   gras_exit();
472   return 0;
473 }
474