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. */
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,
30 gras_socket_t sock, int direction);
33 write_read(gras_datadesc_type_t type,void *src, void *dst,
34 gras_socket_t sock, int direction) {
38 sock = gras_socket_client_from_file(filename);
39 if (direction == WRITE || direction == RW)
40 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);
81 void test_int(gras_socket_t sock, int direction) {
84 INFO0("---- Test on integer ----");
85 write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction);
86 if (direction == READ || direction == RW || direction == COPY)
89 void test_float(gras_socket_t sock, int direction) {
92 INFO0("---- Test on float ----");
93 write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction);
94 if (direction == READ || direction == RW || direction == COPY)
95 xbt_assert2(i == j,"%f != %f",i,j);
97 void test_double(gras_socket_t sock, int direction) {
98 double i=-3252355.1234,j;
100 INFO0("---- Test on double ----");
101 write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction);
102 if (direction == READ || direction == RW || direction == COPY)
103 xbt_assert2(i == j,"%f != %f",i,j);
107 typedef int array[SIZE];
108 void test_array(gras_socket_t sock, int direction) {
109 gras_datadesc_type_t my_type;
111 array i = { 35212,-6226,74337,11414,7733};
115 INFO0("---- Test on fixed array ----");
117 my_type=gras_datadesc_array_fixed("fixed int array",
118 gras_datadesc_by_name("int"),
121 write_read(my_type, &i,&j, sock,direction);
122 if (direction == READ || direction == RW || direction == COPY) {
123 for (cpt=0; cpt<SIZE; cpt++) {
124 DEBUG1("Test spot %d",cpt);
125 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
126 cpt,i[cpt],cpt,j[cpt]);
130 /*** Dynar of scalar ***/
132 void test_dynar_scal(gras_socket_t sock, int direction){
133 gras_datadesc_type_t my_type;
137 INFO0("---- Test on dynar containing integers ----");
138 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
139 i = xbt_dynar_new(sizeof(int),NULL);
140 for (cpt=0; cpt<64; cpt++) {
141 xbt_dynar_push_as(i,int,cpt);
142 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(i));
144 /* xbt_dynar_dump(i);*/
145 write_read(my_type, &i,&j, sock, direction);
146 /* xbt_dynar_dump(j);*/
147 if (direction == READ || direction == RW || direction == COPY) {
148 for (cpt=0; cpt<64; cpt++){
149 int ret=xbt_dynar_get_as(j,cpt,int);
151 CRITICAL3("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",
160 void test_intref(gras_socket_t sock, int direction) {
161 gras_datadesc_type_t my_type;
167 INFO1("---- Test on a reference to an integer (%p) ----",i);
169 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
171 write_read(my_type, &i,&j, sock,direction);
172 if (direction == READ || direction == RW || direction == COPY) {
173 xbt_assert2(*i == *j,"*i != *j (%d != %d)",*i,*j);
180 *** string (dynamic array)
182 void test_string(gras_socket_t sock, int direction) {
183 char *i=xbt_strdup("Some data"), *j=NULL;
186 INFO0("---- Test on string (ref to dynamic array) ----");
187 write_read(gras_datadesc_by_name("string"), &i,&j,
189 if (direction == READ || direction == RW || direction == COPY) {
190 for (cpt=0; cpt<strlen(i); cpt++) {
191 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
192 cpt,i[cpt],cpt,j[cpt]);
201 *** homogeneous struct
206 void test_homostruct(gras_socket_t sock, int direction) {
207 gras_datadesc_type_t my_type;
210 INFO0("---- Test on homogeneous structure ----");
211 /* create descriptor */
212 my_type=gras_datadesc_struct("homostruct");
213 gras_datadesc_struct_append(my_type,"a",
214 gras_datadesc_by_name("signed int"));
215 gras_datadesc_struct_append(my_type,"b",
216 gras_datadesc_by_name("int"));
217 gras_datadesc_struct_append(my_type,"c",
218 gras_datadesc_by_name("int"));
219 gras_datadesc_struct_append(my_type,"d",
220 gras_datadesc_by_name("int"));
221 gras_datadesc_struct_close(my_type);
222 my_type=gras_datadesc_ref("homostruct*",
223 gras_datadesc_by_name("homostruct"));
225 /* init a value, exchange it and check its validity*/
226 i=xbt_new(homostruct,1);
227 i->a = 2235; i->b = 433425;
228 i->c = -23423; i->d = -235235;
230 write_read(my_type, &i,&j, sock,direction);
231 if (direction == READ || direction == RW || direction == COPY) {
232 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
233 xbt_assert(i->b == j->b);
234 xbt_assert(i->c == j->c);
235 xbt_assert(i->d == j->d);
242 *** heterogeneous struct
246 unsigned long int l1;
248 unsigned long int l2;
250 void test_hetestruct(gras_socket_t sock, int direction) {
251 gras_datadesc_type_t my_type;
254 INFO0("---- Test on heterogeneous structure ----");
255 /* create descriptor */
256 my_type=gras_datadesc_struct("hetestruct");
257 gras_datadesc_struct_append(my_type,"c1",
258 gras_datadesc_by_name("unsigned char"));
259 gras_datadesc_struct_append(my_type,"l1",
260 gras_datadesc_by_name("unsigned long int"));
261 gras_datadesc_struct_append(my_type,"c2",
262 gras_datadesc_by_name("unsigned char"));
263 gras_datadesc_struct_append(my_type,"l2",
264 gras_datadesc_by_name("unsigned long int"));
265 gras_datadesc_struct_close(my_type);
266 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
269 /* init a value, exchange it and check its validity*/
270 i=xbt_new(hetestruct,1);
271 i->c1 = 's'; i->l1 = 123455;
272 i->c2 = 'e'; i->l2 = 774531;
274 write_read(my_type, &i,&j, sock,direction);
275 if (direction == READ || direction == RW || direction == COPY) {
276 xbt_assert(i->c1 == j->c1);
277 xbt_assert(i->c2 == j->c2);
278 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
279 xbt_assert(i->l2 == j->l2);
292 void test_nestedstruct(gras_socket_t sock, int direction) {
293 gras_datadesc_type_t my_type;
296 INFO0("---- Test on nested structures ----");
297 /* create descriptor */
298 my_type=gras_datadesc_struct("nestedstruct");
300 gras_datadesc_struct_append(my_type,"hete",
301 gras_datadesc_by_name("hetestruct"));
302 gras_datadesc_struct_append(my_type,"homo",
303 gras_datadesc_by_name("homostruct"));
304 gras_datadesc_struct_close(my_type);
305 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
307 /* init a value, exchange it and check its validity*/
308 i=xbt_new(nestedstruct,1);
309 i->homo.a = 235231; i->homo.b = -124151;
310 i->homo.c = 211551; i->homo.d = -664222;
311 i->hete.c1 = 's'; i->hete.l1 = 123455;
312 i->hete.c2 = 'e'; i->hete.l2 = 774531;
314 write_read(my_type, &i,&j, sock,direction);
315 if (direction == READ || direction == RW || direction == COPY) {
316 xbt_assert(i->homo.a == j->homo.a);
317 xbt_assert(i->homo.b == j->homo.b);
318 xbt_assert(i->homo.c == j->homo.c);
319 xbt_assert(i->homo.d == j->homo.d);
320 xbt_assert(i->hete.c1 == j->hete.c1);
321 xbt_assert(i->hete.c2 == j->hete.c2);
322 xbt_assert(i->hete.l1 == j->hete.l1);
323 xbt_assert(i->hete.l2 == j->hete.l2);
332 typedef struct s_chained_list chained_list_t;
333 struct s_chained_list {
337 void declare_chained_list_type(void);
338 chained_list_t *cons(int v, chained_list_t *l);
339 void list_free(chained_list_t *l);
340 int list_eq(chained_list_t*i,chained_list_t*j);
342 void declare_chained_list_type(void) {
343 gras_datadesc_type_t my_type,ref_my_type;
345 my_type=gras_datadesc_struct("chained_list_t");
346 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
348 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
349 gras_datadesc_struct_append(my_type,"l", ref_my_type);
350 gras_datadesc_struct_close(my_type);
353 chained_list_t * cons(int v, chained_list_t *l) {
354 chained_list_t *nl = xbt_new(chained_list_t,1);
361 void list_free(chained_list_t*l) {
367 int list_eq(chained_list_t*i,chained_list_t*j) {
368 if (!i || !j) return i == j;
371 return list_eq(i->l, j->l);
373 void test_chain_list(gras_socket_t sock, int direction) {
374 chained_list_t *i, *j;
376 INFO0("---- Test on chained list ----");
378 /* init a value, exchange it and check its validity*/
379 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
382 write_read(gras_datadesc_by_name("chained_list_t*"),
383 &i,&j, sock,direction);
384 if (direction == READ || direction == RW || direction == COPY) {
385 xbt_assert(list_eq(i,j));
394 void test_graph(gras_socket_t sock, int direction) {
395 chained_list_t *i, *j;
397 INFO0("---- Test on graph (cyclique chained list of 3 items) ----");
398 /* init a value, exchange it and check its validity*/
399 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
403 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
404 write_read(gras_datadesc_by_name("chained_list_t*"),
405 &i,&j, sock,direction);
406 if (direction == READ || direction == RW || direction == COPY) {
409 DEBUG1("i->l=%p" ,i->l);
410 DEBUG1("i->l->l=%p" ,i->l->l);
411 DEBUG1("i->l->l->l=%p",i->l->l->l);
413 DEBUG1("j->l=%p" ,j->l);
414 DEBUG1("j->l->l=%p" ,j->l->l);
415 DEBUG1("j->l->l->l=%p",j->l->l->l);
416 xbt_assert4(j->l->l->l == j,
417 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
423 xbt_assert(list_eq(i,j));
427 i->l->l->l = NULL; /* do this even in WRITE mode */
432 /*** Dynar of references ***/
433 static void free_string(void *d){ /* used to free the data in dynar */
436 void test_dynar_ref(gras_socket_t sock, int direction){
437 gras_datadesc_type_t my_type;
443 INFO0("---- Test on dynar containing integers ----");
444 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
446 i=xbt_dynar_new(sizeof(char*),&free_string);
447 for (cpt=0; cpt< 64; cpt++) {
448 sprintf(buf,"%d",cpt);
450 xbt_dynar_push(i,&s1);
453 write_read(my_type, &i,&j, sock, direction);
454 if (direction == READ || direction == RW || direction == COPY) {
455 for (cpt=0; cpt< 64; cpt++) {
456 sprintf(buf,"%d",cpt);
457 xbt_dynar_shift(j,&s2);
458 xbt_assert2 (!strcmp(buf,s2),
459 "The retrieved value is not the same than the injected one (%s!=%s)",
470 GRAS_DEFINE_TYPE(s_pbio,
471 struct s_pbio{ /* structure presented in the IEEE article */
482 double Cdfgrd0[373][3];
483 double Cdfgrd1[3][3];
485 double Cddsdde[106][106];
488 typedef struct s_pbio pbio_t;
490 void test_pbio(gras_socket_t sock, int direction) {
493 gras_datadesc_type_t pbio_type;
496 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
497 pbio_type = gras_datadesc_by_symbol(s_pbio);
499 /* Fill in that damn struct */
501 for (cpt=0; cpt<12; cpt++)
502 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
504 for (cpt=0; cpt<110; cpt++)
505 i.Cprops[cpt] = cpt * 100.0;
506 for (cpt=0; cpt<4; cpt++)
507 i.Cndi[cpt] = cpt * 23262;
510 i.Cdtime = -11515.662;
511 i.Ctime[0] = 332523.226;
512 i.Ctime[1] = -26216.113;
513 i.Cntens = 235211411;
515 for (cpt=0; cpt<3; cpt++) {
516 for (cpt2=0; cpt2<373; cpt2++)
517 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
518 for (cpt2=0; cpt2<3; cpt2++)
519 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
521 for (cpt=0; cpt<106; cpt++) {
522 i.Cstress[cpt]=(double)cpt * 22.113;
523 for (cpt2=0; cpt2<106; cpt2++)
524 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
526 write_read(gras_datadesc_by_symbol(s_pbio),
527 &i,&j, sock,direction);
528 if (direction == READ || direction == RW || direction == COPY) {
529 /* Check that the data match */
530 xbt_assert(i.Cnstatv == j.Cnstatv);
531 for (cpt=0; cpt<12; cpt++)
532 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
533 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
534 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
535 xbt_assert(i.Cnprops == j.Cnprops);
536 for (cpt=0; cpt<110; cpt++)
537 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
538 for (cpt=0; cpt<4; cpt++)
539 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
540 xbt_assert(i.Cnshr == j.Cnshr);
541 xbt_assert(i.Cnpt == j.Cnpt);
542 xbt_assert(i.Cdtime == j.Cdtime);
543 xbt_assert(i.Ctime[0] == j.Ctime[0]);
544 xbt_assert(i.Ctime[1] == j.Ctime[1]);
545 xbt_assert(i.Cntens == j.Cntens);
546 for (cpt=0; cpt<3; cpt++) {
547 for (cpt2=0; cpt2<373; cpt2++)
548 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
549 for (cpt2=0; cpt2<3; cpt2++)
550 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
552 for (cpt=0; cpt<106; cpt++) {
553 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
554 for (cpt2=0; cpt2<106; cpt2++)
555 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
556 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
557 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
562 GRAS_DEFINE_TYPE(s_clause,
565 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
567 typedef struct s_clause Clause;
569 void test_clause(gras_socket_t sock, int direction) {
570 gras_datadesc_type_t ddt;
574 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
576 /* create and fill the struct */
580 i->literals = xbt_new(int, i->num_lits);
581 for (cpt=0; cpt<i->num_lits; cpt++)
582 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
583 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
584 DEBUG1("created count=%d",i->num_lits);
586 /* create the damn type descriptor */
587 ddt = gras_datadesc_by_symbol(s_clause);
588 /* gras_datadesc_type_dump(ddt); */
590 ddt=gras_datadesc_ref("Clause*",ddt);
592 write_read(ddt, &i,&j, sock,direction);
593 if (direction == READ || direction == RW || direction == COPY) {
594 xbt_assert(i->num_lits == j->num_lits);
595 for (cpt=0; cpt<i->num_lits; cpt++)
596 xbt_assert(i->literals[cpt] == j->literals[cpt]);
609 int main(int argc,char *argv[]) {
610 gras_socket_t sock=NULL;
613 char r_arch_char = gras_arch_selfid();
615 gras_init(&argc,argv);
617 for (cpt=1; cpt<argc; cpt++) {
618 if (!strcmp(argv[cpt], "--read")) {
620 } else if (!strcmp(argv[cpt], "--write")) {
622 } else if (!strcmp(argv[cpt], "--copy")) {
629 if (direction == WRITE) {
630 sock = gras_socket_client_from_file(filename);
631 gras_datadesc_send(sock, gras_datadesc_by_name("char"),
634 if (direction == READ) {
635 sock = gras_socket_server_from_file(filename);
636 gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
637 gras_arch_selfid(), &r_arch_char);
638 INFO3("This datafile was generated on %s (%d), I'm %s.",
639 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
640 gras_datadesc_arch_name(gras_arch_selfid()));
642 r_arch = (int)r_arch_char;
644 test_int(sock,direction);
645 test_float(sock,direction);
646 test_double(sock,direction);
647 test_array(sock,direction);
648 test_intref(sock,direction);
650 test_string(sock,direction);
651 test_dynar_scal(sock,direction);
653 test_structures(sock,direction);
655 test_homostruct(sock,direction);
656 test_hetestruct(sock,direction);
657 test_nestedstruct(sock,direction);
659 declare_chained_list_type();
660 test_chain_list(sock,direction);
661 test_graph(sock,direction);
662 test_dynar_ref(sock,direction);
664 test_pbio(sock,direction);
666 test_clause(sock,direction);
668 if (direction != RW && direction != COPY)
669 gras_socket_close(sock);