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)",
129 static void test_intref(gras_socket_t sock, int direction) {
135 INFO0("---- Test on a reference to an integer ----");
137 write_read("int*", &i,&j, sock,direction);
138 if (direction == READ || direction == COPY) {
139 xbt_assert2(*i == *j,"*i != *j (%d != %d)",*i,*j);
146 *** string (dynamic array)
148 static void test_string(gras_socket_t sock, int direction) {
149 char *i=xbt_strdup("Some data"), *j=NULL;
152 INFO0("---- Test on string (ref to dynamic array) ----");
153 write_read("string", &i,&j, sock,direction);
154 if (direction == READ || direction == COPY) {
155 for (cpt=0; cpt<strlen(i); cpt++) {
156 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
157 cpt,i[cpt],cpt,j[cpt]);
166 *** homogeneous struct
171 static void test_homostruct(gras_socket_t sock, int direction) {
174 INFO0("---- Test on homogeneous structure ----");
175 /* init a value, exchange it and check its validity*/
176 i=xbt_new(homostruct,1);
177 i->a = 2235; i->b = 433425;
178 i->c = -23423; i->d = -235235;
180 write_read("homostruct*", &i,&j, sock,direction);
181 if (direction == READ || direction == COPY) {
182 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
183 xbt_assert(i->b == j->b);
184 xbt_assert(i->c == j->c);
185 xbt_assert(i->d == j->d);
192 *** heterogeneous struct
196 unsigned long int l1;
198 unsigned long int l2;
200 static void test_hetestruct(gras_socket_t sock, int direction) {
203 INFO0("---- Test on heterogeneous structure ----");
204 /* init a value, exchange it and check its validity*/
205 i=xbt_new(hetestruct,1);
206 i->c1 = 's'; i->l1 = 123455;
207 i->c2 = 'e'; i->l2 = 774531;
209 write_read("hetestruct*", &i,&j, sock,direction);
210 if (direction == READ || direction == COPY) {
211 xbt_assert(i->c1 == j->c1);
212 xbt_assert(i->c2 == j->c2);
213 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
214 xbt_assert(i->l2 == j->l2);
227 static void test_nestedstruct(gras_socket_t sock, int direction) {
230 INFO0("---- Test on nested structures ----");
231 /* init a value, exchange it and check its validity*/
232 i=xbt_new(nestedstruct,1);
233 i->homo.a = 235231; i->homo.b = -124151;
234 i->homo.c = 211551; i->homo.d = -664222;
235 i->hete.c1 = 's'; i->hete.l1 = 123455;
236 i->hete.c2 = 'e'; i->hete.l2 = 774531;
238 write_read("nestedstruct*", &i,&j, sock,direction);
239 if (direction == READ || direction == COPY) {
240 xbt_assert(i->homo.a == j->homo.a);
241 xbt_assert(i->homo.b == j->homo.b);
242 xbt_assert(i->homo.c == j->homo.c);
243 xbt_assert(i->homo.d == j->homo.d);
244 xbt_assert(i->hete.c1 == j->hete.c1);
245 xbt_assert(i->hete.c2 == j->hete.c2);
246 xbt_assert(i->hete.l1 == j->hete.l1);
247 xbt_assert(i->hete.l2 == j->hete.l2);
256 typedef struct s_chained_list chained_list_t;
257 struct s_chained_list {
261 chained_list_t *cons(int v, chained_list_t *l);
262 void list_free(chained_list_t *l);
263 int list_eq(chained_list_t*i,chained_list_t*j);
265 chained_list_t * cons(int v, chained_list_t *l) {
266 chained_list_t *nl = xbt_new(chained_list_t,1);
273 void list_free(chained_list_t*l) {
279 int list_eq(chained_list_t*i,chained_list_t*j) {
280 if (!i || !j) return i == j;
283 return list_eq(i->l, j->l);
285 static void test_chain_list(gras_socket_t sock, int direction) {
286 chained_list_t *i, *j;
288 INFO0("---- Test on chained list ----");
290 /* init a value, exchange it and check its validity*/
291 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
294 write_read("chained_list_t*", &i,&j, sock,direction);
295 if (direction == READ || direction == COPY) {
296 xbt_assert(list_eq(i,j));
305 static void test_graph(gras_socket_t sock, int direction) {
306 chained_list_t *i, *j;
308 INFO0("---- Test on graph (cyclique chained list of 3 items) ----");
309 /* init a value, exchange it and check its validity*/
310 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
314 write_read("chained_list_t*", &i,&j, sock,direction);
315 if (direction == READ || direction == COPY) {
318 DEBUG1("i->l=%p" ,i->l);
319 DEBUG1("i->l->l=%p" ,i->l->l);
320 DEBUG1("i->l->l->l=%p",i->l->l->l);
322 DEBUG1("j->l=%p" ,j->l);
323 DEBUG1("j->l->l=%p" ,j->l->l);
324 DEBUG1("j->l->l->l=%p",j->l->l->l);
325 xbt_assert4(j->l->l->l == j,
326 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
332 xbt_assert(list_eq(i,j));
336 i->l->l->l = NULL; /* do this even in WRITE mode */
341 /*** Dynar of references ***/
342 static void free_string(void *d){ /* used to free the data in dynar */
345 static void test_dynar_ref(gras_socket_t sock, int direction){
351 INFO0("---- Test on dynar containing integers ----");
353 i=xbt_dynar_new(sizeof(char*),&free_string);
354 for (cpt=0; cpt< 64; cpt++) {
355 sprintf(buf,"%d",cpt);
357 xbt_dynar_push(i,&s1);
360 write_read("xbt_dynar_of_string", &i,&j, sock, direction);
361 if (direction == READ || direction == COPY) {
362 for (cpt=0; cpt< 64; cpt++) {
363 sprintf(buf,"%d",cpt);
364 xbt_dynar_shift(j,&s2);
365 xbt_assert2 (!strcmp(buf,s2),
366 "The retrieved value is not the same than the injected one (%s!=%s)",
377 GRAS_DEFINE_TYPE(s_pbio,
378 struct s_pbio{ /* structure presented in the IEEE article */
389 double Cdfgrd0[373][3];
390 double Cdfgrd1[3][3];
392 double Cddsdde[106][106];
395 typedef struct s_pbio pbio_t;
397 static void test_pbio(gras_socket_t sock, int direction) {
400 gras_datadesc_type_t pbio_type;
403 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
404 pbio_type = gras_datadesc_by_symbol(s_pbio);
406 /* Fill in that damn struct */
408 for (cpt=0; cpt<12; cpt++)
409 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
411 for (cpt=0; cpt<110; cpt++)
412 i.Cprops[cpt] = cpt * 100.0;
413 for (cpt=0; cpt<4; cpt++)
414 i.Cndi[cpt] = cpt * 23262;
417 i.Cdtime = -11515.662;
418 i.Ctime[0] = 332523.226;
419 i.Ctime[1] = -26216.113;
420 i.Cntens = 235211411;
422 for (cpt=0; cpt<3; cpt++) {
423 for (cpt2=0; cpt2<373; cpt2++)
424 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
425 for (cpt2=0; cpt2<3; cpt2++)
426 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
428 for (cpt=0; cpt<106; cpt++) {
429 i.Cstress[cpt]=(double)cpt * 22.113;
430 for (cpt2=0; cpt2<106; cpt2++)
431 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
433 write_read("s_pbio", &i,&j, sock,direction);
434 if (direction == READ || direction == COPY) {
435 /* Check that the data match */
436 xbt_assert(i.Cnstatv == j.Cnstatv);
437 for (cpt=0; cpt<12; cpt++)
438 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
439 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
440 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
441 xbt_assert(i.Cnprops == j.Cnprops);
442 for (cpt=0; cpt<110; cpt++)
443 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
444 for (cpt=0; cpt<4; cpt++)
445 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
446 xbt_assert(i.Cnshr == j.Cnshr);
447 xbt_assert(i.Cnpt == j.Cnpt);
448 xbt_assert(i.Cdtime == j.Cdtime);
449 xbt_assert(i.Ctime[0] == j.Ctime[0]);
450 xbt_assert(i.Ctime[1] == j.Ctime[1]);
451 xbt_assert(i.Cntens == j.Cntens);
452 for (cpt=0; cpt<3; cpt++) {
453 for (cpt2=0; cpt2<373; cpt2++)
454 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
455 for (cpt2=0; cpt2<3; cpt2++)
456 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
458 for (cpt=0; cpt<106; cpt++) {
459 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
460 for (cpt2=0; cpt2<106; cpt2++)
461 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
462 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
463 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
468 GRAS_DEFINE_TYPE(s_clause,
471 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
473 typedef struct s_clause Clause;
475 static void test_clause(gras_socket_t sock, int direction) {
479 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
481 /* create and fill the struct */
485 i->literals = xbt_new(int, i->num_lits);
486 for (cpt=0; cpt<i->num_lits; cpt++)
487 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
488 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
489 DEBUG1("created count=%d",i->num_lits);
491 write_read("Clause*", &i,&j, sock,direction);
492 if (direction == READ || direction == COPY) {
493 xbt_assert(i->num_lits == j->num_lits);
494 for (cpt=0; cpt<i->num_lits; cpt++)
495 xbt_assert(i->literals[cpt] == j->literals[cpt]);
504 static void register_types(void) {
505 gras_datadesc_type_t my_type,ref_my_type;
507 gras_msgtype_declare("char",gras_datadesc_by_name("char"));
508 gras_msgtype_declare("int",gras_datadesc_by_name("int"));
509 gras_msgtype_declare("float",gras_datadesc_by_name("float"));
510 gras_msgtype_declare("double",gras_datadesc_by_name("double"));
512 my_type=gras_datadesc_array_fixed("fixed int array",
513 gras_datadesc_by_name("int"),
515 gras_msgtype_declare("fixed int array",my_type);
517 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
518 gras_msgtype_declare("xbt_dynar_of_int",my_type);
520 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
521 gras_msgtype_declare("int*",my_type);
523 gras_msgtype_declare("string",gras_datadesc_by_name("string"));
525 my_type=gras_datadesc_struct("homostruct");
526 gras_datadesc_struct_append(my_type,"a",gras_datadesc_by_name("signed int"));
527 gras_datadesc_struct_append(my_type,"b",gras_datadesc_by_name("int"));
528 gras_datadesc_struct_append(my_type,"c",gras_datadesc_by_name("int"));
529 gras_datadesc_struct_append(my_type,"d",gras_datadesc_by_name("int"));
530 gras_datadesc_struct_close(my_type);
531 my_type=gras_datadesc_ref("homostruct*",gras_datadesc_by_name("homostruct"));
532 gras_msgtype_declare("homostruct*",my_type);
534 my_type=gras_datadesc_struct("hetestruct");
535 gras_datadesc_struct_append(my_type,"c1", gras_datadesc_by_name("unsigned char"));
536 gras_datadesc_struct_append(my_type,"l1", gras_datadesc_by_name("unsigned long int"));
537 gras_datadesc_struct_append(my_type,"c2", gras_datadesc_by_name("unsigned char"));
538 gras_datadesc_struct_append(my_type,"l2", gras_datadesc_by_name("unsigned long int"));
539 gras_datadesc_struct_close(my_type);
540 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
541 gras_msgtype_declare("hetestruct*",my_type);
543 my_type=gras_datadesc_struct("nestedstruct");
544 gras_datadesc_struct_append(my_type,"hete",gras_datadesc_by_name("hetestruct"));
545 gras_datadesc_struct_append(my_type,"homo",gras_datadesc_by_name("homostruct"));
546 gras_datadesc_struct_close(my_type);
547 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
548 gras_msgtype_declare("nestedstruct*",my_type);
550 my_type=gras_datadesc_struct("chained_list_t");
551 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
552 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
553 gras_datadesc_struct_append(my_type,"l", ref_my_type);
554 gras_datadesc_struct_close(my_type);
555 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
556 gras_msgtype_declare("chained_list_t",my_type);
557 gras_msgtype_declare("chained_list_t*",ref_my_type);
559 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
560 gras_msgtype_declare("xbt_dynar_of_string",my_type);
562 my_type = gras_datadesc_by_symbol(s_pbio);
563 gras_msgtype_declare("s_pbio",my_type);
565 my_type = gras_datadesc_by_symbol(s_clause);
566 my_type = gras_datadesc_ref("Clause*",my_type);
567 gras_msgtype_declare("Clause*",my_type);
575 int main(int argc,char *argv[]) {
576 gras_socket_t sock=NULL;
577 int direction = WRITE;
579 char local_arch, remote_arch;
581 gras_init(&argc,argv);
583 register_structures();
585 for (cpt=1; cpt<argc; cpt++) {
586 if (!strcmp(argv[cpt], "--read")) {
588 } else if (!strcmp(argv[cpt], "--write")) {
590 } else if (!strcmp(argv[cpt], "--copy")) {
597 if (direction == WRITE) {
598 INFO1("Write to file %s",filename);
599 sock = gras_socket_client_from_file(filename);
601 if (direction == READ) {
602 INFO1("Read from file %s",filename);
603 sock = gras_socket_server_from_file(filename);
605 if (direction == COPY) {
606 INFO0("Memory copy");
609 local_arch = gras_arch_selfid();
610 write_read("char", &local_arch,&remote_arch, sock,direction);
611 if (direction == READ)
612 VERB2("This file was generated on %s (%d)",
613 gras_datadesc_arch_name(remote_arch),(int)remote_arch);
616 test_int(sock,direction);
617 test_float(sock,direction);
618 test_double(sock,direction);
619 test_array(sock,direction);
620 test_intref(sock,direction);
622 test_string(sock,direction);
623 test_dynar_scal(sock,direction);
625 test_structures(sock,direction);
627 test_homostruct(sock,direction);
628 test_hetestruct(sock,direction);
629 test_nestedstruct(sock,direction);
631 test_chain_list(sock,direction);
632 test_graph(sock,direction);
633 test_dynar_ref(sock,direction);
635 test_pbio(sock,direction);
637 test_clause(sock,direction);
639 if (direction != COPY)
640 gras_socket_close(sock);