Logo AND Algorithmique Numérique Distribuée

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