Logo AND Algorithmique Numérique Distribuée

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