1 /* datadesc: test of data description (using file transport). */
3 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "..\..\..\..\include\gras\datadesc.h"
16 #include "gras/DataDesc/datadesc_interface.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
25 const char *filename = "datadesc_usage.out";
28 write_read(gras_datadesc_type_t type,void *src, void *dst, gras_socket_t sock, int direction);
31 write_read(gras_datadesc_type_t type,void *src, void *dst, gras_socket_t sock, int direction)
36 sock = gras_socket_client_from_file(filename);
38 if (direction == WRITE || direction == RW)
39 gras_datadesc_send(sock, type, src);
42 gras_socket_close(sock);
46 sock = gras_socket_server_from_file(filename);
48 if (direction == READ || direction == RW)
49 gras_datadesc_recv(sock, type, r_arch, dst);
52 gras_socket_close(sock);
54 if (direction == COPY)
55 gras_datadesc_copy(type, src, dst);
58 void test_int(gras_socket_t sock, int direction);
59 void test_float(gras_socket_t sock, int direction);
60 void test_double(gras_socket_t sock, int direction);
61 void test_array(gras_socket_t sock, int direction);
62 void test_dynar_scal(gras_socket_t sock, int direction);
63 void test_intref(gras_socket_t sock, int direction);
64 void test_string(gras_socket_t sock, int direction);
66 void test_homostruct(gras_socket_t sock, int direction);
67 void test_hetestruct(gras_socket_t sock, int direction);
68 void test_nestedstruct(gras_socket_t sock, int direction);
69 void test_chain_list(gras_socket_t sock, int direction);
70 void test_graph(gras_socket_t sock, int direction);
71 void test_dynar_ref(gras_socket_t sock, int direction);
73 void test_pbio(gras_socket_t sock, int direction);
74 void test_clause(gras_socket_t sock, int direction);
76 /* defined in datadesc_structures.c, which in perl generated */
77 void test_structures(gras_socket_t sock, int direction);
79 void test_int(gras_socket_t sock, int direction)
83 INFO0("---- Test on integer ----");
85 write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction);
87 if (direction == READ || direction == RW || direction == COPY)
90 void test_float(gras_socket_t sock, int direction)
94 INFO0("---- Test on float ----");
95 write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction);
97 if (direction == READ || direction == RW || direction == COPY)
98 xbt_assert2(i == j,"%f != %f",i,j);
100 void test_double(gras_socket_t sock, int direction)
102 double i=-3252355.1234,j;
104 INFO0("---- Test on double ----");
105 write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction);
107 if (direction == READ || direction == RW || direction == COPY)
108 xbt_assert2(i == j,"%f != %f",i,j);
112 typedef int array[SIZE];
114 void test_array(gras_socket_t sock, int direction)
116 gras_datadesc_type_t my_type;
118 array i = { 35212,-6226,74337,11414,7733};
122 INFO0("---- Test on fixed array ----");
124 my_type=gras_datadesc_array_fixed("fixed int array", gras_datadesc_by_name("int"),SIZE);
126 write_read(my_type, &i,&j, sock,direction);
128 if (direction == READ || direction == RW || direction == COPY)
130 for (cpt=0; cpt<SIZE; cpt++)
132 DEBUG1("Test spot %d",cpt);
133 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",cpt,i[cpt],cpt,j[cpt]);
137 /*** Dynar of scalar ***/
139 void test_dynar_scal(gras_socket_t sock, int direction)
141 gras_datadesc_type_t my_type;
145 INFO0("---- Test on dynar containing integers ----");
146 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
147 i = xbt_dynar_new(sizeof(int),NULL);
149 for (cpt=0; cpt<64; cpt++)
151 xbt_dynar_push_as(i,int,cpt);
152 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(i));
155 /* xbt_dynar_dump(i);*/
156 write_read(my_type, &i,&j, sock, direction);
158 /* xbt_dynar_dump(j);*/
159 if (direction == READ || direction == RW || direction == COPY)
161 for (cpt=0; cpt<64; cpt++)
163 int ret=xbt_dynar_get_as(j,cpt,int);
167 CRITICAL3("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",cpt,ret,cpt);
178 void test_intref(gras_socket_t sock, int direction)
180 gras_datadesc_type_t my_type;
186 INFO1("---- Test on a reference to an integer (%p) ----",i);
188 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
190 write_read(my_type, &i,&j, sock,direction);
191 if (direction == READ || direction == RW || direction == COPY)
193 xbt_assert2(*i == *j,"*i != *j (%d != %d)",*i,*j);
201 *** string (dynamic array)
203 void test_string(gras_socket_t sock, int direction)
205 char *i=xbt_strdup("Some data"), *j=NULL;
208 INFO0("---- Test on string (ref to dynamic array) ----");
209 write_read(gras_datadesc_by_name("string"), &i,&j,sock,direction);
211 if (direction == READ || direction == RW || direction == COPY)
213 for (cpt=0; cpt<strlen(i); cpt++)
215 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",cpt,i[cpt],cpt,j[cpt]);
226 *** homogeneous struct
233 void test_homostruct(gras_socket_t sock, int direction)
235 gras_datadesc_type_t my_type;
238 INFO0("---- Test on homogeneous structure ----");
239 /* create descriptor */
240 my_type=gras_datadesc_struct("homostruct");
241 gras_datadesc_struct_append(my_type,"a",
242 gras_datadesc_by_name("signed int"));
243 gras_datadesc_struct_append(my_type,"b",
244 gras_datadesc_by_name("int"));
245 gras_datadesc_struct_append(my_type,"c",
246 gras_datadesc_by_name("int"));
247 gras_datadesc_struct_append(my_type,"d",
248 gras_datadesc_by_name("int"));
249 gras_datadesc_struct_close(my_type);
250 my_type=gras_datadesc_ref("homostruct*",
251 gras_datadesc_by_name("homostruct"));
253 /* init a value, exchange it and check its validity*/
254 i=xbt_new(homostruct,1);
255 i->a = 2235; i->b = 433425;
256 i->c = -23423; i->d = -235235;
258 write_read(my_type, &i,&j, sock,direction);
259 if (direction == READ || direction == RW || direction == COPY) {
260 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
261 xbt_assert(i->b == j->b);
262 xbt_assert(i->c == j->c);
263 xbt_assert(i->d == j->d);
270 *** heterogeneous struct
274 unsigned long int l1;
276 unsigned long int l2;
278 void test_hetestruct(gras_socket_t sock, int direction) {
279 gras_datadesc_type_t my_type;
282 INFO0("---- Test on heterogeneous structure ----");
283 /* create descriptor */
284 my_type=gras_datadesc_struct("hetestruct");
285 gras_datadesc_struct_append(my_type,"c1",
286 gras_datadesc_by_name("unsigned char"));
287 gras_datadesc_struct_append(my_type,"l1",
288 gras_datadesc_by_name("unsigned long int"));
289 gras_datadesc_struct_append(my_type,"c2",
290 gras_datadesc_by_name("unsigned char"));
291 gras_datadesc_struct_append(my_type,"l2",
292 gras_datadesc_by_name("unsigned long int"));
293 gras_datadesc_struct_close(my_type);
294 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
297 /* init a value, exchange it and check its validity*/
298 i=xbt_new(hetestruct,1);
299 i->c1 = 's'; i->l1 = 123455;
300 i->c2 = 'e'; i->l2 = 774531;
302 write_read(my_type, &i,&j, sock,direction);
303 if (direction == READ || direction == RW || direction == COPY) {
304 xbt_assert(i->c1 == j->c1);
305 xbt_assert(i->c2 == j->c2);
306 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
307 xbt_assert(i->l2 == j->l2);
320 void test_nestedstruct(gras_socket_t sock, int direction) {
321 gras_datadesc_type_t my_type;
324 INFO0("---- Test on nested structures ----");
325 /* create descriptor */
326 my_type=gras_datadesc_struct("nestedstruct");
328 gras_datadesc_struct_append(my_type,"hete",
329 gras_datadesc_by_name("hetestruct"));
330 gras_datadesc_struct_append(my_type,"homo",
331 gras_datadesc_by_name("homostruct"));
332 gras_datadesc_struct_close(my_type);
333 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
335 /* init a value, exchange it and check its validity*/
336 i=xbt_new(nestedstruct,1);
337 i->homo.a = 235231; i->homo.b = -124151;
338 i->homo.c = 211551; i->homo.d = -664222;
339 i->hete.c1 = 's'; i->hete.l1 = 123455;
340 i->hete.c2 = 'e'; i->hete.l2 = 774531;
342 write_read(my_type, &i,&j, sock,direction);
343 if (direction == READ || direction == RW || direction == COPY) {
344 xbt_assert(i->homo.a == j->homo.a);
345 xbt_assert(i->homo.b == j->homo.b);
346 xbt_assert(i->homo.c == j->homo.c);
347 xbt_assert(i->homo.d == j->homo.d);
348 xbt_assert(i->hete.c1 == j->hete.c1);
349 xbt_assert(i->hete.c2 == j->hete.c2);
350 xbt_assert(i->hete.l1 == j->hete.l1);
351 xbt_assert(i->hete.l2 == j->hete.l2);
360 typedef struct s_chained_list chained_list_t;
361 struct s_chained_list {
365 void declare_chained_list_type(void);
366 chained_list_t *cons(int v, chained_list_t *l);
367 void list_free(chained_list_t *l);
368 int list_eq(chained_list_t*i,chained_list_t*j);
370 void declare_chained_list_type(void) {
371 gras_datadesc_type_t my_type,ref_my_type;
373 my_type=gras_datadesc_struct("chained_list_t");
374 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
376 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
377 gras_datadesc_struct_append(my_type,"l", ref_my_type);
378 gras_datadesc_struct_close(my_type);
381 chained_list_t * cons(int v, chained_list_t *l) {
382 chained_list_t *nl = xbt_new(chained_list_t,1);
389 void list_free(chained_list_t*l) {
395 int list_eq(chained_list_t*i,chained_list_t*j) {
396 if (!i || !j) return i == j;
399 return list_eq(i->l, j->l);
401 void test_chain_list(gras_socket_t sock, int direction) {
402 chained_list_t *i, *j;
404 INFO0("---- Test on chained list ----");
406 /* init a value, exchange it and check its validity*/
407 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
410 write_read(gras_datadesc_by_name("chained_list_t*"),
411 &i,&j, sock,direction);
412 if (direction == READ || direction == RW || direction == COPY) {
413 xbt_assert(list_eq(i,j));
422 void test_graph(gras_socket_t sock, int direction) {
423 chained_list_t *i, *j;
425 INFO0("---- Test on graph (cyclique chained list of 3 items) ----");
426 /* init a value, exchange it and check its validity*/
427 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
431 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
432 write_read(gras_datadesc_by_name("chained_list_t*"),
433 &i,&j, sock,direction);
434 if (direction == READ || direction == RW || direction == COPY) {
437 DEBUG1("i->l=%p" ,i->l);
438 DEBUG1("i->l->l=%p" ,i->l->l);
439 DEBUG1("i->l->l->l=%p",i->l->l->l);
441 DEBUG1("j->l=%p" ,j->l);
442 DEBUG1("j->l->l=%p" ,j->l->l);
443 DEBUG1("j->l->l->l=%p",j->l->l->l);
444 xbt_assert4(j->l->l->l == j,
445 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
451 xbt_assert(list_eq(i,j));
455 i->l->l->l = NULL; /* do this even in WRITE mode */
460 /*** Dynar of references ***/
461 static void free_string(void *d){ /* used to free the data in dynar */
464 void test_dynar_ref(gras_socket_t sock, int direction){
465 gras_datadesc_type_t my_type;
471 INFO0("---- Test on dynar containing integers ----");
472 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
474 i=xbt_dynar_new(sizeof(char*),&free_string);
475 for (cpt=0; cpt< 64; cpt++) {
476 sprintf(buf,"%d",cpt);
478 xbt_dynar_push(i,&s1);
481 write_read(my_type, &i,&j, sock, direction);
482 if (direction == READ || direction == RW || direction == COPY) {
483 for (cpt=0; cpt< 64; cpt++) {
484 sprintf(buf,"%d",cpt);
485 xbt_dynar_shift(j,&s2);
486 xbt_assert2 (!strcmp(buf,s2),
487 "The retrieved value is not the same than the injected one (%s!=%s)",
498 GRAS_DEFINE_TYPE(s_pbio,
499 struct s_pbio{ /* structure presented in the IEEE article */
510 double Cdfgrd0[373][3];
511 double Cdfgrd1[3][3];
513 double Cddsdde[106][106];
516 typedef struct s_pbio pbio_t;
518 void test_pbio(gras_socket_t sock, int direction) {
521 gras_datadesc_type_t pbio_type;
524 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
525 pbio_type = gras_datadesc_by_symbol(s_pbio);
527 /* Fill in that damn struct */
529 for (cpt=0; cpt<12; cpt++)
530 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
532 for (cpt=0; cpt<110; cpt++)
533 i.Cprops[cpt] = cpt * 100.0;
534 for (cpt=0; cpt<4; cpt++)
535 i.Cndi[cpt] = cpt * 23262;
538 i.Cdtime = -11515.662;
539 i.Ctime[0] = 332523.226;
540 i.Ctime[1] = -26216.113;
541 i.Cntens = 235211411;
543 for (cpt=0; cpt<3; cpt++) {
544 for (cpt2=0; cpt2<373; cpt2++)
545 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
546 for (cpt2=0; cpt2<3; cpt2++)
547 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
549 for (cpt=0; cpt<106; cpt++) {
550 i.Cstress[cpt]=(double)cpt * 22.113;
551 for (cpt2=0; cpt2<106; cpt2++)
552 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
554 write_read(gras_datadesc_by_symbol(s_pbio),
555 &i,&j, sock,direction);
556 if (direction == READ || direction == RW || direction == COPY) {
557 /* Check that the data match */
558 xbt_assert(i.Cnstatv == j.Cnstatv);
559 for (cpt=0; cpt<12; cpt++)
560 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
561 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
562 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
563 xbt_assert(i.Cnprops == j.Cnprops);
564 for (cpt=0; cpt<110; cpt++)
565 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
566 for (cpt=0; cpt<4; cpt++)
567 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
568 xbt_assert(i.Cnshr == j.Cnshr);
569 xbt_assert(i.Cnpt == j.Cnpt);
570 xbt_assert(i.Cdtime == j.Cdtime);
571 xbt_assert(i.Ctime[0] == j.Ctime[0]);
572 xbt_assert(i.Ctime[1] == j.Ctime[1]);
573 xbt_assert(i.Cntens == j.Cntens);
574 for (cpt=0; cpt<3; cpt++) {
575 for (cpt2=0; cpt2<373; cpt2++)
576 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
577 for (cpt2=0; cpt2<3; cpt2++)
578 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
580 for (cpt=0; cpt<106; cpt++) {
581 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
582 for (cpt2=0; cpt2<106; cpt2++)
583 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
584 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
585 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
590 GRAS_DEFINE_TYPE(s_clause,
593 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
595 typedef struct s_clause Clause;
597 void test_clause(gras_socket_t sock, int direction) {
598 gras_datadesc_type_t ddt;
602 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
604 /* create and fill the struct */
608 i->literals = xbt_new(int, i->num_lits);
609 for (cpt=0; cpt<i->num_lits; cpt++)
610 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
611 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
612 DEBUG1("created count=%d",i->num_lits);
614 /* create the damn type descriptor */
615 ddt = gras_datadesc_by_symbol(s_clause);
616 /* gras_datadesc_type_dump(ddt); */
618 ddt=gras_datadesc_ref("Clause*",ddt);
620 write_read(ddt, &i,&j, sock,direction);
621 if (direction == READ || direction == RW || direction == COPY) {
622 xbt_assert(i->num_lits == j->num_lits);
623 for (cpt=0; cpt<i->num_lits; cpt++)
624 xbt_assert(i->literals[cpt] == j->literals[cpt]);
635 int main(int argc,char *argv[])
637 gras_socket_t sock=NULL;
638 int direction =WRITE ; /* RW; */
640 char r_arch_char = gras_arch_selfid();
642 gras_init(&argc,argv);
644 for (cpt=1; cpt<argc; cpt++)
646 if (!strcmp(argv[cpt], "--read"))
650 else if (!strcmp(argv[cpt], "--write"))
654 else if (!strcmp(argv[cpt], "--copy"))
664 if (direction == WRITE)
666 sock = gras_socket_client_from_file(filename);
667 gras_datadesc_send(sock, gras_datadesc_by_name("char"),&r_arch_char);
669 if (direction == READ)
671 sock = gras_socket_server_from_file(filename);
672 gras_datadesc_recv(sock, gras_datadesc_by_name("char"),gras_arch_selfid(), &r_arch_char);
673 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()));
675 r_arch = (int)r_arch_char;
677 test_int(sock,direction);
679 test_float(sock,direction);
681 test_double(sock,direction);
683 test_array(sock,direction);
685 test_intref(sock,direction);
687 test_string(sock,direction);
689 test_dynar_scal(sock,direction);
691 test_structures(sock,direction);
693 test_homostruct(sock,direction);
695 test_hetestruct(sock,direction);
697 test_nestedstruct(sock,direction);
699 declare_chained_list_type();
701 test_chain_list(sock,direction);
703 test_graph(sock,direction);
705 test_dynar_ref(sock,direction);
707 test_pbio(sock,direction);
709 test_clause(sock,direction);
711 if (direction != RW && direction != COPY)
712 gras_socket_close(sock);