3 /* datadesc: test of data description (using file transport). */
5 /* Copyright (c) 2004 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. */
16 #include "..\..\..\..\include\gras\datadesc.h"
18 #include "gras/DataDesc/datadesc_interface.h"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
27 const char *filename = "datadesc_usage.out";
30 write_read(gras_datadesc_type_t type,void *src, void *dst, gras_socket_t sock, int direction);
33 write_read(gras_datadesc_type_t type,void *src, void *dst, gras_socket_t sock, int direction)
38 sock = gras_socket_client_from_file(filename);
40 if (direction == WRITE || direction == RW)
41 gras_datadesc_send(sock, type, src);
44 gras_socket_close(sock);
48 sock = gras_socket_server_from_file(filename);
50 if (direction == READ || direction == RW)
51 gras_datadesc_recv(sock, type, r_arch, dst);
54 gras_socket_close(sock);
56 if (direction == COPY)
57 gras_datadesc_copy(type, src, dst);
60 void test_int(gras_socket_t sock, int direction);
61 void test_float(gras_socket_t sock, int direction);
62 void test_double(gras_socket_t sock, int direction);
63 void test_array(gras_socket_t sock, int direction);
64 void test_dynar_scal(gras_socket_t sock, int direction);
65 void test_intref(gras_socket_t sock, int direction);
66 void test_string(gras_socket_t sock, int direction);
68 void test_homostruct(gras_socket_t sock, int direction);
69 void test_hetestruct(gras_socket_t sock, int direction);
70 void test_nestedstruct(gras_socket_t sock, int direction);
71 void test_chain_list(gras_socket_t sock, int direction);
72 void test_graph(gras_socket_t sock, int direction);
73 void test_dynar_ref(gras_socket_t sock, int direction);
75 void test_pbio(gras_socket_t sock, int direction);
76 void test_clause(gras_socket_t sock, int direction);
78 /* defined in datadesc_structures.c, which in perl generated */
79 void test_structures(gras_socket_t sock, int direction);
81 void test_int(gras_socket_t sock, int direction)
85 INFO0("---- Test on integer ----");
87 write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction);
89 if (direction == READ || direction == RW || direction == COPY)
92 void test_float(gras_socket_t sock, int direction)
96 INFO0("---- Test on float ----");
97 write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction);
99 if (direction == READ || direction == RW || direction == COPY)
100 xbt_assert2(i == j,"%f != %f",i,j);
102 void test_double(gras_socket_t sock, int direction)
104 double i=-3252355.1234,j;
106 INFO0("---- Test on double ----");
107 write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction);
109 if (direction == READ || direction == RW || direction == COPY)
110 xbt_assert2(i == j,"%f != %f",i,j);
114 typedef int array[SIZE];
116 void test_array(gras_socket_t sock, int direction)
118 gras_datadesc_type_t my_type;
120 array i = { 35212,-6226,74337,11414,7733};
124 INFO0("---- Test on fixed array ----");
126 my_type=gras_datadesc_array_fixed("fixed int array", gras_datadesc_by_name("int"),SIZE);
128 write_read(my_type, &i,&j, sock,direction);
130 if (direction == READ || direction == RW || direction == COPY)
132 for (cpt=0; cpt<SIZE; cpt++)
134 DEBUG1("Test spot %d",cpt);
135 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",cpt,i[cpt],cpt,j[cpt]);
139 /*** Dynar of scalar ***/
141 void test_dynar_scal(gras_socket_t sock, int direction)
143 gras_datadesc_type_t my_type;
147 INFO0("---- Test on dynar containing integers ----");
148 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
149 i = xbt_dynar_new(sizeof(int),NULL);
151 for (cpt=0; cpt<64; cpt++)
153 xbt_dynar_push_as(i,int,cpt);
154 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(i));
157 /* xbt_dynar_dump(i);*/
158 write_read(my_type, &i,&j, sock, direction);
160 /* xbt_dynar_dump(j);*/
161 if (direction == READ || direction == RW || direction == COPY)
163 for (cpt=0; cpt<64; cpt++)
165 int ret=xbt_dynar_get_as(j,cpt,int);
169 CRITICAL3("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",cpt,ret,cpt);
180 void test_intref(gras_socket_t sock, int direction)
182 gras_datadesc_type_t my_type;
188 INFO1("---- Test on a reference to an integer (%p) ----",i);
190 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
192 write_read(my_type, &i,&j, sock,direction);
193 if (direction == READ || direction == RW || direction == COPY)
195 xbt_assert2(*i == *j,"*i != *j (%d != %d)",*i,*j);
203 *** string (dynamic array)
205 void test_string(gras_socket_t sock, int direction)
207 char *i=xbt_strdup("Some data"), *j=NULL;
210 INFO0("---- Test on string (ref to dynamic array) ----");
211 write_read(gras_datadesc_by_name("string"), &i,&j,sock,direction);
213 if (direction == READ || direction == RW || direction == COPY)
215 for (cpt=0; cpt<strlen(i); cpt++)
217 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",cpt,i[cpt],cpt,j[cpt]);
228 *** homogeneous struct
235 void test_homostruct(gras_socket_t sock, int direction)
237 gras_datadesc_type_t my_type;
240 INFO0("---- Test on homogeneous structure ----");
241 /* create descriptor */
242 my_type=gras_datadesc_struct("homostruct");
243 gras_datadesc_struct_append(my_type,"a",
244 gras_datadesc_by_name("signed int"));
245 gras_datadesc_struct_append(my_type,"b",
246 gras_datadesc_by_name("int"));
247 gras_datadesc_struct_append(my_type,"c",
248 gras_datadesc_by_name("int"));
249 gras_datadesc_struct_append(my_type,"d",
250 gras_datadesc_by_name("int"));
251 gras_datadesc_struct_close(my_type);
252 my_type=gras_datadesc_ref("homostruct*",
253 gras_datadesc_by_name("homostruct"));
255 /* init a value, exchange it and check its validity*/
256 i=xbt_new(homostruct,1);
257 i->a = 2235; i->b = 433425;
258 i->c = -23423; i->d = -235235;
260 write_read(my_type, &i,&j, sock,direction);
261 if (direction == READ || direction == RW || direction == COPY) {
262 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
263 xbt_assert(i->b == j->b);
264 xbt_assert(i->c == j->c);
265 xbt_assert(i->d == j->d);
272 *** heterogeneous struct
276 unsigned long int l1;
278 unsigned long int l2;
280 void test_hetestruct(gras_socket_t sock, int direction) {
281 gras_datadesc_type_t my_type;
284 INFO0("---- Test on heterogeneous structure ----");
285 /* create descriptor */
286 my_type=gras_datadesc_struct("hetestruct");
287 gras_datadesc_struct_append(my_type,"c1",
288 gras_datadesc_by_name("unsigned char"));
289 gras_datadesc_struct_append(my_type,"l1",
290 gras_datadesc_by_name("unsigned long int"));
291 gras_datadesc_struct_append(my_type,"c2",
292 gras_datadesc_by_name("unsigned char"));
293 gras_datadesc_struct_append(my_type,"l2",
294 gras_datadesc_by_name("unsigned long int"));
295 gras_datadesc_struct_close(my_type);
296 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
299 /* init a value, exchange it and check its validity*/
300 i=xbt_new(hetestruct,1);
301 i->c1 = 's'; i->l1 = 123455;
302 i->c2 = 'e'; i->l2 = 774531;
304 write_read(my_type, &i,&j, sock,direction);
305 if (direction == READ || direction == RW || direction == COPY) {
306 xbt_assert(i->c1 == j->c1);
307 xbt_assert(i->c2 == j->c2);
308 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
309 xbt_assert(i->l2 == j->l2);
322 void test_nestedstruct(gras_socket_t sock, int direction) {
323 gras_datadesc_type_t my_type;
326 INFO0("---- Test on nested structures ----");
327 /* create descriptor */
328 my_type=gras_datadesc_struct("nestedstruct");
330 gras_datadesc_struct_append(my_type,"hete",
331 gras_datadesc_by_name("hetestruct"));
332 gras_datadesc_struct_append(my_type,"homo",
333 gras_datadesc_by_name("homostruct"));
334 gras_datadesc_struct_close(my_type);
335 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
337 /* init a value, exchange it and check its validity*/
338 i=xbt_new(nestedstruct,1);
339 i->homo.a = 235231; i->homo.b = -124151;
340 i->homo.c = 211551; i->homo.d = -664222;
341 i->hete.c1 = 's'; i->hete.l1 = 123455;
342 i->hete.c2 = 'e'; i->hete.l2 = 774531;
344 write_read(my_type, &i,&j, sock,direction);
345 if (direction == READ || direction == RW || direction == COPY) {
346 xbt_assert(i->homo.a == j->homo.a);
347 xbt_assert(i->homo.b == j->homo.b);
348 xbt_assert(i->homo.c == j->homo.c);
349 xbt_assert(i->homo.d == j->homo.d);
350 xbt_assert(i->hete.c1 == j->hete.c1);
351 xbt_assert(i->hete.c2 == j->hete.c2);
352 xbt_assert(i->hete.l1 == j->hete.l1);
353 xbt_assert(i->hete.l2 == j->hete.l2);
362 typedef struct s_chained_list chained_list_t;
363 struct s_chained_list {
367 void declare_chained_list_type(void);
368 chained_list_t *cons(int v, chained_list_t *l);
369 void list_free(chained_list_t *l);
370 int list_eq(chained_list_t*i,chained_list_t*j);
372 void declare_chained_list_type(void) {
373 gras_datadesc_type_t my_type,ref_my_type;
375 my_type=gras_datadesc_struct("chained_list_t");
376 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
378 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
379 gras_datadesc_struct_append(my_type,"l", ref_my_type);
380 gras_datadesc_struct_close(my_type);
383 chained_list_t * cons(int v, chained_list_t *l) {
384 chained_list_t *nl = xbt_new(chained_list_t,1);
391 void list_free(chained_list_t*l) {
397 int list_eq(chained_list_t*i,chained_list_t*j) {
398 if (!i || !j) return i == j;
401 return list_eq(i->l, j->l);
403 void test_chain_list(gras_socket_t sock, int direction) {
404 chained_list_t *i, *j;
406 INFO0("---- Test on chained list ----");
408 /* init a value, exchange it and check its validity*/
409 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
412 write_read(gras_datadesc_by_name("chained_list_t*"),
413 &i,&j, sock,direction);
414 if (direction == READ || direction == RW || direction == COPY) {
415 xbt_assert(list_eq(i,j));
424 void test_graph(gras_socket_t sock, int direction) {
425 chained_list_t *i, *j;
427 INFO0("---- Test on graph (cyclique chained list of 3 items) ----");
428 /* init a value, exchange it and check its validity*/
429 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
433 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
434 write_read(gras_datadesc_by_name("chained_list_t*"),
435 &i,&j, sock,direction);
436 if (direction == READ || direction == RW || direction == COPY) {
439 DEBUG1("i->l=%p" ,i->l);
440 DEBUG1("i->l->l=%p" ,i->l->l);
441 DEBUG1("i->l->l->l=%p",i->l->l->l);
443 DEBUG1("j->l=%p" ,j->l);
444 DEBUG1("j->l->l=%p" ,j->l->l);
445 DEBUG1("j->l->l->l=%p",j->l->l->l);
446 xbt_assert4(j->l->l->l == j,
447 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
453 xbt_assert(list_eq(i,j));
457 i->l->l->l = NULL; /* do this even in WRITE mode */
462 /*** Dynar of references ***/
463 static void free_string(void *d){ /* used to free the data in dynar */
466 void test_dynar_ref(gras_socket_t sock, int direction){
467 gras_datadesc_type_t my_type;
473 INFO0("---- Test on dynar containing integers ----");
474 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
476 i=xbt_dynar_new(sizeof(char*),&free_string);
477 for (cpt=0; cpt< 64; cpt++) {
478 sprintf(buf,"%d",cpt);
480 xbt_dynar_push(i,&s1);
483 write_read(my_type, &i,&j, sock, direction);
484 if (direction == READ || direction == RW || direction == COPY) {
485 for (cpt=0; cpt< 64; cpt++) {
486 sprintf(buf,"%d",cpt);
487 xbt_dynar_shift(j,&s2);
488 xbt_assert2 (!strcmp(buf,s2),
489 "The retrieved value is not the same than the injected one (%s!=%s)",
500 GRAS_DEFINE_TYPE(s_pbio,
501 struct s_pbio{ /* structure presented in the IEEE article */
512 double Cdfgrd0[373][3];
513 double Cdfgrd1[3][3];
515 double Cddsdde[106][106];
518 typedef struct s_pbio pbio_t;
520 void test_pbio(gras_socket_t sock, int direction) {
523 gras_datadesc_type_t pbio_type;
526 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
527 pbio_type = gras_datadesc_by_symbol(s_pbio);
529 /* Fill in that damn struct */
531 for (cpt=0; cpt<12; cpt++)
532 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
534 for (cpt=0; cpt<110; cpt++)
535 i.Cprops[cpt] = cpt * 100.0;
536 for (cpt=0; cpt<4; cpt++)
537 i.Cndi[cpt] = cpt * 23262;
540 i.Cdtime = -11515.662;
541 i.Ctime[0] = 332523.226;
542 i.Ctime[1] = -26216.113;
543 i.Cntens = 235211411;
545 for (cpt=0; cpt<3; cpt++) {
546 for (cpt2=0; cpt2<373; cpt2++)
547 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
548 for (cpt2=0; cpt2<3; cpt2++)
549 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
551 for (cpt=0; cpt<106; cpt++) {
552 i.Cstress[cpt]=(double)cpt * 22.113;
553 for (cpt2=0; cpt2<106; cpt2++)
554 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
556 write_read(gras_datadesc_by_symbol(s_pbio),
557 &i,&j, sock,direction);
558 if (direction == READ || direction == RW || direction == COPY) {
559 /* Check that the data match */
560 xbt_assert(i.Cnstatv == j.Cnstatv);
561 for (cpt=0; cpt<12; cpt++)
562 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
563 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
564 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
565 xbt_assert(i.Cnprops == j.Cnprops);
566 for (cpt=0; cpt<110; cpt++)
567 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
568 for (cpt=0; cpt<4; cpt++)
569 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
570 xbt_assert(i.Cnshr == j.Cnshr);
571 xbt_assert(i.Cnpt == j.Cnpt);
572 xbt_assert(i.Cdtime == j.Cdtime);
573 xbt_assert(i.Ctime[0] == j.Ctime[0]);
574 xbt_assert(i.Ctime[1] == j.Ctime[1]);
575 xbt_assert(i.Cntens == j.Cntens);
576 for (cpt=0; cpt<3; cpt++) {
577 for (cpt2=0; cpt2<373; cpt2++)
578 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
579 for (cpt2=0; cpt2<3; cpt2++)
580 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
582 for (cpt=0; cpt<106; cpt++) {
583 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
584 for (cpt2=0; cpt2<106; cpt2++)
585 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
586 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
587 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
592 GRAS_DEFINE_TYPE(s_clause,
595 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
597 typedef struct s_clause Clause;
599 void test_clause(gras_socket_t sock, int direction) {
600 gras_datadesc_type_t ddt;
604 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
606 /* create and fill the struct */
610 i->literals = xbt_new(int, i->num_lits);
611 for (cpt=0; cpt<i->num_lits; cpt++)
612 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
613 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
614 DEBUG1("created count=%d",i->num_lits);
616 /* create the damn type descriptor */
617 ddt = gras_datadesc_by_symbol(s_clause);
618 /* gras_datadesc_type_dump(ddt); */
620 ddt=gras_datadesc_ref("Clause*",ddt);
622 write_read(ddt, &i,&j, sock,direction);
623 if (direction == READ || direction == RW || direction == COPY) {
624 xbt_assert(i->num_lits == j->num_lits);
625 for (cpt=0; cpt<i->num_lits; cpt++)
626 xbt_assert(i->literals[cpt] == j->literals[cpt]);
637 int main(int argc,char *argv[])
639 gras_socket_t sock=NULL;
640 int direction =WRITE ; /* RW; */
642 char r_arch_char = gras_arch_selfid();
644 gras_init(&argc,argv);
646 for (cpt=1; cpt<argc; cpt++)
648 if (!strcmp(argv[cpt], "--read"))
652 else if (!strcmp(argv[cpt], "--write"))
656 else if (!strcmp(argv[cpt], "--copy"))
666 if (direction == WRITE)
668 sock = gras_socket_client_from_file(filename);
669 gras_datadesc_send(sock, gras_datadesc_by_name("char"),&r_arch_char);
671 if (direction == READ)
673 sock = gras_socket_server_from_file(filename);
674 gras_datadesc_recv(sock, gras_datadesc_by_name("char"),gras_arch_selfid(), &r_arch_char);
675 INFO3("This datafile was generated on %s (%d), I'm %s.",gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,gras_datadesc_arch_name(gras_arch_selfid()));
677 r_arch = (int)r_arch_char;
679 test_int(sock,direction);
681 test_float(sock,direction);
683 test_double(sock,direction);
685 test_array(sock,direction);
687 test_intref(sock,direction);
689 test_string(sock,direction);
691 test_dynar_scal(sock,direction);
693 test_structures(sock,direction);
695 test_homostruct(sock,direction);
697 test_hetestruct(sock,direction);
699 test_nestedstruct(sock,direction);
701 declare_chained_list_type();
703 test_chain_list(sock,direction);
705 test_graph(sock,direction);
707 test_dynar_ref(sock,direction);
709 test_pbio(sock,direction);
711 test_clause(sock,direction);
713 if (direction != RW && direction != COPY)
714 gras_socket_close(sock);