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);
22 write_read(gras_datadesc_type_t *type,void *src, void *dst,
23 gras_socket_t *sock, int direction);
26 write_read(gras_datadesc_type_t *type,void *src, void *dst,
27 gras_socket_t *sock, int direction) {
32 TRY(gras_socket_client_from_file("datadesc_usage.out",&sock));
33 if (direction == WRITE || direction == RW)
34 TRY(gras_datadesc_send(sock, type, src));
36 gras_socket_close(&sock);
40 TRY(gras_socket_server_from_file("datadesc_usage.out",&sock));
42 if (direction == READ || direction == RW)
43 TRY(gras_datadesc_recv(sock, type, gras_arch_selfid(), dst));
46 gras_socket_close(&sock);
51 gras_error_t test_int(gras_socket_t *sock, int direction);
52 gras_error_t test_float(gras_socket_t *sock, int direction);
53 gras_error_t test_array(gras_socket_t *sock, int direction);
54 gras_error_t test_intref(gras_socket_t *sock, int direction);
55 gras_error_t test_string(gras_socket_t *sock, int direction);
57 gras_error_t test_homostruct(gras_socket_t *sock, int direction);
58 gras_error_t test_hetestruct(gras_socket_t *sock, int direction);
59 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction);
60 gras_error_t test_chain_list(gras_socket_t *sock, int direction);
61 gras_error_t test_graph(gras_socket_t *sock, int direction);
63 gras_error_t test_int(gras_socket_t *sock, int direction) {
67 INFO0("==== Test on integer ====");
68 TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
69 if (direction == READ || direction == RW) {
74 gras_error_t test_float(gras_socket_t *sock, int direction) {
78 INFO0("==== Test on float ====");
79 TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
80 if (direction == READ || direction == RW) {
87 typedef int array[SIZE];
88 gras_error_t test_array(gras_socket_t *sock, int direction) {
90 gras_datadesc_type_t *my_type;
95 INFO0("==== Test on fixed array ====");
96 for (cpt=0; cpt<SIZE; cpt++) {
100 TRY(gras_datadesc_declare_array_fixed("fixed int array",
101 gras_datadesc_by_name("int"),
104 TRY(write_read(my_type, &i,&j, sock,direction));
105 if (direction == READ || direction == RW) {
106 for (cpt=0; cpt<SIZE; cpt++)
107 gras_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
108 cpt,i[cpt],cpt,j[cpt]);
112 gras_error_t test_intref(gras_socket_t *sock, int direction) {
113 gras_error_t errcode;
114 gras_datadesc_type_t *my_type;
117 if (! (i=malloc(sizeof(int))) )
121 INFO1("==== Test on a reference to an integer (%p) ====",i);
123 TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
125 TRY(write_read(my_type, &i,&j, sock,direction));
126 if (direction == READ || direction == RW) {
127 gras_assert(*i == *j);
135 *** string (dynamic array)
137 gras_error_t test_string(gras_socket_t *sock, int direction) {
138 gras_error_t errcode;
139 char *i=strdup("Some data"), *j=NULL;
142 INFO0("==== Test on string (ref to dynamic array) ====");
143 TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
145 if (direction == READ || direction == RW) {
146 for (cpt=0; cpt<strlen(i); cpt++) {
147 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
148 cpt,i[cpt],cpt,j[cpt]);
158 *** homogeneous struct
163 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
164 gras_error_t errcode;
165 gras_datadesc_type_t *my_type;
168 INFO0("==== Test on homogeneous structure ====");
169 /* create descriptor */
170 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
171 TRY(gras_datadesc_declare_struct_append(my_type,"a",
172 gras_datadesc_by_name("signed int")));
173 TRY(gras_datadesc_declare_struct_append(my_type,"b",
174 gras_datadesc_by_name("int")));
175 TRY(gras_datadesc_declare_struct_append(my_type,"c",
176 gras_datadesc_by_name("int")));
177 TRY(gras_datadesc_declare_struct_append(my_type,"d",
178 gras_datadesc_by_name("int")));
179 TRY(gras_datadesc_declare_ref("homostruct*",
180 gras_datadesc_by_name("homostruct"),
183 /* init a value, exchange it and check its validity*/
184 if (! (i=malloc(sizeof(homostruct))) )
186 i->a = rand(); i->b = rand();
187 i->c = rand(); i->d = rand();
189 TRY(write_read(my_type, &i,&j, sock,direction));
190 if (direction == READ || direction == RW) {
191 gras_assert(i->a == j->a);
192 gras_assert(i->b == j->b);
193 gras_assert(i->c == j->c);
194 gras_assert(i->d == j->d);
202 *** heterogeneous struct
206 unsigned long int l1;
208 unsigned long int l2;
210 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
211 gras_error_t errcode;
212 gras_datadesc_type_t *my_type;
215 INFO0("==== Test on heterogeneous structure ====");
216 /* create descriptor */
217 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
218 TRY(gras_datadesc_declare_struct_append(my_type,"c1",
219 gras_datadesc_by_name("unsigned char")));
220 TRY(gras_datadesc_declare_struct_append(my_type,"l1",
221 gras_datadesc_by_name("unsigned long int")));
222 TRY(gras_datadesc_declare_struct_append(my_type,"c2",
223 gras_datadesc_by_name("unsigned char")));
224 TRY(gras_datadesc_declare_struct_append(my_type,"l2",
225 gras_datadesc_by_name("unsigned long int")));
226 TRY(gras_datadesc_declare_ref("hetestruct*",
227 gras_datadesc_by_name("hetestruct"),
230 /* init a value, exchange it and check its validity*/
231 if (! (i=malloc(sizeof(hetestruct))) )
233 i->c1 = 's'; i->l1 = 123455;
234 i->c2 = 'e'; i->l2 = 774531;
236 TRY(write_read(my_type, &i,&j, sock,direction));
237 if (direction == READ || direction == RW) {
238 gras_assert(i->c1 == j->c1);
239 gras_assert(i->c2 == j->c2);
240 gras_assert(i->l1 == j->l1);
241 gras_assert(i->l2 == j->l2);
255 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
256 gras_error_t errcode;
257 gras_datadesc_type_t *my_type;
260 INFO0("==== Test on nested structures ====");
261 /* create descriptor */
262 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
264 TRY(gras_datadesc_declare_struct_append(my_type,"hete",
265 gras_datadesc_by_name("hetestruct")));
266 TRY(gras_datadesc_declare_struct_append(my_type,"homo",
267 gras_datadesc_by_name("homostruct")));
268 TRY(gras_datadesc_declare_ref("nestedstruct*",
269 gras_datadesc_by_name("nestedstruct"),
272 /* init a value, exchange it and check its validity*/
273 if (! (i=malloc(sizeof(nestedstruct))) )
275 i->homo.a = rand(); i->homo.b = rand();
276 i->homo.c = rand(); i->homo.d = rand();
277 i->hete.c1 = 's'; i->hete.l1 = 123455;
278 i->hete.c2 = 'e'; i->hete.l2 = 774531;
280 TRY(write_read(my_type, &i,&j, sock,direction));
281 if (direction == READ || direction == RW) {
282 gras_assert(i->homo.a == j->homo.a);
283 gras_assert(i->homo.b == j->homo.b);
284 gras_assert(i->homo.c == j->homo.c);
285 gras_assert(i->homo.d == j->homo.d);
286 gras_assert(i->hete.c1 == j->hete.c1);
287 gras_assert(i->hete.c2 == j->hete.c2);
288 gras_assert(i->hete.l1 == j->hete.l1);
289 gras_assert(i->hete.l2 == j->hete.l2);
299 typedef struct s_chained_list chained_list_t;
300 struct s_chained_list {
304 gras_error_t declare_chained_list_type(void);
305 chained_list_t *cons(int v, chained_list_t *l);
306 void list_free(chained_list_t *l);
307 int list_eq(chained_list_t*i,chained_list_t*j);
309 gras_error_t declare_chained_list_type(void) {
310 gras_error_t errcode;
311 gras_datadesc_type_t *my_type,*ref_my_type;
313 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
314 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
316 TRY(gras_datadesc_declare_struct_append(my_type,"v",
317 gras_datadesc_by_name("int")));
318 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
323 chained_list_t * cons(int v, chained_list_t *l) {
324 chained_list_t *nl = malloc(sizeof (chained_list_t));
331 void list_free(chained_list_t*l) {
337 int list_eq(chained_list_t*i,chained_list_t*j) {
338 if (!i || !j) return i == j;
341 return list_eq(i->l, j->l);
343 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
344 gras_error_t errcode;
345 chained_list_t *i, *j;
347 INFO0("==== Test on chained list ====");
349 /* init a value, exchange it and check its validity*/
350 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
353 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
356 if (direction == READ || direction == RW) {
357 gras_assert(list_eq(i,j));
367 gras_error_t test_graph(gras_socket_t *sock, int direction) {
368 gras_error_t errcode;
369 chained_list_t *i, *j;
371 INFO0("==== Test on graph (cyclique chained list) ====");
372 /* init a value, exchange it and check its validity*/
373 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
377 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
378 &i,&j, sock,direction));
379 if (direction == READ || direction == RW) {
382 DEBUG1("i->l=%p" ,i->l);
383 DEBUG1("i->l->l=%p" ,i->l->l);
384 DEBUG1("i->l->l->l=%p",i->l->l->l);
386 DEBUG1("j->l=%p" ,j->l);
387 DEBUG1("j->l->l=%p" ,j->l->l);
388 DEBUG1("j->l->l->l=%p",j->l->l->l);
389 gras_assert4(j->l->l->l == j,
390 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
396 gras_assert(list_eq(i,j));
405 typedef struct { /* structure presented in the IEEE article */
410 int Cndi[4], Cnshr, Cnpt;
411 double Cdtime, Ctime[2];
413 double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
416 int main(int argc,char *argv[]) {
417 gras_error_t errcode;
419 int direction = RW; // READ; // WRITE
421 gras_init_defaultlog(&argc,argv,
422 "DataDesc.thresh=verbose"
424 //" set.thresh=debug"
427 if (!strcmp(argv[1], "--read"))
429 if (!strcmp(argv[1], "--write"))
433 if (direction == WRITE)
434 TRYFAIL(gras_socket_client_from_file("datadesc_usage.out",&sock));
435 if (direction == READ)
436 TRYFAIL(gras_socket_server_from_file("datadesc_usage.out",&sock));
438 TRYFAIL(test_int(sock,direction));
439 TRYFAIL(test_float(sock,direction));
440 TRYFAIL(test_array(sock,direction));
441 TRYFAIL(test_intref(sock,direction));
443 TRYFAIL(test_string(sock,direction));
445 TRYFAIL(test_homostruct(sock,direction));
446 TRYFAIL(test_hetestruct(sock,direction));
447 TRYFAIL(test_nestedstruct(sock,direction));
449 TRYFAIL(declare_chained_list_type());
450 TRYFAIL(test_chain_list(sock,direction));
451 TRYFAIL(test_graph(sock,direction));
454 gras_socket_close(&sock);