Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New dataset from alpha
[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_double(gras_socket_t *sock, int direction);
57 gras_error_t test_array(gras_socket_t *sock, int direction);
58 gras_error_t test_intref(gras_socket_t *sock, int direction);
59 gras_error_t test_string(gras_socket_t *sock, int direction);
60
61 gras_error_t test_homostruct(gras_socket_t *sock, int direction);
62 gras_error_t test_hetestruct(gras_socket_t *sock, int direction);
63 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction);
64 gras_error_t test_chain_list(gras_socket_t *sock, int direction);
65 gras_error_t test_graph(gras_socket_t *sock, int direction);
66
67 gras_error_t test_pbio(gras_socket_t *sock, int direction);
68
69 /* defined in datadesc_structures.c, which in perl generated */
70 gras_error_t test_structures(gras_socket_t *sock, int direction); 
71
72
73
74 gras_error_t test_int(gras_socket_t *sock, int direction) {
75   gras_error_t errcode;
76   int i=5,j;
77   
78   INFO0("---- Test on integer ----");
79   TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
80   if (direction == READ || direction == RW) {
81     gras_assert(i == j);
82   }
83   return no_error;
84 }
85 gras_error_t test_float(gras_socket_t *sock, int direction) {
86   gras_error_t errcode;
87   float i=5.0,j;
88   
89   INFO0("---- Test on float ----");
90   TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
91   if (direction == READ || direction == RW) {
92     gras_assert2(i == j,"%f != %f",i,j);
93   }
94   return no_error;
95 }
96 gras_error_t test_double(gras_socket_t *sock, int direction) {
97   gras_error_t errcode;
98   double i=-3252355.1234,j;
99   
100   INFO0("---- Test on double ----");
101   TRY(write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction));
102   if (direction == READ || direction == RW) {
103     gras_assert2(i == j,"%f != %f",i,j);
104   }
105   return no_error;
106 }
107
108 #define SIZE 5
109 typedef int array[SIZE];
110 gras_error_t test_array(gras_socket_t *sock, int direction) {
111   gras_error_t errcode;
112   gras_datadesc_type_t *my_type;
113   
114   array i = { 35212,-6226,74337,11414,7733};
115   array j;
116   int cpt;
117
118   INFO0("---- Test on fixed array ----");
119
120   TRY(gras_datadesc_declare_array_fixed("fixed int array", 
121                                         gras_datadesc_by_name("int"),
122                                         SIZE, &my_type));
123
124   TRY(write_read(my_type, &i,&j, sock,direction));
125   if (direction == READ || direction == RW) {
126     for (cpt=0; cpt<SIZE; cpt++) {
127       DEBUG1("Test spot %d",cpt);
128       gras_assert4(i[cpt] == j[cpt],"i[%d]=%d  !=  j[%d]=%d",
129                    cpt,i[cpt],cpt,j[cpt]);
130     }
131   }
132   return no_error;
133 }
134 gras_error_t test_intref(gras_socket_t *sock, int direction) {
135   gras_error_t errcode;
136   gras_datadesc_type_t *my_type;
137   int *i,*j;
138   
139   if (! (i=malloc(sizeof(int))) )
140     RAISE_MALLOC;
141   *i=12345;
142
143   INFO1("---- Test on a reference to an integer (%p) ----",i);
144
145   TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
146
147   TRY(write_read(my_type, &i,&j, sock,direction));
148   if (direction == READ || direction == RW) {
149     gras_assert(*i == *j);
150     free(j);
151   }
152   free(i);
153   return no_error;
154 }
155
156 /***
157  *** string (dynamic array)
158  ***/ 
159 gras_error_t test_string(gras_socket_t *sock, int direction) {
160   gras_error_t errcode;
161   char *i=strdup("Some data"), *j=NULL;
162   int cpt;
163   
164   INFO0("---- Test on string (ref to dynamic array) ----");
165   TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
166                  sock,direction));
167   if (direction == READ || direction == RW) {
168     for (cpt=0; cpt<strlen(i); cpt++) {
169       gras_assert4(i[cpt] == j[cpt],"i[%d]=%c  !=  j[%d]=%c",
170                    cpt,i[cpt],cpt,j[cpt]);
171     } 
172     free(j);
173   }
174   free(i);
175   return no_error;
176 }
177
178
179 /***
180  *** homogeneous struct
181  ***/ 
182 typedef struct {
183   int a,b,c,d;
184 } homostruct;
185 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
186   gras_error_t errcode;
187   gras_datadesc_type_t *my_type;
188   homostruct *i, *j; 
189
190   INFO0("---- Test on homogeneous structure ----");
191   /* create descriptor */
192   TRY(gras_datadesc_declare_struct("homostruct",&my_type));
193   TRY(gras_datadesc_declare_struct_append(my_type,"a",
194                                           gras_datadesc_by_name("signed int")));
195   TRY(gras_datadesc_declare_struct_append(my_type,"b",
196                                           gras_datadesc_by_name("int")));
197   TRY(gras_datadesc_declare_struct_append(my_type,"c",
198                                           gras_datadesc_by_name("int")));
199   TRY(gras_datadesc_declare_struct_append(my_type,"d",
200                                           gras_datadesc_by_name("int")));
201   gras_datadesc_declare_struct_close(my_type);
202   TRY(gras_datadesc_declare_ref("homostruct*",
203                                 gras_datadesc_by_name("homostruct"),
204                                 &my_type));
205
206   /* init a value, exchange it and check its validity*/
207   if (! (i=malloc(sizeof(homostruct))) )
208     RAISE_MALLOC;
209   i->a = 2235;    i->b = 433425;
210   i->c = -23423;  i->d = -235235;
211
212   TRY(write_read(my_type, &i,&j, sock,direction));
213   if (direction == READ || direction == RW) {
214     gras_assert(i->a == j->a);
215     gras_assert(i->b == j->b);
216     gras_assert(i->c == j->c);
217     gras_assert(i->d == j->d);
218     free(j);
219   }
220   free(i);
221   return no_error;
222 }
223
224 /***
225  *** heterogeneous struct
226  ***/ 
227 typedef struct {
228   unsigned char c1;
229   unsigned long int l1;
230   unsigned char c2;
231   unsigned long int l2;
232 } hetestruct;
233 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
234   gras_error_t errcode;
235   gras_datadesc_type_t *my_type;
236   hetestruct *i, *j; 
237
238   INFO0("---- Test on heterogeneous structure ----");
239   /* create descriptor */
240   TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
241   TRY(gras_datadesc_declare_struct_append(my_type,"c1",
242                                           gras_datadesc_by_name("unsigned char")));
243   TRY(gras_datadesc_declare_struct_append(my_type,"l1",
244                                           gras_datadesc_by_name("unsigned long int")));
245   TRY(gras_datadesc_declare_struct_append(my_type,"c2",
246                                           gras_datadesc_by_name("unsigned char")));
247   TRY(gras_datadesc_declare_struct_append(my_type,"l2",
248                                           gras_datadesc_by_name("unsigned long int")));
249   gras_datadesc_declare_struct_close(my_type);
250   TRY(gras_datadesc_declare_ref("hetestruct*",
251                                 gras_datadesc_by_name("hetestruct"),
252                                 &my_type));
253
254   /* init a value, exchange it and check its validity*/
255   if (! (i=malloc(sizeof(hetestruct))) )
256     RAISE_MALLOC;
257   i->c1 = 's'; i->l1 = 123455;
258   i->c2 = 'e'; i->l2 = 774531;
259
260   TRY(write_read(my_type, &i,&j, sock,direction));
261   if (direction == READ || direction == RW) {
262     gras_assert(i->c1 == j->c1);
263     gras_assert(i->c2 == j->c2);
264     gras_assert2(i->l1 == j->l1,"i->l1(=%d)  !=  j->l1(=%d)",i->l1,j->l1);
265     gras_assert(i->l2 == j->l2);
266     free(j);
267   }
268   free(i);
269   return no_error;
270 }
271
272 /***
273  *** nested struct
274  ***/ 
275 typedef struct {
276   hetestruct hete;
277   homostruct homo;
278 } nestedstruct;
279 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
280   gras_error_t errcode;
281   gras_datadesc_type_t *my_type;
282   nestedstruct *i, *j; 
283
284   INFO0("---- Test on nested structures ----");
285   /* create descriptor */
286   TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
287
288   TRY(gras_datadesc_declare_struct_append(my_type,"hete",
289                                           gras_datadesc_by_name("hetestruct")));
290   TRY(gras_datadesc_declare_struct_append(my_type,"homo",
291                                           gras_datadesc_by_name("homostruct")));
292   gras_datadesc_declare_struct_close(my_type);
293   TRY(gras_datadesc_declare_ref("nestedstruct*",
294                                 gras_datadesc_by_name("nestedstruct"),
295                                 &my_type));
296
297   /* init a value, exchange it and check its validity*/
298   if (! (i=malloc(sizeof(nestedstruct))) )
299     RAISE_MALLOC;
300   i->homo.a = 235231;  i->homo.b = -124151;
301   i->homo.c = 211551;  i->homo.d = -664222;
302   i->hete.c1 = 's'; i->hete.l1 = 123455;
303   i->hete.c2 = 'e'; i->hete.l2 = 774531;
304
305   TRY(write_read(my_type, &i,&j, sock,direction));
306   if (direction == READ || direction == RW) {
307     gras_assert(i->homo.a == j->homo.a);
308     gras_assert(i->homo.b == j->homo.b);
309     gras_assert(i->homo.c == j->homo.c);
310     gras_assert(i->homo.d == j->homo.d);
311     gras_assert(i->hete.c1 == j->hete.c1);
312     gras_assert(i->hete.c2 == j->hete.c2);
313     gras_assert(i->hete.l1 == j->hete.l1);
314     gras_assert(i->hete.l2 == j->hete.l2);
315     free(j);
316   }
317   free(i);
318   return no_error;
319 }
320
321 /***
322  *** chained list
323  ***/ 
324 typedef struct s_chained_list chained_list_t;
325 struct s_chained_list {
326   int          v;
327   chained_list_t *l;
328 };
329 gras_error_t declare_chained_list_type(void);
330 chained_list_t *cons(int v, chained_list_t *l);
331 void list_free(chained_list_t *l);
332 int list_eq(chained_list_t*i,chained_list_t*j);
333
334 gras_error_t declare_chained_list_type(void) {
335   gras_error_t errcode;
336   gras_datadesc_type_t *my_type,*ref_my_type;
337
338   TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
339   TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
340
341   TRY(gras_datadesc_declare_struct_append(my_type,"v",
342                                           gras_datadesc_by_name("int")));
343   TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
344   gras_datadesc_declare_struct_close(my_type);
345
346   return no_error;
347 }
348
349 chained_list_t * cons(int v, chained_list_t *l) {
350   chained_list_t *nl = malloc(sizeof (chained_list_t));
351   
352   nl->v = v;
353   nl->l = l;
354   
355   return nl;
356 }
357 void list_free(chained_list_t*l) {
358   if (l) {
359     list_free(l->l);
360     free(l);
361   }
362 }
363 int list_eq(chained_list_t*i,chained_list_t*j) {
364   if (!i || !j) return i == j;
365   if (i->v != j->v)
366     return 0;
367   return list_eq(i->l, j->l); 
368 }
369 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
370   gras_error_t errcode;
371   chained_list_t *i, *j; 
372
373   INFO0("---- Test on chained list ----");
374
375   /* init a value, exchange it and check its validity*/
376   i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
377   j = NULL;
378
379   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
380                  &i,&j,
381                  sock,direction));
382   if (direction == READ || direction == RW) {
383     gras_assert(list_eq(i,j));    
384     list_free(j);
385   }
386
387   list_free(i);
388   return no_error;
389 }
390 /***
391  *** graph
392  ***/
393 gras_error_t test_graph(gras_socket_t *sock, int direction) {
394   gras_error_t errcode;
395   chained_list_t *i, *j; 
396
397   INFO0("---- Test on graph (cyclique chained list) ----");
398   /* init a value, exchange it and check its validity*/
399   i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
400   i->l->l->l = i;
401   j = NULL;
402
403   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
404                  &i,&j, sock,direction));
405   if (direction == READ || direction == RW) {
406     
407     DEBUG1("i=%p"         ,i);
408     DEBUG1("i->l=%p"      ,i->l);
409     DEBUG1("i->l->l=%p"   ,i->l->l);
410     DEBUG1("i->l->l->l=%p",i->l->l->l);
411     DEBUG1("j=%p"         ,j);
412     DEBUG1("j->l=%p"      ,j->l);
413     DEBUG1("j->l->l=%p"   ,j->l->l);
414     DEBUG1("j->l->l->l=%p",j->l->l->l);
415     gras_assert4(j->l->l->l == j,
416                  "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
417                  "j=%p; &j=%p",
418                  j,j->l->l->l, 
419                  j ,&j);
420     j->l->l->l = NULL;
421     i->l->l->l = NULL;
422     gras_assert(list_eq(i,j));
423
424     list_free(j);
425   }
426   i->l->l->l = NULL; /* do this even in WRITE mode */
427   list_free(i);
428   return no_error;
429 }
430
431 /**** PBIO *****/
432 GRAS_DEFINE_TYPE(s_pbio,
433 struct s_pbio{ /* structure presented in the IEEE article */
434   int Cnstatv;
435   double Cstatev[12];
436   int Cnprops;
437   double Cprops[110];
438   int Cndi[4];
439   int Cnshr;
440   int Cnpt;
441   double Cdtime;
442   double Ctime[2];
443   int Cntens;
444   double Cdfgrd0[373][3];
445   double Cdfgrd1[3][3];
446   double Cstress[106];
447   double Cddsdde[106][106];
448 };
449                  )
450 typedef struct s_pbio pbio_t;
451
452 gras_error_t test_pbio(gras_socket_t *sock, int direction) {
453   gras_error_t errcode;
454   pbio_t i,j;
455   int cpt;
456   int cpt2;
457   gras_datadesc_type_t *pbio_type;
458
459   INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
460   pbio_type = gras_datadesc_by_symbol(s_pbio);
461
462   /* Fill in that damn struct */
463   i.Cnstatv = 325115;
464   for (cpt=0; cpt<12; cpt++) 
465     i.Cstatev[cpt] = ((double) cpt) * -2361.11;
466   i.Cnprops = -37373;
467   for (cpt=0; cpt<110; cpt++)
468     i.Cprops[cpt] = cpt * 100.0;
469   for (cpt=0; cpt<4; cpt++)
470     i.Cndi[cpt] = cpt * 23262;
471   i.Cnshr = -4634;
472   i.Cnpt = 114142;
473   i.Cdtime = -11515.662;
474   i.Ctime[0] = 332523.226;
475   i.Ctime[1] = -26216.113;
476   i.Cntens = 235211411;
477   
478   for (cpt=0; cpt<3; cpt++) {
479     for (cpt2=0; cpt2<373; cpt2++)
480       i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
481     for (cpt2=0; cpt2<3; cpt2++)
482       i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
483   }
484   for (cpt=0; cpt<106; cpt++) {
485     i.Cstress[cpt]=(double)cpt * 22.113;
486     for (cpt2=0; cpt2<106; cpt2++) 
487       i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
488   }
489   TRY(write_read(gras_datadesc_by_symbol(s_pbio),
490                  &i,&j, sock,direction));
491   if (direction == READ || direction == RW) {
492     /* Check that the data match */
493     gras_assert(i.Cnstatv == j.Cnstatv);
494     for (cpt=0; cpt<12; cpt++)
495       gras_assert(i.Cstatev[cpt] == j.Cstatev[cpt]);
496     gras_assert(i.Cnprops == j.Cnprops);
497     for (cpt=0; cpt<110; cpt++)
498       gras_assert(i.Cprops[cpt] == j.Cprops[cpt]);
499     for (cpt=0; cpt<4; cpt++) 
500       gras_assert(i.Cndi[cpt] == j.Cndi[cpt]);
501     gras_assert(i.Cnshr == j.Cnshr);
502     gras_assert(i.Cnpt == j.Cnpt);
503     gras_assert(i.Cdtime == j.Cdtime);
504     gras_assert(i.Ctime[0] == j.Ctime[0]);
505     gras_assert(i.Ctime[1] == j.Ctime[1]);
506     gras_assert(i.Cntens == j.Cntens);
507     for (cpt=0; cpt<3; cpt++) {
508       for (cpt2=0; cpt2<373; cpt2++)
509         gras_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
510       for (cpt2=0; cpt2<3; cpt2++)
511         gras_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
512     }
513     for (cpt=0; cpt<106; cpt++) {
514       gras_assert(i.Cstress[cpt] == j.Cstress[cpt]);
515       for (cpt2=0; cpt2<106; cpt2++) 
516         gras_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
517                      "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
518                      i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
519     }
520   }
521
522   return no_error;
523 }
524
525 int main(int argc,char *argv[]) {
526   gras_error_t errcode;
527   gras_socket_t *sock;
528   int direction = RW;
529   int cpt;
530   char r_arch_char = gras_arch_selfid();
531
532   gras_init_defaultlog(&argc,argv,NULL);
533
534   for (cpt=1; cpt<argc; cpt++) {
535     if (!strcmp(argv[cpt], "--read")) {
536       direction = READ;
537     } else if (!strcmp(argv[cpt], "--write")) {
538       direction = WRITE;
539     } else {
540        filename=argv[cpt];
541     }
542   }
543     
544   if (direction == WRITE) {
545     TRYFAIL(gras_socket_client_from_file(filename,&sock));
546     TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
547                            &r_arch_char));
548   }
549   if (direction == READ) {
550     TRYFAIL(gras_socket_server_from_file(filename,&sock));
551     TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
552                            gras_arch_selfid(), &r_arch_char));
553     INFO3("This datafile was generated on %s (%d), I'm %s.",
554           gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
555           gras_datadesc_arch_name(gras_arch_selfid()));
556   }
557   r_arch = (int)r_arch_char;
558   
559   
560   TRYFAIL(test_int(sock,direction));    
561   TRYFAIL(test_float(sock,direction));  
562   TRYFAIL(test_double(sock,direction));  
563   TRYFAIL(test_array(sock,direction));  
564   TRYFAIL(test_intref(sock,direction)); 
565   
566   TRYFAIL(test_string(sock,direction)); 
567
568   TRYFAIL(test_structures(sock,direction));
569
570   TRYFAIL(test_homostruct(sock,direction));
571   TRYFAIL(test_hetestruct(sock,direction));
572   TRYFAIL(test_nestedstruct(sock,direction));
573
574   TRYFAIL(declare_chained_list_type());
575   TRYFAIL(test_chain_list(sock,direction));
576   TRYFAIL(test_graph(sock,direction));
577
578   TRYFAIL(test_pbio(sock,direction));
579
580   if (direction != RW) 
581     gras_socket_close(sock);
582   gras_exit();
583   return 0;
584 }
585