Logo AND Algorithmique Numérique Distribuée

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