Logo AND Algorithmique Numérique Distribuée

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