1 /* datadesc: test of data description (using file transport). */
3 /* Copyright (c) 2007, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "..\..\..\..\include\gras\datadesc.h"
17 #include "gras/DataDesc/datadesc_interface.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
26 const char *filename = "datadesc_usage.out";
29 write_read(gras_datadesc_type_t type,void *src, void *dst, gras_socket_t sock, int direction);
32 write_read(gras_datadesc_type_t type,void *src, void *dst, gras_socket_t sock, int direction)
37 sock = gras_socket_client_from_file(filename);
39 if (direction == WRITE || direction == RW)
40 gras_datadesc_send(sock, type, src);
43 gras_socket_close(sock);
47 sock = gras_socket_server_from_file(filename);
49 if (direction == READ || direction == RW)
50 gras_datadesc_recv(sock, type, r_arch, dst);
53 gras_socket_close(sock);
55 if (direction == COPY)
56 gras_datadesc_copy(type, src, dst);
59 void test_int(gras_socket_t sock, int direction);
60 void test_float(gras_socket_t sock, int direction);
61 void test_double(gras_socket_t sock, int direction);
62 void test_array(gras_socket_t sock, int direction);
63 void test_dynar_scal(gras_socket_t sock, int direction);
64 void test_intref(gras_socket_t sock, int direction);
65 void test_string(gras_socket_t sock, int direction);
67 void test_homostruct(gras_socket_t sock, int direction);
68 void test_hetestruct(gras_socket_t sock, int direction);
69 void test_nestedstruct(gras_socket_t sock, int direction);
70 void test_chain_list(gras_socket_t sock, int direction);
71 void test_graph(gras_socket_t sock, int direction);
72 void test_dynar_ref(gras_socket_t sock, int direction);
74 void test_pbio(gras_socket_t sock, int direction);
75 void test_clause(gras_socket_t sock, int direction);
77 /* defined in datadesc_structures.c, which in perl generated */
78 void test_structures(gras_socket_t sock, int direction);
80 void test_int(gras_socket_t sock, int direction)
84 INFO0("---- Test on integer ----");
86 write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction);
88 if (direction == READ || direction == RW || direction == COPY)
91 void test_float(gras_socket_t sock, int direction)
95 INFO0("---- Test on float ----");
96 write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction);
98 if (direction == READ || direction == RW || direction == COPY)
99 xbt_assert2(i == j,"%f != %f",i,j);
101 void test_double(gras_socket_t sock, int direction)
103 double i=-3252355.1234,j;
105 INFO0("---- Test on double ----");
106 write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction);
108 if (direction == READ || direction == RW || direction == COPY)
109 xbt_assert2(i == j,"%f != %f",i,j);
113 typedef int array[SIZE];
115 void test_array(gras_socket_t sock, int direction)
117 gras_datadesc_type_t my_type;
119 array i = { 35212,-6226,74337,11414,7733};
123 INFO0("---- Test on fixed array ----");
125 my_type=gras_datadesc_array_fixed("fixed int array", gras_datadesc_by_name("int"),SIZE);
127 write_read(my_type, &i,&j, sock,direction);
129 if (direction == READ || direction == RW || direction == COPY)
131 for (cpt=0; cpt<SIZE; cpt++)
133 DEBUG1("Test spot %d",cpt);
134 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",cpt,i[cpt],cpt,j[cpt]);
138 /*** Dynar of scalar ***/
140 void test_dynar_scal(gras_socket_t sock, int direction)
142 gras_datadesc_type_t my_type;
146 INFO0("---- Test on dynar containing integers ----");
147 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
148 i = xbt_dynar_new(sizeof(int),NULL);
150 for (cpt=0; cpt<64; cpt++)
152 xbt_dynar_push_as(i,int,cpt);
153 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(i));
156 /* xbt_dynar_dump(i);*/
157 write_read(my_type, &i,&j, sock, direction);
159 /* xbt_dynar_dump(j);*/
160 if (direction == READ || direction == RW || direction == COPY)
162 for (cpt=0; cpt<64; cpt++)
164 int ret=xbt_dynar_get_as(j,cpt,int);
168 CRITICAL3("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",cpt,ret,cpt);
179 void test_intref(gras_socket_t sock, int direction)
181 gras_datadesc_type_t my_type;
187 INFO1("---- Test on a reference to an integer (%p) ----",i);
189 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
191 write_read(my_type, &i,&j, sock,direction);
192 if (direction == READ || direction == RW || direction == COPY)
194 xbt_assert2(*i == *j,"*i != *j (%d != %d)",*i,*j);
202 *** string (dynamic array)
204 void test_string(gras_socket_t sock, int direction)
206 char *i=xbt_strdup("Some data"), *j=NULL;
209 INFO0("---- Test on string (ref to dynamic array) ----");
210 write_read(gras_datadesc_by_name("string"), &i,&j,sock,direction);
212 if (direction == READ || direction == RW || direction == COPY)
214 for (cpt=0; cpt<strlen(i); cpt++)
216 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",cpt,i[cpt],cpt,j[cpt]);
227 *** homogeneous struct
234 void test_homostruct(gras_socket_t sock, int direction)
236 gras_datadesc_type_t my_type;
239 INFO0("---- Test on homogeneous structure ----");
240 /* create descriptor */
241 my_type=gras_datadesc_struct("homostruct");
242 gras_datadesc_struct_append(my_type,"a",
243 gras_datadesc_by_name("signed int"));
244 gras_datadesc_struct_append(my_type,"b",
245 gras_datadesc_by_name("int"));
246 gras_datadesc_struct_append(my_type,"c",
247 gras_datadesc_by_name("int"));
248 gras_datadesc_struct_append(my_type,"d",
249 gras_datadesc_by_name("int"));
250 gras_datadesc_struct_close(my_type);
251 my_type=gras_datadesc_ref("homostruct*",
252 gras_datadesc_by_name("homostruct"));
254 /* init a value, exchange it and check its validity*/
255 i=xbt_new(homostruct,1);
256 i->a = 2235; i->b = 433425;
257 i->c = -23423; i->d = -235235;
259 write_read(my_type, &i,&j, sock,direction);
260 if (direction == READ || direction == RW || direction == COPY) {
261 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
262 xbt_assert(i->b == j->b);
263 xbt_assert(i->c == j->c);
264 xbt_assert(i->d == j->d);
271 *** heterogeneous struct
275 unsigned long int l1;
277 unsigned long int l2;
279 void test_hetestruct(gras_socket_t sock, int direction) {
280 gras_datadesc_type_t my_type;
283 INFO0("---- Test on heterogeneous structure ----");
284 /* create descriptor */
285 my_type=gras_datadesc_struct("hetestruct");
286 gras_datadesc_struct_append(my_type,"c1",
287 gras_datadesc_by_name("unsigned char"));
288 gras_datadesc_struct_append(my_type,"l1",
289 gras_datadesc_by_name("unsigned long int"));
290 gras_datadesc_struct_append(my_type,"c2",
291 gras_datadesc_by_name("unsigned char"));
292 gras_datadesc_struct_append(my_type,"l2",
293 gras_datadesc_by_name("unsigned long int"));
294 gras_datadesc_struct_close(my_type);
295 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
298 /* init a value, exchange it and check its validity*/
299 i=xbt_new(hetestruct,1);
300 i->c1 = 's'; i->l1 = 123455;
301 i->c2 = 'e'; i->l2 = 774531;
303 write_read(my_type, &i,&j, sock,direction);
304 if (direction == READ || direction == RW || direction == COPY) {
305 xbt_assert(i->c1 == j->c1);
306 xbt_assert(i->c2 == j->c2);
307 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
308 xbt_assert(i->l2 == j->l2);
321 void test_nestedstruct(gras_socket_t sock, int direction) {
322 gras_datadesc_type_t my_type;
325 INFO0("---- Test on nested structures ----");
326 /* create descriptor */
327 my_type=gras_datadesc_struct("nestedstruct");
329 gras_datadesc_struct_append(my_type,"hete",
330 gras_datadesc_by_name("hetestruct"));
331 gras_datadesc_struct_append(my_type,"homo",
332 gras_datadesc_by_name("homostruct"));
333 gras_datadesc_struct_close(my_type);
334 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
336 /* init a value, exchange it and check its validity*/
337 i=xbt_new(nestedstruct,1);
338 i->homo.a = 235231; i->homo.b = -124151;
339 i->homo.c = 211551; i->homo.d = -664222;
340 i->hete.c1 = 's'; i->hete.l1 = 123455;
341 i->hete.c2 = 'e'; i->hete.l2 = 774531;
343 write_read(my_type, &i,&j, sock,direction);
344 if (direction == READ || direction == RW || direction == COPY) {
345 xbt_assert(i->homo.a == j->homo.a);
346 xbt_assert(i->homo.b == j->homo.b);
347 xbt_assert(i->homo.c == j->homo.c);
348 xbt_assert(i->homo.d == j->homo.d);
349 xbt_assert(i->hete.c1 == j->hete.c1);
350 xbt_assert(i->hete.c2 == j->hete.c2);
351 xbt_assert(i->hete.l1 == j->hete.l1);
352 xbt_assert(i->hete.l2 == j->hete.l2);
361 typedef struct s_chained_list chained_list_t;
362 struct s_chained_list {
366 void declare_chained_list_type(void);
367 chained_list_t *cons(int v, chained_list_t *l);
368 void list_free(chained_list_t *l);
369 int list_eq(chained_list_t*i,chained_list_t*j);
371 void declare_chained_list_type(void) {
372 gras_datadesc_type_t my_type,ref_my_type;
374 my_type=gras_datadesc_struct("chained_list_t");
375 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
377 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
378 gras_datadesc_struct_append(my_type,"l", ref_my_type);
379 gras_datadesc_struct_close(my_type);
382 chained_list_t * cons(int v, chained_list_t *l) {
383 chained_list_t *nl = xbt_new(chained_list_t,1);
390 void list_free(chained_list_t*l) {
396 int list_eq(chained_list_t*i,chained_list_t*j) {
397 if (!i || !j) return i == j;
400 return list_eq(i->l, j->l);
402 void test_chain_list(gras_socket_t sock, int direction) {
403 chained_list_t *i, *j;
405 INFO0("---- Test on chained list ----");
407 /* init a value, exchange it and check its validity*/
408 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
411 write_read(gras_datadesc_by_name("chained_list_t*"),
412 &i,&j, sock,direction);
413 if (direction == READ || direction == RW || direction == COPY) {
414 xbt_assert(list_eq(i,j));
423 void test_graph(gras_socket_t sock, int direction) {
424 chained_list_t *i, *j;
426 INFO0("---- Test on graph (cyclique chained list of 3 items) ----");
427 /* init a value, exchange it and check its validity*/
428 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
432 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
433 write_read(gras_datadesc_by_name("chained_list_t*"),
434 &i,&j, sock,direction);
435 if (direction == READ || direction == RW || direction == COPY) {
438 DEBUG1("i->l=%p" ,i->l);
439 DEBUG1("i->l->l=%p" ,i->l->l);
440 DEBUG1("i->l->l->l=%p",i->l->l->l);
442 DEBUG1("j->l=%p" ,j->l);
443 DEBUG1("j->l->l=%p" ,j->l->l);
444 DEBUG1("j->l->l->l=%p",j->l->l->l);
445 xbt_assert4(j->l->l->l == j,
446 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
452 xbt_assert(list_eq(i,j));
456 i->l->l->l = NULL; /* do this even in WRITE mode */
461 /*** Dynar of references ***/
462 static void free_string(void *d){ /* used to free the data in dynar */
465 void test_dynar_ref(gras_socket_t sock, int direction){
466 gras_datadesc_type_t my_type;
472 INFO0("---- Test on dynar containing integers ----");
473 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
475 i=xbt_dynar_new(sizeof(char*),&free_string);
476 for (cpt=0; cpt< 64; cpt++) {
477 sprintf(buf,"%d",cpt);
479 xbt_dynar_push(i,&s1);
482 write_read(my_type, &i,&j, sock, direction);
483 if (direction == READ || direction == RW || direction == COPY) {
484 for (cpt=0; cpt< 64; cpt++) {
485 sprintf(buf,"%d",cpt);
486 xbt_dynar_shift(j,&s2);
487 xbt_assert2 (!strcmp(buf,s2),
488 "The retrieved value is not the same than the injected one (%s!=%s)",
499 GRAS_DEFINE_TYPE(s_pbio,
500 struct s_pbio{ /* structure presented in the IEEE article */
511 double Cdfgrd0[373][3];
512 double Cdfgrd1[3][3];
514 double Cddsdde[106][106];
517 typedef struct s_pbio pbio_t;
519 void test_pbio(gras_socket_t sock, int direction) {
522 gras_datadesc_type_t pbio_type;
525 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
526 pbio_type = gras_datadesc_by_symbol(s_pbio);
528 /* Fill in that damn struct */
530 for (cpt=0; cpt<12; cpt++)
531 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
533 for (cpt=0; cpt<110; cpt++)
534 i.Cprops[cpt] = cpt * 100.0;
535 for (cpt=0; cpt<4; cpt++)
536 i.Cndi[cpt] = cpt * 23262;
539 i.Cdtime = -11515.662;
540 i.Ctime[0] = 332523.226;
541 i.Ctime[1] = -26216.113;
542 i.Cntens = 235211411;
544 for (cpt=0; cpt<3; cpt++) {
545 for (cpt2=0; cpt2<373; cpt2++)
546 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
547 for (cpt2=0; cpt2<3; cpt2++)
548 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
550 for (cpt=0; cpt<106; cpt++) {
551 i.Cstress[cpt]=(double)cpt * 22.113;
552 for (cpt2=0; cpt2<106; cpt2++)
553 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
555 write_read(gras_datadesc_by_symbol(s_pbio),
556 &i,&j, sock,direction);
557 if (direction == READ || direction == RW || direction == COPY) {
558 /* Check that the data match */
559 xbt_assert(i.Cnstatv == j.Cnstatv);
560 for (cpt=0; cpt<12; cpt++)
561 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
562 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
563 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
564 xbt_assert(i.Cnprops == j.Cnprops);
565 for (cpt=0; cpt<110; cpt++)
566 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
567 for (cpt=0; cpt<4; cpt++)
568 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
569 xbt_assert(i.Cnshr == j.Cnshr);
570 xbt_assert(i.Cnpt == j.Cnpt);
571 xbt_assert(i.Cdtime == j.Cdtime);
572 xbt_assert(i.Ctime[0] == j.Ctime[0]);
573 xbt_assert(i.Ctime[1] == j.Ctime[1]);
574 xbt_assert(i.Cntens == j.Cntens);
575 for (cpt=0; cpt<3; cpt++) {
576 for (cpt2=0; cpt2<373; cpt2++)
577 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
578 for (cpt2=0; cpt2<3; cpt2++)
579 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
581 for (cpt=0; cpt<106; cpt++) {
582 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
583 for (cpt2=0; cpt2<106; cpt2++)
584 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
585 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
586 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
591 GRAS_DEFINE_TYPE(s_clause,
594 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
596 typedef struct s_clause Clause;
598 void test_clause(gras_socket_t sock, int direction) {
599 gras_datadesc_type_t ddt;
603 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
605 /* create and fill the struct */
609 i->literals = xbt_new(int, i->num_lits);
610 for (cpt=0; cpt<i->num_lits; cpt++)
611 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
612 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
613 DEBUG1("created count=%d",i->num_lits);
615 /* create the damn type descriptor */
616 ddt = gras_datadesc_by_symbol(s_clause);
617 /* gras_datadesc_type_dump(ddt); */
619 ddt=gras_datadesc_ref("Clause*",ddt);
621 write_read(ddt, &i,&j, sock,direction);
622 if (direction == READ || direction == RW || direction == COPY) {
623 xbt_assert(i->num_lits == j->num_lits);
624 for (cpt=0; cpt<i->num_lits; cpt++)
625 xbt_assert(i->literals[cpt] == j->literals[cpt]);
636 int main(int argc,char *argv[])
638 gras_socket_t sock=NULL;
639 int direction =WRITE ; /* RW; */
641 char r_arch_char = gras_arch_selfid();
643 gras_init(&argc,argv);
645 for (cpt=1; cpt<argc; cpt++)
647 if (!strcmp(argv[cpt], "--read"))
651 else if (!strcmp(argv[cpt], "--write"))
655 else if (!strcmp(argv[cpt], "--copy"))
665 if (direction == WRITE)
667 sock = gras_socket_client_from_file(filename);
668 gras_datadesc_send(sock, gras_datadesc_by_name("char"),&r_arch_char);
670 if (direction == READ)
672 sock = gras_socket_server_from_file(filename);
673 gras_datadesc_recv(sock, gras_datadesc_by_name("char"),gras_arch_selfid(), &r_arch_char);
674 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()));
676 r_arch = (int)r_arch_char;
678 test_int(sock,direction);
680 test_float(sock,direction);
682 test_double(sock,direction);
684 test_array(sock,direction);
686 test_intref(sock,direction);
688 test_string(sock,direction);
690 test_dynar_scal(sock,direction);
692 test_structures(sock,direction);
694 test_homostruct(sock,direction);
696 test_hetestruct(sock,direction);
698 test_nestedstruct(sock,direction);
700 declare_chained_list_type();
702 test_chain_list(sock,direction);
704 test_graph(sock,direction);
706 test_dynar_ref(sock,direction);
708 test_pbio(sock,direction);
710 test_clause(sock,direction);
712 if (direction != RW && direction != COPY)
713 gras_socket_close(sock);