Logo AND Algorithmique Numérique Distribuée

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