Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f09b5d99a86cfa7cf30f8d622cc6ad5c2f26050d
[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   gras_datadesc_type_t *my_type;
140   char *i=strdup("Some data"), *j=NULL;
141   int cpt;
142   
143   INFO0("==== Test on string (dynamic array) ====");
144   TRY(gras_datadesc_declare_ref("string*",
145                                 gras_datadesc_by_name("string"),
146                                 &my_type));
147
148   TRY(write_read(gras_datadesc_by_name("string*"), &i,&j,
149                  sock,direction));
150   if (direction == READ || direction == RW) {
151     for (cpt=0; cpt<strlen(i); cpt++) {
152       fprintf(stderr,"%d ", cpt);
153       gras_assert4(i[cpt] == j[cpt],"i[%d]=%c  !=  j[%d]=%c",
154                    cpt,i[cpt],cpt,j[cpt]);
155     } 
156     free(j);
157   }
158   free(i);
159   return no_error;
160 }
161
162
163 /***
164  *** homogeneous struct
165  ***/ 
166 typedef struct {
167   int a,b,c,d;
168 } homostruct;
169 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
170   gras_error_t errcode;
171   gras_datadesc_type_t *my_type;
172   homostruct *i, *j; 
173
174   INFO0("==== Test on homogeneous structure ====");
175   /* create descriptor */
176   TRY(gras_datadesc_declare_struct("homostruct",&my_type));
177   TRY(gras_datadesc_declare_struct_append(my_type,"a",
178                                           gras_datadesc_by_name("signed int")));
179   TRY(gras_datadesc_declare_struct_append(my_type,"b",
180                                           gras_datadesc_by_name("int")));
181   TRY(gras_datadesc_declare_struct_append(my_type,"c",
182                                           gras_datadesc_by_name("int")));
183   TRY(gras_datadesc_declare_struct_append(my_type,"d",
184                                           gras_datadesc_by_name("int")));
185   TRY(gras_datadesc_declare_ref("homostruct*",
186                                 gras_datadesc_by_name("homostruct"),
187                                 &my_type));
188
189   /* init a value, exchange it and check its validity*/
190   if (! (i=malloc(sizeof(homostruct))) )
191     RAISE_MALLOC;
192   i->a = rand();  i->b = rand();
193   i->c = rand();  i->d = rand();
194
195   TRY(write_read(my_type, &i,&j, sock,direction));
196   if (direction == READ || direction == RW) {
197     gras_assert(i->a == j->a);
198     gras_assert(i->b == j->b);
199     gras_assert(i->c == j->c);
200     gras_assert(i->d == j->d);
201     free(j);
202   }
203   free(i);
204   return no_error;
205 }
206
207 /***
208  *** heterogeneous struct
209  ***/ 
210 typedef struct {
211   unsigned char c1;
212   unsigned long int l1;
213   unsigned char c2;
214   unsigned long int l2;
215 } hetestruct;
216 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
217   gras_error_t errcode;
218   gras_datadesc_type_t *my_type;
219   hetestruct *i, *j; 
220
221   INFO0("==== Test on heterogeneous structure ====");
222   /* create descriptor */
223   TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
224   TRY(gras_datadesc_declare_struct_append(my_type,"c1",
225                                           gras_datadesc_by_name("unsigned char")));
226   TRY(gras_datadesc_declare_struct_append(my_type,"l1",
227                                           gras_datadesc_by_name("unsigned long int")));
228   TRY(gras_datadesc_declare_struct_append(my_type,"c2",
229                                           gras_datadesc_by_name("unsigned char")));
230   TRY(gras_datadesc_declare_struct_append(my_type,"l2",
231                                           gras_datadesc_by_name("unsigned long int")));
232   TRY(gras_datadesc_declare_ref("hetestruct*",
233                                 gras_datadesc_by_name("hetestruct"),
234                                 &my_type));
235
236   /* init a value, exchange it and check its validity*/
237   if (! (i=malloc(sizeof(hetestruct))) )
238     RAISE_MALLOC;
239   i->c1 = 's'; i->l1 = 123455;
240   i->c2 = 'e'; i->l2 = 774531;
241
242   TRY(write_read(my_type, &i,&j, sock,direction));
243   if (direction == READ || direction == RW) {
244     gras_assert(i->c1 == j->c1);
245     gras_assert(i->c2 == j->c2);
246     gras_assert(i->l1 == j->l1);
247     gras_assert(i->l2 == j->l2);
248     free(j);
249   }
250   free(i);
251   return no_error;
252 }
253
254 /***
255  *** nested struct
256  ***/ 
257 typedef struct {
258   hetestruct hete;
259   homostruct homo;
260 } nestedstruct;
261 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
262   gras_error_t errcode;
263   gras_datadesc_type_t *my_type;
264   nestedstruct *i, *j; 
265
266   INFO0("==== Test on nested structures ====");
267   /* create descriptor */
268   TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
269
270   TRY(gras_datadesc_declare_struct_append(my_type,"hete",
271                                           gras_datadesc_by_name("hetestruct")));
272   TRY(gras_datadesc_declare_struct_append(my_type,"homo",
273                                           gras_datadesc_by_name("homostruct")));
274   TRY(gras_datadesc_declare_ref("nestedstruct*",
275                                 gras_datadesc_by_name("nestedstruct"),
276                                 &my_type));
277
278   /* init a value, exchange it and check its validity*/
279   if (! (i=malloc(sizeof(nestedstruct))) )
280     RAISE_MALLOC;
281   i->homo.a = rand();  i->homo.b = rand();
282   i->homo.c = rand();  i->homo.d = rand();
283   i->hete.c1 = 's'; i->hete.l1 = 123455;
284   i->hete.c2 = 'e'; i->hete.l2 = 774531;
285
286   TRY(write_read(my_type, &i,&j, sock,direction));
287   if (direction == READ || direction == RW) {
288     gras_assert(i->homo.a == j->homo.a);
289     gras_assert(i->homo.b == j->homo.b);
290     gras_assert(i->homo.c == j->homo.c);
291     gras_assert(i->homo.d == j->homo.d);
292     gras_assert(i->hete.c1 == j->hete.c1);
293     gras_assert(i->hete.c2 == j->hete.c2);
294     gras_assert(i->hete.l1 == j->hete.l1);
295     gras_assert(i->hete.l2 == j->hete.l2);
296     free(j);
297   }
298   free(i);
299   return no_error;
300 }
301
302 /***
303  *** chained list
304  ***/ 
305 typedef struct s_chained_list chained_list_t;
306 struct s_chained_list {
307   int          v;
308   chained_list_t *l;
309 };
310 gras_error_t declare_chained_list_type(void);
311 chained_list_t *cons(int v, chained_list_t *l);
312 void list_free(chained_list_t *l);
313 int list_eq(chained_list_t*i,chained_list_t*j);
314
315 gras_error_t declare_chained_list_type(void) {
316   gras_error_t errcode;
317   gras_datadesc_type_t *my_type,*ref_my_type;
318
319   TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
320   TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
321
322   TRY(gras_datadesc_declare_struct_append(my_type,"v",
323                                           gras_datadesc_by_name("int")));
324   TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
325
326   return no_error;
327 }
328
329 chained_list_t * cons(int v, chained_list_t *l) {
330   chained_list_t *nl = malloc(sizeof (chained_list_t));
331   
332   nl->v = v;
333   nl->l = l;
334   
335   return nl;
336 }
337 void list_free(chained_list_t*l) {
338   if (l) {
339     list_free(l->l);
340     free(l);
341   }
342 }
343 int list_eq(chained_list_t*i,chained_list_t*j) {
344   if (!i || !j) return i == j;
345   if (i->v != j->v)
346     return 0;
347   return list_eq(i->l, j->l); 
348 }
349 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
350   gras_error_t errcode;
351   chained_list_t *i, *j; 
352
353   INFO0("==== Test on chained list ====");
354
355   /* init a value, exchange it and check its validity*/
356   i = cons( rand(), cons( rand() , cons( rand(), NULL)));
357   j = NULL;
358
359   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
360                  &i,&j,
361                  sock,direction));
362   if (direction == READ || direction == RW) {
363     gras_assert(list_eq(i,j));    
364     list_free(j);
365   }
366
367   list_free(i);
368   return no_error;
369 }
370 /***
371  *** graph
372  ***/
373 gras_error_t test_graph(gras_socket_t *sock, int direction) {
374   gras_error_t errcode;
375   chained_list_t *i, *j; 
376
377   INFO0("==== Test on graph (cyclique chained list) ====");
378   /* init a value, exchange it and check its validity*/
379   i = cons( rand(), cons( rand() , cons( rand(), NULL)));
380   i->l->l->l = i;
381   j = NULL;
382
383   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
384                  &i,&j, sock,direction));
385   if (direction == READ || direction == RW) {
386     
387     DEBUG1("i=%p"         ,i);
388     DEBUG1("i->l=%p"      ,i->l);
389     DEBUG1("i->l->l=%p"   ,i->l->l);
390     DEBUG1("i->l->l->l=%p",i->l->l->l);
391     DEBUG1("j=%p"         ,j);
392     DEBUG1("j->l=%p"      ,j->l);
393     DEBUG1("j->l->l=%p"   ,j->l->l);
394     DEBUG1("j->l->l->l=%p",j->l->l->l);
395     gras_assert4(j->l->l->l == j,
396                  "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
397                  "j=%p; &j=%p",
398                  j,j->l->l->l, 
399                  j ,&j);
400     i->l->l->l = NULL;
401     j->l->l->l = NULL;
402     gras_assert(list_eq(i,j));
403
404     list_free(j);
405   }
406   list_free(i);
407   return no_error;
408 }
409
410 /**** PBIO *****/
411 typedef struct { /* structure presented in the IEEE article */
412   int Cnstatv;
413   double Cstatev[12];
414   int Cnprops;
415   double Cprops[110];
416   int Cndi[4], Cnshr, Cnpt;
417   double Cdtime, Ctime[2];
418   int Cntens;
419   double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
420 } KSdata1;
421
422 int main(int argc,char *argv[]) {
423   gras_error_t errcode;
424   gras_socket_t *sock;
425   int direction = RW; // READ; // WRITE
426
427   gras_init_defaultlog(&argc,argv,
428                        "DataDesc.thresh=verbose"
429                        " test.thresh=debug"
430                        //" set.thresh=debug"
431                        );
432   if (argc >= 2) {
433     if (!strcmp(argv[1], "--read"))
434       direction = READ;
435     if (!strcmp(argv[1], "--write"))
436       direction = WRITE;
437   }
438     
439   if (direction == WRITE) 
440     TRYFAIL(gras_socket_client_from_file("datadesc_usage.out",&sock));
441   if (direction == READ) 
442     TRYFAIL(gras_socket_server_from_file("datadesc_usage.out",&sock));
443
444   TRYFAIL(test_int(sock,direction));    
445   TRYFAIL(test_float(sock,direction));  
446   TRYFAIL(test_array(sock,direction));  
447   TRYFAIL(test_intref(sock,direction)); 
448
449   TRYFAIL(test_string(sock,direction)); 
450
451   TRYFAIL(test_homostruct(sock,direction));
452   TRYFAIL(test_hetestruct(sock,direction));
453   TRYFAIL(test_nestedstruct(sock,direction));
454
455   TRYFAIL(declare_chained_list_type());
456   TRYFAIL(test_chain_list(sock,direction));
457   TRYFAIL(test_graph(sock,direction));
458
459   if (direction != RW) 
460     gras_socket_close(&sock);
461   gras_exit();
462   return 0;
463 }