3 /* datadesc: test of data description (using file transport). */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 the OURAGAN project. */
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. */
14 #include "../DataDesc/datadesc_interface.h"
15 GRAS_LOG_NEW_DEFAULT_CATEGORY(test);
18 write_read(gras_datadesc_type_t *type,void *src, void *dst);
21 write_read(gras_datadesc_type_t *type,void *src, void *dst) {
26 TRY(gras_socket_client_from_file("datadesc_usage.out",&sock));
27 TRY(gras_datadesc_send(sock, type, src));
28 gras_socket_close(&sock);
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);
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);
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);
50 gras_error_t test_int(void) {
52 gras_datadesc_type_t *type;
55 INFO0("==== Test on integer ====");
56 TRY(gras_datadesc_by_name("int", &type));
57 TRY(write_read(type, (void*)&i,(void**) &j));
62 gras_error_t test_float(void) {
64 gras_datadesc_type_t *type;
67 INFO0("==== Test on float ====");
68 TRY(gras_datadesc_by_name("float", &type));
69 TRY(write_read(type, (void*)&i,(void**) &j));
76 typedef int array[SIZE];
77 gras_error_t test_array(void) {
79 gras_datadesc_type_t *int_type;
80 gras_datadesc_type_t *my_type;
86 INFO0("==== Test on fixed array ====");
87 for (cpt=0; cpt<SIZE; cpt++) {
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));
96 TRY(write_read(my_type, (void*)&i,(void**) &j));
97 for (cpt=0; cpt<SIZE; cpt++)
98 gras_assert(i[cpt] == (*j)[cpt]);
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;
109 if (! (i=malloc(sizeof(int))) )
112 INFO1("==== Test on a reference to an integer (%p) ====",i);
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));
118 TRY(write_read(my_type, (void*)&i,(void**) &j));
119 gras_assert(*i == **j);
125 *** string (dynamic array)
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");
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));
144 *** homogeneous struct
149 gras_error_t test_homostruct(void) {
150 gras_error_t errcode;
151 gras_datadesc_type_t *my_type;
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));
164 /* init a value, exchange it and check its validity*/
165 if (! (i=malloc(sizeof(homostruct))) )
167 i->a = rand(); i->b = rand();
168 i->c = rand(); i->d = rand();
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);
183 *** heterogeneous struct
187 unsigned long int l1;
189 unsigned long int l2;
191 gras_error_t test_hetestruct(void) {
192 gras_error_t errcode;
193 gras_datadesc_type_t *my_type;
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));
206 /* init a value, exchange it and check its validity*/
207 if (! (i=malloc(sizeof(hetestruct))) )
209 i->c1 = 's'; i->l1 = 123455;
210 i->c2 = 'e'; i->l2 = 774531;
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);
231 gras_error_t test_nestedstruct(void) {
232 gras_error_t errcode;
233 gras_datadesc_type_t *my_type;
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));
244 /* init a value, exchange it and check its validity*/
245 if (! (i=malloc(sizeof(nestedstruct))) )
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;
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);
271 typedef struct s_chained_list chained_list_t;
272 struct s_chained_list {
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));
287 void list_free(chained_list_t*l) {
293 int list_eq(chained_list_t*i,chained_list_t*j) {
294 if (!i || !j) return i == j;
297 return list_eq(i->l, j->l);
299 gras_error_t test_chain_list(void) {
300 gras_error_t errcode;
301 gras_datadesc_type_t *my_type,*ref_my_type;
303 long int ref_my_code;
304 chained_list_t *i, *j;
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));
311 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_code));
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));
316 TRY(gras_datadesc_by_code(ref_my_code, &ref_my_type));
318 /* init a value, exchange it and check its validity*/
319 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
322 TRY(write_read(my_type, (void*)i, (void**)&j));
323 gras_assert(list_eq(i,j));
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;
337 INFO0("==== Test on graph (cyclique chained list) ====");
338 TRY(gras_datadesc_by_name("chained_list_t*", &my_type));
340 /* init a value, exchange it and check its validity*/
341 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
345 TRY(write_read(my_type, (void*)&i, (void**)&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"
357 gras_assert(list_eq(i,j));
365 typedef struct { /* structure presented in the IEEE article */
370 int Cndi[4], Cnshr, Cnpt;
371 double Cdtime, Ctime[2];
373 double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
376 int main(int argc,char *argv[]) {
377 gras_error_t errcode;
379 gras_init_defaultlog(argc,argv,
380 "DataDesc.thresh=verbose"
382 // " set.thresh=debug"
387 TRYFAIL(test_float());
388 TRYFAIL(test_array());
389 TRYFAIL(test_intref());
390 TRYFAIL(test_string());
392 TRYFAIL(test_homostruct());
393 TRYFAIL(test_hetestruct());
394 TRYFAIL(test_nestedstruct());
396 TRYFAIL(test_chain_list());
397 TRYFAIL(test_graph());