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 "gras/DataDesc/datadesc_interface.h"
15 GRAS_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this 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);
68 gras_error_t test_clause(gras_socket_t *sock, int direction);
70 /* defined in datadesc_structures.c, which in perl generated */
71 gras_error_t test_structures(gras_socket_t *sock, int direction);
75 gras_error_t test_int(gras_socket_t *sock, int direction) {
79 INFO0("---- Test on integer ----");
80 TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
81 if (direction == READ || direction == RW) {
86 gras_error_t test_float(gras_socket_t *sock, int direction) {
90 INFO0("---- Test on float ----");
91 TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
92 if (direction == READ || direction == RW) {
93 gras_assert2(i == j,"%f != %f",i,j);
97 gras_error_t test_double(gras_socket_t *sock, int direction) {
99 double i=-3252355.1234,j;
101 INFO0("---- Test on double ----");
102 TRY(write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction));
103 if (direction == READ || direction == RW) {
104 gras_assert2(i == j,"%f != %f",i,j);
110 typedef int array[SIZE];
111 gras_error_t test_array(gras_socket_t *sock, int direction) {
112 gras_error_t errcode;
113 gras_datadesc_type_t *my_type;
115 array i = { 35212,-6226,74337,11414,7733};
119 INFO0("---- Test on fixed array ----");
121 TRY(gras_datadesc_array_fixed("fixed int array",
122 gras_datadesc_by_name("int"),
125 TRY(write_read(my_type, &i,&j, sock,direction));
126 if (direction == READ || direction == RW) {
127 for (cpt=0; cpt<SIZE; cpt++) {
128 DEBUG1("Test spot %d",cpt);
129 gras_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
130 cpt,i[cpt],cpt,j[cpt]);
135 gras_error_t test_intref(gras_socket_t *sock, int direction) {
136 gras_error_t errcode;
137 gras_datadesc_type_t *my_type;
140 if (! (i=gras_new(int,1)) )
144 INFO1("---- Test on a reference to an integer (%p) ----",i);
146 TRY(gras_datadesc_ref("int*",gras_datadesc_by_name("int"),&my_type));
148 TRY(write_read(my_type, &i,&j, sock,direction));
149 if (direction == READ || direction == RW) {
150 gras_assert(*i == *j);
158 *** string (dynamic array)
160 gras_error_t test_string(gras_socket_t *sock, int direction) {
161 gras_error_t errcode;
162 char *i=strdup("Some data"), *j=NULL;
165 INFO0("---- Test on string (ref to dynamic array) ----");
166 TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
168 if (direction == READ || direction == RW) {
169 for (cpt=0; cpt<strlen(i); cpt++) {
170 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
171 cpt,i[cpt],cpt,j[cpt]);
181 *** homogeneous struct
186 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
187 gras_error_t errcode;
188 gras_datadesc_type_t *my_type;
191 INFO0("---- Test on homogeneous structure ----");
192 /* create descriptor */
193 TRY(gras_datadesc_struct("homostruct",&my_type));
194 TRY(gras_datadesc_struct_append(my_type,"a",
195 gras_datadesc_by_name("signed int")));
196 TRY(gras_datadesc_struct_append(my_type,"b",
197 gras_datadesc_by_name("int")));
198 TRY(gras_datadesc_struct_append(my_type,"c",
199 gras_datadesc_by_name("int")));
200 TRY(gras_datadesc_struct_append(my_type,"d",
201 gras_datadesc_by_name("int")));
202 gras_datadesc_struct_close(my_type);
203 TRY(gras_datadesc_ref("homostruct*",
204 gras_datadesc_by_name("homostruct"),
207 /* init a value, exchange it and check its validity*/
208 if (! (i=gras_new(homostruct,1)) )
210 i->a = 2235; i->b = 433425;
211 i->c = -23423; i->d = -235235;
213 TRY(write_read(my_type, &i,&j, sock,direction));
214 if (direction == READ || direction == RW) {
215 gras_assert(i->a == j->a);
216 gras_assert(i->b == j->b);
217 gras_assert(i->c == j->c);
218 gras_assert(i->d == j->d);
226 *** heterogeneous struct
230 unsigned long int l1;
232 unsigned long int l2;
234 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
235 gras_error_t errcode;
236 gras_datadesc_type_t *my_type;
239 INFO0("---- Test on heterogeneous structure ----");
240 /* create descriptor */
241 TRY(gras_datadesc_struct("hetestruct",&my_type));
242 TRY(gras_datadesc_struct_append(my_type,"c1",
243 gras_datadesc_by_name("unsigned char")));
244 TRY(gras_datadesc_struct_append(my_type,"l1",
245 gras_datadesc_by_name("unsigned long int")));
246 TRY(gras_datadesc_struct_append(my_type,"c2",
247 gras_datadesc_by_name("unsigned char")));
248 TRY(gras_datadesc_struct_append(my_type,"l2",
249 gras_datadesc_by_name("unsigned long int")));
250 gras_datadesc_struct_close(my_type);
251 TRY(gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"),
254 /* init a value, exchange it and check its validity*/
255 if (! (i=gras_new(hetestruct,1)) )
257 i->c1 = 's'; i->l1 = 123455;
258 i->c2 = 'e'; i->l2 = 774531;
260 TRY(write_read(my_type, &i,&j, sock,direction));
261 if (direction == READ || direction == RW) {
262 gras_assert(i->c1 == j->c1);
263 gras_assert(i->c2 == j->c2);
264 gras_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
265 gras_assert(i->l2 == j->l2);
279 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
280 gras_error_t errcode;
281 gras_datadesc_type_t *my_type;
284 INFO0("---- Test on nested structures ----");
285 /* create descriptor */
286 TRY(gras_datadesc_struct("nestedstruct",&my_type));
288 TRY(gras_datadesc_struct_append(my_type,"hete",
289 gras_datadesc_by_name("hetestruct")));
290 TRY(gras_datadesc_struct_append(my_type,"homo",
291 gras_datadesc_by_name("homostruct")));
292 gras_datadesc_struct_close(my_type);
293 TRY(gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"),
296 /* init a value, exchange it and check its validity*/
297 if (! (i=gras_new(nestedstruct,1)) )
299 i->homo.a = 235231; i->homo.b = -124151;
300 i->homo.c = 211551; i->homo.d = -664222;
301 i->hete.c1 = 's'; i->hete.l1 = 123455;
302 i->hete.c2 = 'e'; i->hete.l2 = 774531;
304 TRY(write_read(my_type, &i,&j, sock,direction));
305 if (direction == READ || direction == RW) {
306 gras_assert(i->homo.a == j->homo.a);
307 gras_assert(i->homo.b == j->homo.b);
308 gras_assert(i->homo.c == j->homo.c);
309 gras_assert(i->homo.d == j->homo.d);
310 gras_assert(i->hete.c1 == j->hete.c1);
311 gras_assert(i->hete.c2 == j->hete.c2);
312 gras_assert(i->hete.l1 == j->hete.l1);
313 gras_assert(i->hete.l2 == j->hete.l2);
323 typedef struct s_chained_list chained_list_t;
324 struct s_chained_list {
328 gras_error_t declare_chained_list_type(void);
329 chained_list_t *cons(int v, chained_list_t *l);
330 void list_free(chained_list_t *l);
331 int list_eq(chained_list_t*i,chained_list_t*j);
333 gras_error_t declare_chained_list_type(void) {
334 gras_error_t errcode;
335 gras_datadesc_type_t *my_type,*ref_my_type;
337 TRY(gras_datadesc_struct("chained_list_t",&my_type));
338 TRY(gras_datadesc_ref("chained_list_t*",my_type,&ref_my_type));
340 TRY(gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int")));
341 TRY(gras_datadesc_struct_append(my_type,"l", ref_my_type));
342 gras_datadesc_struct_close(my_type);
347 chained_list_t * cons(int v, chained_list_t *l) {
348 chained_list_t *nl = gras_new(chained_list_t,1);
355 void list_free(chained_list_t*l) {
361 int list_eq(chained_list_t*i,chained_list_t*j) {
362 if (!i || !j) return i == j;
365 return list_eq(i->l, j->l);
367 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
368 gras_error_t errcode;
369 chained_list_t *i, *j;
371 INFO0("---- Test on chained list ----");
373 /* init a value, exchange it and check its validity*/
374 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
377 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
380 if (direction == READ || direction == RW) {
381 gras_assert(list_eq(i,j));
391 gras_error_t test_graph(gras_socket_t *sock, int direction) {
392 gras_error_t errcode;
393 chained_list_t *i, *j;
395 INFO0("---- Test on graph (cyclique chained list) ----");
396 /* init a value, exchange it and check its validity*/
397 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
401 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
402 &i,&j, sock,direction));
403 if (direction == READ || direction == RW) {
406 DEBUG1("i->l=%p" ,i->l);
407 DEBUG1("i->l->l=%p" ,i->l->l);
408 DEBUG1("i->l->l->l=%p",i->l->l->l);
410 DEBUG1("j->l=%p" ,j->l);
411 DEBUG1("j->l->l=%p" ,j->l->l);
412 DEBUG1("j->l->l->l=%p",j->l->l->l);
413 gras_assert4(j->l->l->l == j,
414 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
420 gras_assert(list_eq(i,j));
424 i->l->l->l = NULL; /* do this even in WRITE mode */
430 GRAS_DEFINE_TYPE(s_pbio,
431 struct s_pbio{ /* structure presented in the IEEE article */
442 double Cdfgrd0[373][3];
443 double Cdfgrd1[3][3];
445 double Cddsdde[106][106];
448 typedef struct s_pbio pbio_t;
450 gras_error_t test_pbio(gras_socket_t *sock, int direction) {
451 gras_error_t errcode;
455 gras_datadesc_type_t *pbio_type;
457 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
458 pbio_type = gras_datadesc_by_symbol(s_pbio);
460 /* Fill in that damn struct */
462 for (cpt=0; cpt<12; cpt++)
463 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
465 for (cpt=0; cpt<110; cpt++)
466 i.Cprops[cpt] = cpt * 100.0;
467 for (cpt=0; cpt<4; cpt++)
468 i.Cndi[cpt] = cpt * 23262;
471 i.Cdtime = -11515.662;
472 i.Ctime[0] = 332523.226;
473 i.Ctime[1] = -26216.113;
474 i.Cntens = 235211411;
476 for (cpt=0; cpt<3; cpt++) {
477 for (cpt2=0; cpt2<373; cpt2++)
478 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
479 for (cpt2=0; cpt2<3; cpt2++)
480 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
482 for (cpt=0; cpt<106; cpt++) {
483 i.Cstress[cpt]=(double)cpt * 22.113;
484 for (cpt2=0; cpt2<106; cpt2++)
485 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
487 TRY(write_read(gras_datadesc_by_symbol(s_pbio),
488 &i,&j, sock,direction));
489 if (direction == READ || direction == RW) {
490 /* Check that the data match */
491 gras_assert(i.Cnstatv == j.Cnstatv);
492 for (cpt=0; cpt<12; cpt++)
493 gras_assert(i.Cstatev[cpt] == j.Cstatev[cpt]);
494 gras_assert(i.Cnprops == j.Cnprops);
495 for (cpt=0; cpt<110; cpt++)
496 gras_assert(i.Cprops[cpt] == j.Cprops[cpt]);
497 for (cpt=0; cpt<4; cpt++)
498 gras_assert(i.Cndi[cpt] == j.Cndi[cpt]);
499 gras_assert(i.Cnshr == j.Cnshr);
500 gras_assert(i.Cnpt == j.Cnpt);
501 gras_assert(i.Cdtime == j.Cdtime);
502 gras_assert(i.Ctime[0] == j.Ctime[0]);
503 gras_assert(i.Ctime[1] == j.Ctime[1]);
504 gras_assert(i.Cntens == j.Cntens);
505 for (cpt=0; cpt<3; cpt++) {
506 for (cpt2=0; cpt2<373; cpt2++)
507 gras_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
508 for (cpt2=0; cpt2<3; cpt2++)
509 gras_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
511 for (cpt=0; cpt<106; cpt++) {
512 gras_assert(i.Cstress[cpt] == j.Cstress[cpt]);
513 for (cpt2=0; cpt2<106; cpt2++)
514 gras_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
515 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
516 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
523 GRAS_DEFINE_TYPE(s_clause,
526 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
528 typedef struct s_clause Clause;
530 gras_error_t test_clause(gras_socket_t *sock, int direction) {
531 gras_error_t errcode;
532 gras_datadesc_type_t *ddt,*array_t;
536 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
538 /* create and fill the struct */
539 if (! (i=gras_new(Clause,1)) )
543 if (! (i->literals = gras_new(int, i->num_lits)) )
545 for (cpt=0; cpt<i->num_lits; cpt++)
546 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
547 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
548 DEBUG1("created count=%d",i->num_lits);
550 /* create the damn type descriptor */
551 ddt = gras_datadesc_by_symbol(s_clause);
552 // gras_datadesc_type_dump(ddt);
554 TRYFAIL(gras_datadesc_ref("Clause*",ddt,&ddt));
556 TRY(write_read(ddt, &i,&j, sock,direction));
557 if (direction == READ || direction == RW) {
558 gras_assert(i->num_lits == j->num_lits);
559 for (cpt=0; cpt<i->num_lits; cpt++)
560 gras_assert(i->literals[cpt] == j->literals[cpt]);
562 gras_free(j->literals);
565 gras_free(i->literals);
570 int main(int argc,char *argv[]) {
571 gras_error_t errcode;
575 char r_arch_char = gras_arch_selfid();
577 gras_init_defaultlog(&argc,argv,NULL);
579 for (cpt=1; cpt<argc; cpt++) {
580 if (!strcmp(argv[cpt], "--read")) {
582 } else if (!strcmp(argv[cpt], "--write")) {
589 if (direction == WRITE) {
590 TRYFAIL(gras_socket_client_from_file(filename,&sock));
591 TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
594 if (direction == READ) {
595 TRYFAIL(gras_socket_server_from_file(filename,&sock));
596 TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
597 gras_arch_selfid(), &r_arch_char));
598 INFO3("This datafile was generated on %s (%d), I'm %s.",
599 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
600 gras_datadesc_arch_name(gras_arch_selfid()));
602 r_arch = (int)r_arch_char;
604 TRYFAIL(test_int(sock,direction));
605 TRYFAIL(test_float(sock,direction));
606 TRYFAIL(test_double(sock,direction));
607 TRYFAIL(test_array(sock,direction));
608 TRYFAIL(test_intref(sock,direction));
610 TRYFAIL(test_string(sock,direction));
612 TRYFAIL(test_structures(sock,direction));
614 TRYFAIL(test_homostruct(sock,direction));
615 TRYFAIL(test_hetestruct(sock,direction));
616 TRYFAIL(test_nestedstruct(sock,direction));
618 TRYFAIL(declare_chained_list_type());
619 TRYFAIL(test_chain_list(sock,direction));
620 // TRYFAIL(test_graph(sock,direction));
622 TRYFAIL(test_pbio(sock,direction));
624 TRYFAIL(test_clause(sock,direction));
627 gras_socket_close(sock);