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 const char *filename = "datadesc_usage.out";
25 write_read(gras_datadesc_type_t *type,void *src, void *dst,
26 gras_socket_t *sock, int direction);
29 write_read(gras_datadesc_type_t *type,void *src, void *dst,
30 gras_socket_t *sock, int direction) {
35 TRY(gras_socket_client_from_file(filename,&sock));
36 if (direction == WRITE || direction == RW)
37 TRY(gras_datadesc_send(sock, type, src));
39 gras_socket_close(sock);
43 TRY(gras_socket_server_from_file(filename,&sock));
45 if (direction == READ || direction == RW)
46 TRY(gras_datadesc_recv(sock, type, r_arch, dst));
49 gras_socket_close(sock);
54 gras_error_t test_int(gras_socket_t *sock, int direction);
55 gras_error_t test_float(gras_socket_t *sock, int direction);
56 gras_error_t test_double(gras_socket_t *sock, int direction);
57 gras_error_t test_array(gras_socket_t *sock, int direction);
58 gras_error_t test_intref(gras_socket_t *sock, int direction);
59 gras_error_t test_string(gras_socket_t *sock, int direction);
61 gras_error_t test_homostruct(gras_socket_t *sock, int direction);
62 gras_error_t test_hetestruct(gras_socket_t *sock, int direction);
63 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction);
64 gras_error_t test_chain_list(gras_socket_t *sock, int direction);
65 gras_error_t test_graph(gras_socket_t *sock, int direction);
67 gras_error_t test_pbio(gras_socket_t *sock, int direction);
69 gras_error_t test_int(gras_socket_t *sock, int direction) {
73 INFO0("---- Test on integer ----");
74 TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
75 if (direction == READ || direction == RW) {
80 gras_error_t test_float(gras_socket_t *sock, int direction) {
84 INFO0("---- Test on float ----");
85 TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
86 if (direction == READ || direction == RW) {
87 gras_assert2(i == j,"%f != %f",i,j);
91 gras_error_t test_double(gras_socket_t *sock, int direction) {
93 double i=-3252355.1234,j;
95 INFO0("---- Test on double ----");
96 TRY(write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction));
97 if (direction == READ || direction == RW) {
98 gras_assert2(i == j,"%f != %f",i,j);
104 typedef int array[SIZE];
105 gras_error_t test_array(gras_socket_t *sock, int direction) {
106 gras_error_t errcode;
107 gras_datadesc_type_t *my_type;
109 array i = { 35212,-6226,74337,11414,7733};
113 INFO0("---- Test on fixed array ----");
115 TRY(gras_datadesc_declare_array_fixed("fixed int array",
116 gras_datadesc_by_name("int"),
119 TRY(write_read(my_type, &i,&j, sock,direction));
120 if (direction == READ || direction == RW) {
121 for (cpt=0; cpt<SIZE; cpt++) {
122 DEBUG1("Test spot %d",cpt);
123 gras_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
124 cpt,i[cpt],cpt,j[cpt]);
129 gras_error_t test_intref(gras_socket_t *sock, int direction) {
130 gras_error_t errcode;
131 gras_datadesc_type_t *my_type;
134 if (! (i=malloc(sizeof(int))) )
138 INFO1("---- Test on a reference to an integer (%p) ----",i);
140 TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
142 TRY(write_read(my_type, &i,&j, sock,direction));
143 if (direction == READ || direction == RW) {
144 gras_assert(*i == *j);
152 *** string (dynamic array)
154 gras_error_t test_string(gras_socket_t *sock, int direction) {
155 gras_error_t errcode;
156 char *i=strdup("Some data"), *j=NULL;
159 INFO0("---- Test on string (ref to dynamic array) ----");
160 TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
162 if (direction == READ || direction == RW) {
163 for (cpt=0; cpt<strlen(i); cpt++) {
164 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
165 cpt,i[cpt],cpt,j[cpt]);
175 *** homogeneous struct
180 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
181 gras_error_t errcode;
182 gras_datadesc_type_t *my_type;
185 INFO0("---- Test on homogeneous structure ----");
186 /* create descriptor */
187 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
188 TRY(gras_datadesc_declare_struct_append(my_type,"a",
189 gras_datadesc_by_name("signed int")));
190 TRY(gras_datadesc_declare_struct_append(my_type,"b",
191 gras_datadesc_by_name("int")));
192 TRY(gras_datadesc_declare_struct_append(my_type,"c",
193 gras_datadesc_by_name("int")));
194 TRY(gras_datadesc_declare_struct_append(my_type,"d",
195 gras_datadesc_by_name("int")));
196 TRY(gras_datadesc_declare_ref("homostruct*",
197 gras_datadesc_by_name("homostruct"),
200 /* init a value, exchange it and check its validity*/
201 if (! (i=malloc(sizeof(homostruct))) )
203 i->a = 2235; i->b = 433425;
204 i->c = -23423; i->d = -235235;
206 TRY(write_read(my_type, &i,&j, sock,direction));
207 if (direction == READ || direction == RW) {
208 gras_assert(i->a == j->a);
209 gras_assert(i->b == j->b);
210 gras_assert(i->c == j->c);
211 gras_assert(i->d == j->d);
219 *** heterogeneous struct
223 unsigned long int l1;
225 unsigned long int l2;
227 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
228 gras_error_t errcode;
229 gras_datadesc_type_t *my_type;
232 INFO0("---- Test on heterogeneous structure ----");
233 /* create descriptor */
234 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
235 TRY(gras_datadesc_declare_struct_append(my_type,"c1",
236 gras_datadesc_by_name("unsigned char")));
237 TRY(gras_datadesc_declare_struct_append(my_type,"l1",
238 gras_datadesc_by_name("unsigned long int")));
239 TRY(gras_datadesc_declare_struct_append(my_type,"c2",
240 gras_datadesc_by_name("unsigned char")));
241 TRY(gras_datadesc_declare_struct_append(my_type,"l2",
242 gras_datadesc_by_name("unsigned long int")));
243 TRY(gras_datadesc_declare_ref("hetestruct*",
244 gras_datadesc_by_name("hetestruct"),
247 /* init a value, exchange it and check its validity*/
248 if (! (i=malloc(sizeof(hetestruct))) )
250 i->c1 = 's'; i->l1 = 123455;
251 i->c2 = 'e'; i->l2 = 774531;
253 TRY(write_read(my_type, &i,&j, sock,direction));
254 if (direction == READ || direction == RW) {
255 gras_assert(i->c1 == j->c1);
256 gras_assert(i->c2 == j->c2);
257 gras_assert2(i->l1 == j->l1,"i->l1(=%d) != j->l1(=%d)",i->l1,j->l1);
258 gras_assert(i->l2 == j->l2);
272 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
273 gras_error_t errcode;
274 gras_datadesc_type_t *my_type;
277 INFO0("---- Test on nested structures ----");
278 /* create descriptor */
279 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
281 TRY(gras_datadesc_declare_struct_append(my_type,"hete",
282 gras_datadesc_by_name("hetestruct")));
283 TRY(gras_datadesc_declare_struct_append(my_type,"homo",
284 gras_datadesc_by_name("homostruct")));
285 TRY(gras_datadesc_declare_ref("nestedstruct*",
286 gras_datadesc_by_name("nestedstruct"),
289 /* init a value, exchange it and check its validity*/
290 if (! (i=malloc(sizeof(nestedstruct))) )
292 i->homo.a = 235231; i->homo.b = -124151;
293 i->homo.c = 211551; i->homo.d = -664222;
294 i->hete.c1 = 's'; i->hete.l1 = 123455;
295 i->hete.c2 = 'e'; i->hete.l2 = 774531;
297 TRY(write_read(my_type, &i,&j, sock,direction));
298 if (direction == READ || direction == RW) {
299 gras_assert(i->homo.a == j->homo.a);
300 gras_assert(i->homo.b == j->homo.b);
301 gras_assert(i->homo.c == j->homo.c);
302 gras_assert(i->homo.d == j->homo.d);
303 gras_assert(i->hete.c1 == j->hete.c1);
304 gras_assert(i->hete.c2 == j->hete.c2);
305 gras_assert(i->hete.l1 == j->hete.l1);
306 gras_assert(i->hete.l2 == j->hete.l2);
316 typedef struct s_chained_list chained_list_t;
317 struct s_chained_list {
321 gras_error_t declare_chained_list_type(void);
322 chained_list_t *cons(int v, chained_list_t *l);
323 void list_free(chained_list_t *l);
324 int list_eq(chained_list_t*i,chained_list_t*j);
326 gras_error_t declare_chained_list_type(void) {
327 gras_error_t errcode;
328 gras_datadesc_type_t *my_type,*ref_my_type;
330 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
331 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
333 TRY(gras_datadesc_declare_struct_append(my_type,"v",
334 gras_datadesc_by_name("int")));
335 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
340 chained_list_t * cons(int v, chained_list_t *l) {
341 chained_list_t *nl = malloc(sizeof (chained_list_t));
348 void list_free(chained_list_t*l) {
354 int list_eq(chained_list_t*i,chained_list_t*j) {
355 if (!i || !j) return i == j;
358 return list_eq(i->l, j->l);
360 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
361 gras_error_t errcode;
362 chained_list_t *i, *j;
364 INFO0("---- Test on chained list ----");
366 /* init a value, exchange it and check its validity*/
367 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
370 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
373 if (direction == READ || direction == RW) {
374 gras_assert(list_eq(i,j));
384 gras_error_t test_graph(gras_socket_t *sock, int direction) {
385 gras_error_t errcode;
386 chained_list_t *i, *j;
388 INFO0("---- Test on graph (cyclique chained list) ----");
389 /* init a value, exchange it and check its validity*/
390 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
394 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
395 &i,&j, sock,direction));
396 if (direction == READ || direction == RW) {
399 DEBUG1("i->l=%p" ,i->l);
400 DEBUG1("i->l->l=%p" ,i->l->l);
401 DEBUG1("i->l->l->l=%p",i->l->l->l);
403 DEBUG1("j->l=%p" ,j->l);
404 DEBUG1("j->l->l=%p" ,j->l->l);
405 DEBUG1("j->l->l->l=%p",j->l->l->l);
406 gras_assert4(j->l->l->l == j,
407 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
413 gras_assert(list_eq(i,j));
417 i->l->l->l = NULL; /* do this even in WRITE mode */
423 GRAS_DEFINE_TYPE(s_pbio,
424 struct s_pbio{ /* structure presented in the IEEE article */
436 double Cdfgrd0[373][3];
437 double Cdfgrd1[3][3];
439 double Cddsdde[106][106];
443 typedef struct s_pbio pbio_t;
445 gras_error_t test_pbio(gras_socket_t *sock, int direction) {
446 gras_error_t errcode;
450 gras_datadesc_type_t *pbio_type;
452 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
453 pbio_type = gras_datadesc_by_symbol(s_pbio);
454 INFO1("sizof(s_pbio)=%d",sizeof(struct s_pbio));
455 gras_datadesc_type_dump(gras_datadesc_by_name("double[373]"));
456 gras_datadesc_type_dump(pbio_type);
457 /* Fill in that damn struct */
459 for (cpt=0; cpt<12; cpt++)
460 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
462 for (cpt=0; cpt<110; cpt++)
463 i.Cprops[cpt] = cpt * 100.0;
464 for (cpt=0; cpt<4; cpt++)
465 i.Cndi[cpt] = cpt * 23262;
468 i.Cdtime = -11515.662;
469 i.Ctime[0] = 332523.226;
470 i.Ctime[1] = -26216.113;
471 i.Cntens = 235211411;
473 for (cpt=0; cpt<3; cpt++) {
474 for (cpt2=0; cpt2<373; cpt2++)
475 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
476 for (cpt2=0; cpt2<3; cpt2++)
477 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
479 for (cpt=0; cpt<106; cpt++) {
480 i.Cstress[cpt]=(double)cpt * 22.113;
481 for (cpt2=0; cpt2<106; cpt2++)
482 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
484 TRY(write_read(gras_datadesc_by_symbol(s_pbio),
485 &i,&j, sock,direction));
486 if (direction == READ || direction == RW) {
487 /* Check that the data match */
488 gras_assert(i.Cnstatv == j.Cnstatv);
489 for (cpt=0; cpt<12; cpt++)
490 gras_assert(i.Cstatev[cpt] == j.Cstatev[cpt]);
491 gras_assert(i.Cnprops == j.Cnprops);
492 for (cpt=0; cpt<110; cpt++)
493 gras_assert(i.Cprops[cpt] == j.Cprops[cpt]);
494 for (cpt=0; cpt<4; cpt++)
495 gras_assert(i.Cndi[cpt] == j.Cndi[cpt]);
496 gras_assert(i.Cnshr == j.Cnshr);
497 gras_assert(i.Cnpt == j.Cnpt);
498 gras_assert(i.Cdtime == j.Cdtime);
499 gras_assert(i.Ctime[0] == j.Ctime[0]);
500 gras_assert(i.Ctime[1] == j.Ctime[1]);
501 gras_assert(i.Cntens == j.Cntens);
503 for (cpt=0; cpt<3; cpt++) {
504 for (cpt2=0; cpt2<373; cpt2++)
505 gras_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
506 for (cpt2=0; cpt2<3; cpt2++)
507 gras_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
509 for (cpt=0; cpt<106; cpt++) {
510 gras_assert(i.Cstress[cpt] == j.Cstress[cpt]);
511 for (cpt2=0; cpt2<106; cpt2++)
512 gras_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
513 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
514 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
521 int main(int argc,char *argv[]) {
522 gras_error_t errcode;
526 char r_arch_char = gras_arch_selfid();
528 gras_init_defaultlog(&argc,argv,NULL);
530 for (cpt=1; cpt<argc; cpt++) {
531 if (!strcmp(argv[cpt], "--read")) {
533 } else if (!strcmp(argv[cpt], "--write")) {
540 if (direction == WRITE) {
541 TRYFAIL(gras_socket_client_from_file(filename,&sock));
542 TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
545 if (direction == READ) {
546 TRYFAIL(gras_socket_server_from_file(filename,&sock));
547 TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
548 gras_arch_selfid(), &r_arch_char));
549 INFO3("This datafile was generated on %s (%d), I'm %s.",
550 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
551 gras_datadesc_arch_name(gras_arch_selfid()));
553 r_arch = (int)r_arch_char;
556 TRYFAIL(test_int(sock,direction));
557 TRYFAIL(test_float(sock,direction));
558 TRYFAIL(test_double(sock,direction));
559 TRYFAIL(test_array(sock,direction));
560 TRYFAIL(test_intref(sock,direction));
562 TRYFAIL(test_string(sock,direction));
564 TRYFAIL(test_homostruct(sock,direction));
565 TRYFAIL(test_hetestruct(sock,direction));
566 TRYFAIL(test_nestedstruct(sock,direction));
568 TRYFAIL(declare_chained_list_type());
569 TRYFAIL(test_chain_list(sock,direction));
570 TRYFAIL(test_graph(sock,direction));
572 TRYFAIL(test_pbio(sock,direction));
575 gras_socket_close(sock);