Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a322d80c11228b0106f2a410c28fbc40695515c8
[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,j;
96   int cpt;
97
98   INFO0("==== Test on fixed array ====");
99   for (cpt=0; cpt<SIZE; cpt++) {
100     i[cpt] = rand();
101   }
102
103   TRY(gras_datadesc_declare_array_fixed("fixed int array", 
104                                         gras_datadesc_by_name("int"),
105                                         SIZE, &my_type));
106
107   TRY(write_read(my_type, &i,&j, sock,direction));
108   if (direction == READ || direction == RW) {
109     for (cpt=0; cpt<SIZE; cpt++) {
110       DEBUG1("Test spot %d",cpt);
111       gras_assert4(i[cpt] == j[cpt],"i[%d]=%d  !=  j[%d]=%d",
112                    cpt,i[cpt],cpt,j[cpt]);
113     }
114   }
115   return no_error;
116 }
117 gras_error_t test_intref(gras_socket_t *sock, int direction) {
118   gras_error_t errcode;
119   gras_datadesc_type_t *my_type;
120   int *i,*j;
121   
122   if (! (i=malloc(sizeof(int))) )
123     RAISE_MALLOC;
124   *i=12345;
125
126   INFO1("==== Test on a reference to an integer (%p) ====",i);
127
128   TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
129
130   TRY(write_read(my_type, &i,&j, sock,direction));
131   if (direction == READ || direction == RW) {
132     gras_assert(*i == *j);
133     free(j);
134   }
135   free(i);
136   return no_error;
137 }
138
139 /***
140  *** string (dynamic array)
141  ***/ 
142 gras_error_t test_string(gras_socket_t *sock, int direction) {
143   gras_error_t errcode;
144   char *i=strdup("Some data"), *j=NULL;
145   int cpt;
146   
147   INFO0("==== Test on string (ref to dynamic array) ====");
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       gras_assert4(i[cpt] == j[cpt],"i[%d]=%c  !=  j[%d]=%c",
153                    cpt,i[cpt],cpt,j[cpt]);
154     } 
155     free(j);
156   }
157   free(i);
158   return no_error;
159 }
160
161
162 /***
163  *** homogeneous struct
164  ***/ 
165 typedef struct {
166   int a,b,c,d;
167 } homostruct;
168 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
169   gras_error_t errcode;
170   gras_datadesc_type_t *my_type;
171   homostruct *i, *j; 
172
173   INFO0("==== Test on homogeneous structure ====");
174   /* create descriptor */
175   TRY(gras_datadesc_declare_struct("homostruct",&my_type));
176   TRY(gras_datadesc_declare_struct_append(my_type,"a",
177                                           gras_datadesc_by_name("signed int")));
178   TRY(gras_datadesc_declare_struct_append(my_type,"b",
179                                           gras_datadesc_by_name("int")));
180   TRY(gras_datadesc_declare_struct_append(my_type,"c",
181                                           gras_datadesc_by_name("int")));
182   TRY(gras_datadesc_declare_struct_append(my_type,"d",
183                                           gras_datadesc_by_name("int")));
184   TRY(gras_datadesc_declare_ref("homostruct*",
185                                 gras_datadesc_by_name("homostruct"),
186                                 &my_type));
187
188   /* init a value, exchange it and check its validity*/
189   if (! (i=malloc(sizeof(homostruct))) )
190     RAISE_MALLOC;
191   i->a = rand();  i->b = rand();
192   i->c = rand();  i->d = rand();
193
194   TRY(write_read(my_type, &i,&j, sock,direction));
195   if (direction == READ || direction == RW) {
196     gras_assert(i->a == j->a);
197     gras_assert(i->b == j->b);
198     gras_assert(i->c == j->c);
199     gras_assert(i->d == j->d);
200     free(j);
201   }
202   free(i);
203   return no_error;
204 }
205
206 /***
207  *** heterogeneous struct
208  ***/ 
209 typedef struct {
210   unsigned char c1;
211   unsigned long int l1;
212   unsigned char c2;
213   unsigned long int l2;
214 } hetestruct;
215 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
216   gras_error_t errcode;
217   gras_datadesc_type_t *my_type;
218   hetestruct *i, *j; 
219
220   INFO0("==== Test on heterogeneous structure ====");
221   /* create descriptor */
222   TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
223   TRY(gras_datadesc_declare_struct_append(my_type,"c1",
224                                           gras_datadesc_by_name("unsigned char")));
225   TRY(gras_datadesc_declare_struct_append(my_type,"l1",
226                                           gras_datadesc_by_name("unsigned long int")));
227   TRY(gras_datadesc_declare_struct_append(my_type,"c2",
228                                           gras_datadesc_by_name("unsigned char")));
229   TRY(gras_datadesc_declare_struct_append(my_type,"l2",
230                                           gras_datadesc_by_name("unsigned long int")));
231   TRY(gras_datadesc_declare_ref("hetestruct*",
232                                 gras_datadesc_by_name("hetestruct"),
233                                 &my_type));
234
235   /* init a value, exchange it and check its validity*/
236   if (! (i=malloc(sizeof(hetestruct))) )
237     RAISE_MALLOC;
238   i->c1 = 's'; i->l1 = 123455;
239   i->c2 = 'e'; i->l2 = 774531;
240
241   TRY(write_read(my_type, &i,&j, sock,direction));
242   if (direction == READ || direction == RW) {
243     gras_assert(i->c1 == j->c1);
244     gras_assert(i->c2 == j->c2);
245     gras_assert(i->l1 == j->l1);
246     gras_assert(i->l2 == j->l2);
247     free(j);
248   }
249   free(i);
250   return no_error;
251 }
252
253 /***
254  *** nested struct
255  ***/ 
256 typedef struct {
257   hetestruct hete;
258   homostruct homo;
259 } nestedstruct;
260 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
261   gras_error_t errcode;
262   gras_datadesc_type_t *my_type;
263   nestedstruct *i, *j; 
264
265   INFO0("==== Test on nested structures ====");
266   /* create descriptor */
267   TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
268
269   TRY(gras_datadesc_declare_struct_append(my_type,"hete",
270                                           gras_datadesc_by_name("hetestruct")));
271   TRY(gras_datadesc_declare_struct_append(my_type,"homo",
272                                           gras_datadesc_by_name("homostruct")));
273   TRY(gras_datadesc_declare_ref("nestedstruct*",
274                                 gras_datadesc_by_name("nestedstruct"),
275                                 &my_type));
276
277   /* init a value, exchange it and check its validity*/
278   if (! (i=malloc(sizeof(nestedstruct))) )
279     RAISE_MALLOC;
280   i->homo.a = rand();  i->homo.b = rand();
281   i->homo.c = rand();  i->homo.d = rand();
282   i->hete.c1 = 's'; i->hete.l1 = 123455;
283   i->hete.c2 = 'e'; i->hete.l2 = 774531;
284
285   TRY(write_read(my_type, &i,&j, sock,direction));
286   if (direction == READ || direction == RW) {
287     gras_assert(i->homo.a == j->homo.a);
288     gras_assert(i->homo.b == j->homo.b);
289     gras_assert(i->homo.c == j->homo.c);
290     gras_assert(i->homo.d == j->homo.d);
291     gras_assert(i->hete.c1 == j->hete.c1);
292     gras_assert(i->hete.c2 == j->hete.c2);
293     gras_assert(i->hete.l1 == j->hete.l1);
294     gras_assert(i->hete.l2 == j->hete.l2);
295     free(j);
296   }
297   free(i);
298   return no_error;
299 }
300
301 /***
302  *** chained list
303  ***/ 
304 typedef struct s_chained_list chained_list_t;
305 struct s_chained_list {
306   int          v;
307   chained_list_t *l;
308 };
309 gras_error_t declare_chained_list_type(void);
310 chained_list_t *cons(int v, chained_list_t *l);
311 void list_free(chained_list_t *l);
312 int list_eq(chained_list_t*i,chained_list_t*j);
313
314 gras_error_t declare_chained_list_type(void) {
315   gras_error_t errcode;
316   gras_datadesc_type_t *my_type,*ref_my_type;
317
318   TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
319   TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
320
321   TRY(gras_datadesc_declare_struct_append(my_type,"v",
322                                           gras_datadesc_by_name("int")));
323   TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
324
325   return no_error;
326 }
327
328 chained_list_t * cons(int v, chained_list_t *l) {
329   chained_list_t *nl = malloc(sizeof (chained_list_t));
330   
331   nl->v = v;
332   nl->l = l;
333   
334   return nl;
335 }
336 void list_free(chained_list_t*l) {
337   if (l) {
338     list_free(l->l);
339     free(l);
340   }
341 }
342 int list_eq(chained_list_t*i,chained_list_t*j) {
343   if (!i || !j) return i == j;
344   if (i->v != j->v)
345     return 0;
346   return list_eq(i->l, j->l); 
347 }
348 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
349   gras_error_t errcode;
350   chained_list_t *i, *j; 
351
352   INFO0("==== Test on chained list ====");
353
354   /* init a value, exchange it and check its validity*/
355   i = cons( rand(), cons( rand() , cons( rand(), NULL)));
356   j = NULL;
357
358   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
359                  &i,&j,
360                  sock,direction));
361   if (direction == READ || direction == RW) {
362     gras_assert(list_eq(i,j));    
363     list_free(j);
364   }
365
366   list_free(i);
367   return no_error;
368 }
369 /***
370  *** graph
371  ***/
372 gras_error_t test_graph(gras_socket_t *sock, int direction) {
373   gras_error_t errcode;
374   chained_list_t *i, *j; 
375
376   INFO0("==== Test on graph (cyclique chained list) ====");
377   /* init a value, exchange it and check its validity*/
378   i = cons( rand(), cons( rand() , cons( rand(), NULL)));
379   i->l->l->l = i;
380   j = NULL;
381
382   TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
383                  &i,&j, sock,direction));
384   if (direction == READ || direction == RW) {
385     
386     DEBUG1("i=%p"         ,i);
387     DEBUG1("i->l=%p"      ,i->l);
388     DEBUG1("i->l->l=%p"   ,i->l->l);
389     DEBUG1("i->l->l->l=%p",i->l->l->l);
390     DEBUG1("j=%p"         ,j);
391     DEBUG1("j->l=%p"      ,j->l);
392     DEBUG1("j->l->l=%p"   ,j->l->l);
393     DEBUG1("j->l->l->l=%p",j->l->l->l);
394     gras_assert4(j->l->l->l == j,
395                  "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
396                  "j=%p; &j=%p",
397                  j,j->l->l->l, 
398                  j ,&j);
399     j->l->l->l = NULL;
400     i->l->l->l = NULL;
401     gras_assert(list_eq(i,j));
402
403     list_free(j);
404   }
405   i->l->l->l = NULL; /* do this even in READ mode */
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;
426   int cpt;
427   char r_arch_char = gras_arch_selfid();
428
429   gras_init_defaultlog(&argc,argv,NULL);
430
431   for (cpt=1; cpt<argc; cpt++) {
432     if (!strcmp(argv[cpt], "--read")) {
433       direction = READ;
434     } else if (!strcmp(argv[cpt], "--write")) {
435       direction = WRITE;
436     } else {
437        filename=argv[cpt];
438     }
439   }
440     
441   if (direction == WRITE) {
442     TRYFAIL(gras_socket_client_from_file(filename,&sock));
443     TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
444                            &r_arch_char));
445   }
446   if (direction == READ) {
447     TRYFAIL(gras_socket_server_from_file(filename,&sock));
448     TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
449                            gras_arch_selfid(), &r_arch_char));
450     INFO2("This datafile was generated on %s (%d)",
451           gras_datadesc_arch_name(r_arch_char),(int)r_arch_char);
452   }
453   r_arch = (int)r_arch_char;
454   
455   TRYFAIL(test_int(sock,direction));    
456   TRYFAIL(test_float(sock,direction));  
457   TRYFAIL(test_array(sock,direction));  
458   TRYFAIL(test_intref(sock,direction)); 
459   
460   TRYFAIL(test_string(sock,direction)); 
461
462   TRYFAIL(test_homostruct(sock,direction));
463   TRYFAIL(test_hetestruct(sock,direction));
464   TRYFAIL(test_nestedstruct(sock,direction));
465
466   TRYFAIL(declare_chained_list_type());
467   TRYFAIL(test_chain_list(sock,direction));
468   TRYFAIL(test_graph(sock,direction));
469
470   if (direction != RW) 
471     gras_socket_close(sock);
472   gras_exit();
473   return 0;
474 }
475