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 my_type=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;
143 INFO1("---- Test on a reference to an integer (%p) ----",i);
145 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
147 TRY(write_read(my_type, &i,&j, sock,direction));
148 if (direction == READ || direction == RW) {
149 gras_assert(*i == *j);
157 *** string (dynamic array)
159 gras_error_t test_string(gras_socket_t *sock, int direction) {
160 gras_error_t errcode;
161 char *i=gras_strdup("Some data"), *j=NULL;
164 INFO0("---- Test on string (ref to dynamic array) ----");
165 TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
167 if (direction == READ || direction == RW) {
168 for (cpt=0; cpt<strlen(i); cpt++) {
169 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
170 cpt,i[cpt],cpt,j[cpt]);
180 *** homogeneous struct
185 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
186 gras_error_t errcode;
187 gras_datadesc_type_t *my_type;
190 INFO0("---- Test on homogeneous structure ----");
191 /* create descriptor */
192 my_type=gras_datadesc_struct("homostruct");
193 gras_datadesc_struct_append(my_type,"a",
194 gras_datadesc_by_name("signed int"));
195 gras_datadesc_struct_append(my_type,"b",
196 gras_datadesc_by_name("int"));
197 gras_datadesc_struct_append(my_type,"c",
198 gras_datadesc_by_name("int"));
199 gras_datadesc_struct_append(my_type,"d",
200 gras_datadesc_by_name("int"));
201 gras_datadesc_struct_close(my_type);
202 my_type=gras_datadesc_ref("homostruct*",
203 gras_datadesc_by_name("homostruct"));
205 /* init a value, exchange it and check its validity*/
206 i=gras_new(homostruct,1);
207 i->a = 2235; i->b = 433425;
208 i->c = -23423; i->d = -235235;
210 TRY(write_read(my_type, &i,&j, sock,direction));
211 if (direction == READ || direction == RW) {
212 gras_assert(i->a == j->a);
213 gras_assert(i->b == j->b);
214 gras_assert(i->c == j->c);
215 gras_assert(i->d == j->d);
223 *** heterogeneous struct
227 unsigned long int l1;
229 unsigned long int l2;
231 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
232 gras_error_t errcode;
233 gras_datadesc_type_t *my_type;
236 INFO0("---- Test on heterogeneous structure ----");
237 /* create descriptor */
238 my_type=gras_datadesc_struct("hetestruct");
239 gras_datadesc_struct_append(my_type,"c1",
240 gras_datadesc_by_name("unsigned char"));
241 gras_datadesc_struct_append(my_type,"l1",
242 gras_datadesc_by_name("unsigned long int"));
243 gras_datadesc_struct_append(my_type,"c2",
244 gras_datadesc_by_name("unsigned char"));
245 gras_datadesc_struct_append(my_type,"l2",
246 gras_datadesc_by_name("unsigned long int"));
247 gras_datadesc_struct_close(my_type);
248 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
251 /* init a value, exchange it and check its validity*/
252 i=gras_new(hetestruct,1);
253 i->c1 = 's'; i->l1 = 123455;
254 i->c2 = 'e'; i->l2 = 774531;
256 TRY(write_read(my_type, &i,&j, sock,direction));
257 if (direction == READ || direction == RW) {
258 gras_assert(i->c1 == j->c1);
259 gras_assert(i->c2 == j->c2);
260 gras_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
261 gras_assert(i->l2 == j->l2);
275 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
276 gras_error_t errcode;
277 gras_datadesc_type_t *my_type;
280 INFO0("---- Test on nested structures ----");
281 /* create descriptor */
282 my_type=gras_datadesc_struct("nestedstruct");
284 gras_datadesc_struct_append(my_type,"hete",
285 gras_datadesc_by_name("hetestruct"));
286 gras_datadesc_struct_append(my_type,"homo",
287 gras_datadesc_by_name("homostruct"));
288 gras_datadesc_struct_close(my_type);
289 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
291 /* init a value, exchange it and check its validity*/
292 i=gras_new(nestedstruct,1);
293 i->homo.a = 235231; i->homo.b = -124151;
294 i->homo.c = 211551; i->homo.d = -664222;
295 i->hete.c1 = 's'; i->hete.l1 = 123455;
296 i->hete.c2 = 'e'; i->hete.l2 = 774531;
298 TRY(write_read(my_type, &i,&j, sock,direction));
299 if (direction == READ || direction == RW) {
300 gras_assert(i->homo.a == j->homo.a);
301 gras_assert(i->homo.b == j->homo.b);
302 gras_assert(i->homo.c == j->homo.c);
303 gras_assert(i->homo.d == j->homo.d);
304 gras_assert(i->hete.c1 == j->hete.c1);
305 gras_assert(i->hete.c2 == j->hete.c2);
306 gras_assert(i->hete.l1 == j->hete.l1);
307 gras_assert(i->hete.l2 == j->hete.l2);
317 typedef struct s_chained_list chained_list_t;
318 struct s_chained_list {
322 gras_error_t declare_chained_list_type(void);
323 chained_list_t *cons(int v, chained_list_t *l);
324 void list_free(chained_list_t *l);
325 int list_eq(chained_list_t*i,chained_list_t*j);
327 gras_error_t declare_chained_list_type(void) {
328 gras_error_t errcode;
329 gras_datadesc_type_t *my_type,*ref_my_type;
331 my_type=gras_datadesc_struct("chained_list_t");
332 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
334 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
335 gras_datadesc_struct_append(my_type,"l", ref_my_type);
336 gras_datadesc_struct_close(my_type);
341 chained_list_t * cons(int v, chained_list_t *l) {
342 chained_list_t *nl = gras_new(chained_list_t,1);
349 void list_free(chained_list_t*l) {
355 int list_eq(chained_list_t*i,chained_list_t*j) {
356 if (!i || !j) return i == j;
359 return list_eq(i->l, j->l);
361 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
362 gras_error_t errcode;
363 chained_list_t *i, *j;
365 INFO0("---- Test on chained list ----");
367 /* init a value, exchange it and check its validity*/
368 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
371 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
374 if (direction == READ || direction == RW) {
375 gras_assert(list_eq(i,j));
385 gras_error_t test_graph(gras_socket_t *sock, int direction) {
386 gras_error_t errcode;
387 chained_list_t *i, *j;
389 INFO0("---- Test on graph (cyclique chained list) ----");
390 /* init a value, exchange it and check its validity*/
391 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
395 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
396 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
397 &i,&j, sock,direction));
398 if (direction == READ || direction == RW) {
401 DEBUG1("i->l=%p" ,i->l);
402 DEBUG1("i->l->l=%p" ,i->l->l);
403 DEBUG1("i->l->l->l=%p",i->l->l->l);
405 DEBUG1("j->l=%p" ,j->l);
406 DEBUG1("j->l->l=%p" ,j->l->l);
407 DEBUG1("j->l->l->l=%p",j->l->l->l);
408 gras_assert4(j->l->l->l == j,
409 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
415 gras_assert(list_eq(i,j));
419 i->l->l->l = NULL; /* do this even in WRITE mode */
425 GRAS_DEFINE_TYPE(s_pbio,
426 struct s_pbio{ /* structure presented in the IEEE article */
437 double Cdfgrd0[373][3];
438 double Cdfgrd1[3][3];
440 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);
455 /* Fill in that damn struct */
457 for (cpt=0; cpt<12; cpt++)
458 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
460 for (cpt=0; cpt<110; cpt++)
461 i.Cprops[cpt] = cpt * 100.0;
462 for (cpt=0; cpt<4; cpt++)
463 i.Cndi[cpt] = cpt * 23262;
466 i.Cdtime = -11515.662;
467 i.Ctime[0] = 332523.226;
468 i.Ctime[1] = -26216.113;
469 i.Cntens = 235211411;
471 for (cpt=0; cpt<3; cpt++) {
472 for (cpt2=0; cpt2<373; cpt2++)
473 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
474 for (cpt2=0; cpt2<3; cpt2++)
475 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
477 for (cpt=0; cpt<106; cpt++) {
478 i.Cstress[cpt]=(double)cpt * 22.113;
479 for (cpt2=0; cpt2<106; cpt2++)
480 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
482 TRY(write_read(gras_datadesc_by_symbol(s_pbio),
483 &i,&j, sock,direction));
484 if (direction == READ || direction == RW) {
485 /* Check that the data match */
486 gras_assert(i.Cnstatv == j.Cnstatv);
487 for (cpt=0; cpt<12; cpt++)
488 gras_assert(i.Cstatev[cpt] == j.Cstatev[cpt]);
489 gras_assert(i.Cnprops == j.Cnprops);
490 for (cpt=0; cpt<110; cpt++)
491 gras_assert(i.Cprops[cpt] == j.Cprops[cpt]);
492 for (cpt=0; cpt<4; cpt++)
493 gras_assert(i.Cndi[cpt] == j.Cndi[cpt]);
494 gras_assert(i.Cnshr == j.Cnshr);
495 gras_assert(i.Cnpt == j.Cnpt);
496 gras_assert(i.Cdtime == j.Cdtime);
497 gras_assert(i.Ctime[0] == j.Ctime[0]);
498 gras_assert(i.Ctime[1] == j.Ctime[1]);
499 gras_assert(i.Cntens == j.Cntens);
500 for (cpt=0; cpt<3; cpt++) {
501 for (cpt2=0; cpt2<373; cpt2++)
502 gras_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
503 for (cpt2=0; cpt2<3; cpt2++)
504 gras_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
506 for (cpt=0; cpt<106; cpt++) {
507 gras_assert(i.Cstress[cpt] == j.Cstress[cpt]);
508 for (cpt2=0; cpt2<106; cpt2++)
509 gras_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
510 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
511 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
518 GRAS_DEFINE_TYPE(s_clause,
521 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
523 typedef struct s_clause Clause;
525 gras_error_t test_clause(gras_socket_t *sock, int direction) {
526 gras_error_t errcode;
527 gras_datadesc_type_t *ddt,*array_t;
531 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
533 /* create and fill the struct */
534 i=gras_new(Clause,1);
537 i->literals = gras_new(int, i->num_lits);
538 for (cpt=0; cpt<i->num_lits; cpt++)
539 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
540 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
541 DEBUG1("created count=%d",i->num_lits);
543 /* create the damn type descriptor */
544 ddt = gras_datadesc_by_symbol(s_clause);
545 /* gras_datadesc_type_dump(ddt); */
547 ddt=gras_datadesc_ref("Clause*",ddt);
549 TRY(write_read(ddt, &i,&j, sock,direction));
550 if (direction == READ || direction == RW) {
551 gras_assert(i->num_lits == j->num_lits);
552 for (cpt=0; cpt<i->num_lits; cpt++)
553 gras_assert(i->literals[cpt] == j->literals[cpt]);
555 gras_free(j->literals);
558 gras_free(i->literals);
563 int main(int argc,char *argv[]) {
564 gras_error_t errcode;
568 char r_arch_char = gras_arch_selfid();
570 gras_init_defaultlog(&argc,argv,NULL);
572 for (cpt=1; cpt<argc; cpt++) {
573 if (!strcmp(argv[cpt], "--read")) {
575 } else if (!strcmp(argv[cpt], "--write")) {
582 if (direction == WRITE) {
583 TRYFAIL(gras_socket_client_from_file(filename,&sock));
584 TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
587 if (direction == READ) {
588 TRYFAIL(gras_socket_server_from_file(filename,&sock));
589 TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
590 gras_arch_selfid(), &r_arch_char));
591 INFO3("This datafile was generated on %s (%d), I'm %s.",
592 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
593 gras_datadesc_arch_name(gras_arch_selfid()));
595 r_arch = (int)r_arch_char;
597 TRYFAIL(test_int(sock,direction));
598 TRYFAIL(test_float(sock,direction));
599 TRYFAIL(test_double(sock,direction));
600 TRYFAIL(test_array(sock,direction));
601 TRYFAIL(test_intref(sock,direction));
603 TRYFAIL(test_string(sock,direction));
605 TRYFAIL(test_structures(sock,direction));
607 TRYFAIL(test_homostruct(sock,direction));
608 TRYFAIL(test_hetestruct(sock,direction));
609 TRYFAIL(test_nestedstruct(sock,direction));
611 TRYFAIL(declare_chained_list_type());
612 TRYFAIL(test_chain_list(sock,direction));
613 TRYFAIL(test_graph(sock,direction));
615 TRYFAIL(test_pbio(sock,direction));
617 TRYFAIL(test_clause(sock,direction));
620 gras_socket_close(sock);