3 /* datadesc: test of data description (using file transport). */
5 /* Copyright (c) 2004-2007 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
17 #include "gras/DataDesc/datadesc_interface.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
24 const char *filename = "datadesc_usage.out";
27 write_read(const char* msgtype,void *src, void *dst,
28 gras_socket_t sock, int direction);
31 write_read(const char* msgtype,void *src, void *dst,
32 gras_socket_t sock, int direction) {
36 gras_msg_wait(15, msgtype, NULL, dst);
40 gras_msg_send(sock, msgtype, src);
44 gras_datadesc_memcpy(gras_datadesc_by_name(msgtype), src, dst);
50 /* defined in datadesc_structures.c, which in perl generated */
51 void register_structures(void);
52 void test_structures(gras_socket_t sock, int direction);
54 /************************
55 * Each and every tests *
56 ************************/
58 static void test_int(gras_socket_t sock, int direction) {
61 INFO0("---- Test on integer ----");
62 write_read("int", &i,&j, sock,direction);
63 if (direction == READ || direction == COPY)
66 static void test_float(gras_socket_t sock, int direction) {
69 INFO0("---- Test on float ----");
70 write_read("float", &i,&j, sock,direction);
71 if (direction == READ || direction == COPY)
72 xbt_assert2(i == j,"%f != %f",i,j);
74 static void test_double(gras_socket_t sock, int direction) {
75 double i=-3252355.1234,j;
77 INFO0("---- Test on double ----");
78 write_read("double", &i,&j, sock,direction);
79 if (direction == READ || direction == COPY)
80 xbt_assert2(i == j,"%f != %f",i,j);
83 #define FIXED_ARRAY_SIZE 5
84 typedef int array[FIXED_ARRAY_SIZE];
85 static void test_array(gras_socket_t sock, int direction) {
86 array i = { 35212,-6226,74337,11414,7733};
90 INFO0("---- Test on fixed array ----");
92 write_read("fixed int array", &i,&j, sock,direction);
93 if (direction == READ || direction == COPY) {
94 for (cpt=0; cpt<FIXED_ARRAY_SIZE; cpt++) {
95 DEBUG1("Test spot %d",cpt);
96 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
97 cpt,i[cpt],cpt,j[cpt]);
101 /*** Dynar of scalar ***/
103 static void test_dynar_scal(gras_socket_t sock, int direction){
107 INFO0("---- Test on dynar containing integers ----");
108 i = xbt_dynar_new(sizeof(int),NULL);
109 for (cpt=0; cpt<64; cpt++) {
110 xbt_dynar_push_as(i,int,cpt);
111 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(i));
113 /* xbt_dynar_dump(i);*/
114 write_read("xbt_dynar_of_int", &i,&j, sock, direction);
115 /* xbt_dynar_dump(j);*/
116 if (direction == READ || direction == COPY) {
117 for (cpt=0; cpt<64; cpt++){
118 int ret=xbt_dynar_get_as(j,cpt,int);
120 CRITICAL3("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",
130 /*** Empty dynar ***/
132 static void test_dynar_empty(gras_socket_t sock, int direction){
135 INFO0("---- Test on empty dynar of integers ----");
136 i = xbt_dynar_new(sizeof(int),NULL);
137 write_read("xbt_dynar_of_int", &i,&j, sock, direction);
138 /* xbt_dynar_dump(j);*/
139 if (direction == READ || direction == COPY) {
140 xbt_assert(xbt_dynar_length(j) == 0);
146 static void test_intref(gras_socket_t sock, int direction) {
152 INFO0("---- Test on a reference to an integer ----");
154 write_read("int*", &i,&j, sock,direction);
155 if (direction == READ || direction == COPY) {
156 xbt_assert2(*i == *j,"*i != *j (%d != %d)",*i,*j);
163 *** string (dynamic array)
165 static void test_string(gras_socket_t sock, int direction) {
166 char *i=xbt_strdup("Some data"), *j=NULL;
169 INFO0("---- Test on string (ref to dynamic array) ----");
170 write_read("string", &i,&j, sock,direction);
171 if (direction == READ || direction == COPY) {
172 for (cpt=0; cpt<strlen(i); cpt++) {
173 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
174 cpt,i[cpt],cpt,j[cpt]);
183 *** homogeneous struct
188 static void test_homostruct(gras_socket_t sock, int direction) {
191 INFO0("---- Test on homogeneous structure ----");
192 /* init a value, exchange it and check its validity*/
193 i=xbt_new(homostruct,1);
194 i->a = 2235; i->b = 433425;
195 i->c = -23423; i->d = -235235;
197 write_read("homostruct*", &i,&j, sock,direction);
198 if (direction == READ || direction == COPY) {
199 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
200 xbt_assert(i->b == j->b);
201 xbt_assert(i->c == j->c);
202 xbt_assert(i->d == j->d);
209 *** heterogeneous struct
213 unsigned long int l1;
215 unsigned long int l2;
217 static void test_hetestruct(gras_socket_t sock, int direction) {
220 INFO0("---- Test on heterogeneous structure ----");
221 /* init a value, exchange it and check its validity*/
222 i=xbt_new(hetestruct,1);
223 i->c1 = 's'; i->l1 = 123455;
224 i->c2 = 'e'; i->l2 = 774531;
226 write_read("hetestruct*", &i,&j, sock,direction);
227 if (direction == READ || direction == COPY) {
228 xbt_assert(i->c1 == j->c1);
229 xbt_assert(i->c2 == j->c2);
230 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
231 xbt_assert(i->l2 == j->l2);
237 static void test_hetestruct_array(gras_socket_t sock, int direction) {
238 hetestruct *i, *j, *p,*q;
241 INFO0("---- Test on heterogeneous structure arrays ----");
242 /* init a value, exchange it and check its validity*/
243 i=xbt_malloc(sizeof(hetestruct)*10);
244 for (cpt=0,p=i;cpt<10;cpt++,p++) {
245 p->c1 = 's'+cpt; p->l1 = 123455+cpt;
246 p->c2 = 'e'+cpt; p->l2 = 774531+cpt;
249 write_read("hetestruct[10]*", &i,&j, sock,direction);
250 if (direction == READ || direction == COPY) {
254 xbt_assert(p->c1 == q->c1);
255 xbt_assert(p->c2 == q->c2);
256 xbt_assert3(p->l1 == p->l1,
257 "for cpt=%d i->l1(=%ld) != j->l1(=%ld)",cpt,p->l1,q->l1);
258 xbt_assert(q->l2 == p->l2);
271 static void test_nestedstruct(gras_socket_t sock, int direction) {
274 INFO0("---- Test on nested structures ----");
275 /* init a value, exchange it and check its validity*/
276 i=xbt_new(nestedstruct,1);
277 i->homo.a = 235231; i->homo.b = -124151;
278 i->homo.c = 211551; i->homo.d = -664222;
279 i->hete.c1 = 's'; i->hete.l1 = 123455;
280 i->hete.c2 = 'e'; i->hete.l2 = 774531;
282 write_read("nestedstruct*", &i,&j, sock,direction);
283 if (direction == READ || direction == COPY) {
284 xbt_assert(i->homo.a == j->homo.a);
285 xbt_assert(i->homo.b == j->homo.b);
286 xbt_assert(i->homo.c == j->homo.c);
287 xbt_assert(i->homo.d == j->homo.d);
288 xbt_assert(i->hete.c1 == j->hete.c1);
289 xbt_assert(i->hete.c2 == j->hete.c2);
290 xbt_assert(i->hete.l1 == j->hete.l1);
291 xbt_assert(i->hete.l2 == j->hete.l2);
300 typedef struct s_chained_list chained_list_t;
301 struct s_chained_list {
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 chained_list_t * cons(int v, chained_list_t *l) {
310 chained_list_t *nl = xbt_new(chained_list_t,1);
317 void list_free(chained_list_t*l) {
323 int list_eq(chained_list_t*i,chained_list_t*j) {
324 if (!i || !j) return i == j;
327 return list_eq(i->l, j->l);
329 static void test_chain_list(gras_socket_t sock, int direction) {
330 chained_list_t *i, *j;
332 INFO0("---- Test on chained list ----");
334 /* init a value, exchange it and check its validity*/
335 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
338 write_read("chained_list_t*", &i,&j, sock,direction);
339 if (direction == READ || direction == COPY) {
340 xbt_assert(list_eq(i,j));
349 static void test_graph(gras_socket_t sock, int direction) {
350 chained_list_t *i, *j;
352 INFO0("---- Test on graph (cyclique chained list of 3 items) ----");
353 /* init a value, exchange it and check its validity*/
354 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
358 write_read("chained_list_t*", &i,&j, sock,direction);
359 if (direction == READ || direction == COPY) {
362 DEBUG1("i->l=%p" ,i->l);
363 DEBUG1("i->l->l=%p" ,i->l->l);
364 DEBUG1("i->l->l->l=%p",i->l->l->l);
366 DEBUG1("j->l=%p" ,j->l);
367 DEBUG1("j->l->l=%p" ,j->l->l);
368 DEBUG1("j->l->l->l=%p",j->l->l->l);
369 xbt_assert4(j->l->l->l == j,
370 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
376 xbt_assert(list_eq(i,j));
380 i->l->l->l = NULL; /* do this even in WRITE mode */
385 /*** Dynar of references ***/
386 static void free_string(void *d){ /* used to free the data in dynar */
389 static void test_dynar_ref(gras_socket_t sock, int direction){
395 INFO0("---- Test on dynar containing integers ----");
397 i=xbt_dynar_new(sizeof(char*),&free_string);
398 for (cpt=0; cpt< 64; cpt++) {
399 sprintf(buf,"%d",cpt);
401 xbt_dynar_push(i,&s1);
404 write_read("xbt_dynar_of_string", &i,&j, sock, direction);
405 if (direction == READ || direction == COPY) {
406 for (cpt=0; cpt< 64; cpt++) {
407 sprintf(buf,"%d",cpt);
408 xbt_dynar_shift(j,&s2);
409 xbt_assert2 (!strcmp(buf,s2),
410 "The retrieved value is not the same than the injected one (%s!=%s)",
421 GRAS_DEFINE_TYPE(s_pbio,
422 struct s_pbio{ /* structure presented in the IEEE article */
433 double Cdfgrd0[373][3];
434 double Cdfgrd1[3][3];
436 double Cddsdde[106][106];
439 typedef struct s_pbio pbio_t;
441 static void test_pbio(gras_socket_t sock, int direction) {
444 gras_datadesc_type_t pbio_type;
447 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
448 pbio_type = gras_datadesc_by_symbol(s_pbio);
450 /* Fill in that damn struct */
452 for (cpt=0; cpt<12; cpt++)
453 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
455 for (cpt=0; cpt<110; cpt++)
456 i.Cprops[cpt] = cpt * 100.0;
457 for (cpt=0; cpt<4; cpt++)
458 i.Cndi[cpt] = cpt * 23262;
461 i.Cdtime = -11515.662;
462 i.Ctime[0] = 332523.226;
463 i.Ctime[1] = -26216.113;
464 i.Cntens = 235211411;
466 for (cpt=0; cpt<3; cpt++) {
467 for (cpt2=0; cpt2<373; cpt2++)
468 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
469 for (cpt2=0; cpt2<3; cpt2++)
470 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
472 for (cpt=0; cpt<106; cpt++) {
473 i.Cstress[cpt]=(double)cpt * 22.113;
474 for (cpt2=0; cpt2<106; cpt2++)
475 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
477 write_read("s_pbio", &i,&j, sock,direction);
478 if (direction == READ || direction == COPY) {
479 /* Check that the data match */
480 xbt_assert(i.Cnstatv == j.Cnstatv);
481 for (cpt=0; cpt<12; cpt++)
482 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
483 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
484 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
485 xbt_assert(i.Cnprops == j.Cnprops);
486 for (cpt=0; cpt<110; cpt++)
487 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
488 for (cpt=0; cpt<4; cpt++)
489 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
490 xbt_assert(i.Cnshr == j.Cnshr);
491 xbt_assert(i.Cnpt == j.Cnpt);
492 xbt_assert(i.Cdtime == j.Cdtime);
493 xbt_assert(i.Ctime[0] == j.Ctime[0]);
494 xbt_assert(i.Ctime[1] == j.Ctime[1]);
495 xbt_assert(i.Cntens == j.Cntens);
496 for (cpt=0; cpt<3; cpt++) {
497 for (cpt2=0; cpt2<373; cpt2++)
498 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
499 for (cpt2=0; cpt2<3; cpt2++)
500 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
502 for (cpt=0; cpt<106; cpt++) {
503 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
504 for (cpt2=0; cpt2<106; cpt2++)
505 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
506 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
507 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
512 GRAS_DEFINE_TYPE(s_clause,
515 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
517 typedef struct s_clause Clause;
519 static void test_clause(gras_socket_t sock, int direction) {
523 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
525 /* create and fill the struct */
529 i->literals = xbt_new(int, i->num_lits);
530 for (cpt=0; cpt<i->num_lits; cpt++)
531 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
532 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
533 DEBUG1("created count=%d",i->num_lits);
535 write_read("Clause*", &i,&j, sock,direction);
536 if (direction == READ || direction == COPY) {
537 xbt_assert(i->num_lits == j->num_lits);
538 for (cpt=0; cpt<i->num_lits; cpt++)
539 xbt_assert(i->literals[cpt] == j->literals[cpt]);
548 static void test_clause_empty(gras_socket_t sock, int direction) {
551 INFO0("---- Test on struct containing dynamic array and its size when size=0 (cbps test) ----");
553 /* create and fill the struct */
558 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
559 DEBUG1("created count=%d",i->num_lits);
561 write_read("Clause*", &i,&j, sock,direction);
562 if (direction == READ || direction == COPY) {
563 xbt_assert(i->num_lits == j->num_lits);
572 static void register_types(void) {
573 gras_datadesc_type_t my_type,ref_my_type;
575 gras_msgtype_declare("char",gras_datadesc_by_name("char"));
576 gras_msgtype_declare("int",gras_datadesc_by_name("int"));
577 gras_msgtype_declare("float",gras_datadesc_by_name("float"));
578 gras_msgtype_declare("double",gras_datadesc_by_name("double"));
580 my_type=gras_datadesc_array_fixed("fixed int array",
581 gras_datadesc_by_name("int"),
583 gras_msgtype_declare("fixed int array",my_type);
585 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
586 gras_msgtype_declare("xbt_dynar_of_int",my_type);
588 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
589 gras_msgtype_declare("int*",my_type);
591 gras_msgtype_declare("string",gras_datadesc_by_name("string"));
593 my_type=gras_datadesc_struct("homostruct");
594 gras_datadesc_struct_append(my_type,"a",gras_datadesc_by_name("signed int"));
595 gras_datadesc_struct_append(my_type,"b",gras_datadesc_by_name("int"));
596 gras_datadesc_struct_append(my_type,"c",gras_datadesc_by_name("int"));
597 gras_datadesc_struct_append(my_type,"d",gras_datadesc_by_name("int"));
598 gras_datadesc_struct_close(my_type);
599 my_type=gras_datadesc_ref("homostruct*",gras_datadesc_by_name("homostruct"));
600 gras_msgtype_declare("homostruct*",my_type);
602 my_type=gras_datadesc_struct("hetestruct");
603 gras_datadesc_struct_append(my_type,"c1", gras_datadesc_by_name("unsigned char"));
604 gras_datadesc_struct_append(my_type,"l1", gras_datadesc_by_name("unsigned long int"));
605 gras_datadesc_struct_append(my_type,"c2", gras_datadesc_by_name("unsigned char"));
606 gras_datadesc_struct_append(my_type,"l2", gras_datadesc_by_name("unsigned long int"));
607 gras_datadesc_struct_close(my_type);
608 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
609 gras_msgtype_declare("hetestruct*",my_type);
611 my_type = gras_datadesc_array_fixed("hetestruct[10]",gras_datadesc_by_name("hetestruct"),10);
612 my_type = gras_datadesc_ref("hetestruct[10]*", my_type);
613 gras_msgtype_declare("hetestruct[10]*",my_type);
615 my_type=gras_datadesc_struct("nestedstruct");
616 gras_datadesc_struct_append(my_type,"hete",gras_datadesc_by_name("hetestruct"));
617 gras_datadesc_struct_append(my_type,"homo",gras_datadesc_by_name("homostruct"));
618 gras_datadesc_struct_close(my_type);
619 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
620 gras_msgtype_declare("nestedstruct*",my_type);
622 my_type=gras_datadesc_struct("chained_list_t");
623 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
624 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
625 gras_datadesc_struct_append(my_type,"l", ref_my_type);
626 gras_datadesc_struct_close(my_type);
627 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
628 gras_msgtype_declare("chained_list_t",my_type);
629 gras_msgtype_declare("chained_list_t*",ref_my_type);
631 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
632 gras_msgtype_declare("xbt_dynar_of_string",my_type);
634 my_type = gras_datadesc_by_symbol(s_pbio);
635 gras_msgtype_declare("s_pbio",my_type);
637 my_type = gras_datadesc_by_symbol(s_clause);
638 my_type = gras_datadesc_ref("Clause*",my_type);
639 gras_msgtype_declare("Clause*",my_type);
647 int main(int argc,char *argv[]) {
648 gras_socket_t sock=NULL;
649 int direction = WRITE;
651 char local_arch, remote_arch;
653 gras_init(&argc,argv);
655 register_structures();
657 for (cpt=1; cpt<argc; cpt++) {
658 if (!strcmp(argv[cpt], "--arch")) {
659 INFO2("We are on %s (#%d)",
660 gras_datadesc_arch_name(gras_arch_selfid()),(int)gras_arch_selfid());
662 } else if (!strcmp(argv[cpt], "--help")) {
663 printf("Usage: datadesc_usage [option] [filename]\n");
664 printf(" --arch: display the current architecture and quit.\n");
665 printf(" --read file: reads the description from the given file\n");
666 printf(" --write file: writes the description to the given file\n");
667 printf(" --copy: copy the description in memory\n");
668 printf(" --regen: write the description to the file of the current architecture\n");
669 printf(" --help: displays this message\n");
671 } else if (!strcmp(argv[cpt], "--regen")) {
673 filename = bprintf("datadesc.%s",gras_datadesc_arch_name(gras_arch_selfid()));
674 } else if (!strcmp(argv[cpt], "--read")) {
676 } else if (!strcmp(argv[cpt], "--write")) {
678 } else if (!strcmp(argv[cpt], "--copy")) {
685 if (direction == WRITE) {
686 INFO1("Write to file %s",filename);
687 sock = gras_socket_client_from_file(filename);
689 if (direction == READ) {
690 INFO1("Read from file %s",filename);
691 sock = gras_socket_server_from_file(filename);
693 if (direction == COPY) {
694 INFO0("Memory copy");
697 local_arch = gras_arch_selfid();
698 write_read("char", &local_arch,&remote_arch, sock,direction);
699 if (direction == READ)
700 VERB2("This file was generated on %s (%d)",
701 gras_datadesc_arch_name(remote_arch),(int)remote_arch);
704 test_int(sock,direction);
705 test_float(sock,direction);
706 test_double(sock,direction);
707 test_array(sock,direction);
708 test_intref(sock,direction);
710 test_string(sock,direction);
711 test_dynar_scal(sock,direction);
712 test_dynar_empty(sock,direction);
714 test_structures(sock,direction);
716 test_homostruct(sock,direction);
717 test_hetestruct(sock,direction);
718 test_nestedstruct(sock,direction);
719 test_hetestruct_array(sock,direction);
721 test_chain_list(sock,direction);
722 test_graph(sock,direction);
723 test_dynar_ref(sock,direction);
725 test_pbio(sock,direction);
727 test_clause(sock,direction);
728 test_clause_empty(sock,direction);
730 if (direction != COPY)
731 gras_socket_close(sock);