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. */
13 #include "gras/DataDesc/datadesc_interface.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
22 const char *filename = "datadesc_usage.out";
25 write_read(gras_datadesc_type_t type,void *src, void *dst,
26 gras_socket_t sock, int direction);
29 write_read(gras_datadesc_type_t type,void *src, void *dst,
30 gras_socket_t sock, int direction) {
34 sock = gras_socket_client_from_file(filename);
35 if (direction == WRITE || direction == RW)
36 gras_datadesc_send(sock, type, src);
38 gras_socket_close(sock);
42 sock = gras_socket_server_from_file(filename);
44 if (direction == READ || direction == RW)
45 gras_datadesc_recv(sock, type, r_arch, dst);
48 gras_socket_close(sock);
50 if (direction == COPY)
51 gras_datadesc_copy(type, src, dst);
54 void test_int(gras_socket_t sock, int direction);
55 void test_float(gras_socket_t sock, int direction);
56 void test_double(gras_socket_t sock, int direction);
57 void test_array(gras_socket_t sock, int direction);
58 void test_dynar_scal(gras_socket_t sock, int direction);
59 void test_intref(gras_socket_t sock, int direction);
60 void test_string(gras_socket_t sock, int direction);
62 void test_homostruct(gras_socket_t sock, int direction);
63 void test_hetestruct(gras_socket_t sock, int direction);
64 void test_nestedstruct(gras_socket_t sock, int direction);
65 void test_chain_list(gras_socket_t sock, int direction);
66 void test_graph(gras_socket_t sock, int direction);
67 void test_dynar_ref(gras_socket_t sock, int direction);
69 void test_pbio(gras_socket_t sock, int direction);
70 void test_clause(gras_socket_t sock, int direction);
72 /* defined in datadesc_structures.c, which in perl generated */
73 void test_structures(gras_socket_t sock, int direction);
77 void test_int(gras_socket_t sock, int direction) {
80 INFO0("---- Test on integer ----");
81 write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction);
82 if (direction == READ || direction == RW || direction == COPY)
85 void test_float(gras_socket_t sock, int direction) {
88 INFO0("---- Test on float ----");
89 write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction);
90 if (direction == READ || direction == RW || direction == COPY)
91 xbt_assert2(i == j,"%f != %f",i,j);
93 void test_double(gras_socket_t sock, int direction) {
94 double i=-3252355.1234,j;
96 INFO0("---- Test on double ----");
97 write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction);
98 if (direction == READ || direction == RW || direction == COPY)
99 xbt_assert2(i == j,"%f != %f",i,j);
103 typedef int array[SIZE];
104 void test_array(gras_socket_t sock, int direction) {
105 gras_datadesc_type_t my_type;
107 array i = { 35212,-6226,74337,11414,7733};
111 INFO0("---- Test on fixed array ----");
113 my_type=gras_datadesc_array_fixed("fixed int array",
114 gras_datadesc_by_name("int"),
117 write_read(my_type, &i,&j, sock,direction);
118 if (direction == READ || direction == RW || direction == COPY) {
119 for (cpt=0; cpt<SIZE; cpt++) {
120 DEBUG1("Test spot %d",cpt);
121 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
122 cpt,i[cpt],cpt,j[cpt]);
126 /*** Dynar of scalar ***/
128 void test_dynar_scal(gras_socket_t sock, int direction){
129 gras_datadesc_type_t my_type;
133 INFO0("---- Test on dynar containing integers ----");
134 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
135 i = xbt_dynar_new(sizeof(int),NULL);
136 for (cpt=0; cpt<64; cpt++) {
137 xbt_dynar_push_as(i,int,cpt);
138 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(i));
140 /* xbt_dynar_dump(i);*/
141 write_read(my_type, &i,&j, sock, direction);
142 /* xbt_dynar_dump(j);*/
143 if (direction == READ || direction == RW || direction == COPY) {
144 for (cpt=0; cpt<64; cpt++){
145 int ret=xbt_dynar_get_as(j,cpt,int);
147 CRITICAL3("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",
156 void test_intref(gras_socket_t sock, int direction) {
157 gras_datadesc_type_t my_type;
163 INFO1("---- Test on a reference to an integer (%p) ----",i);
165 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
167 write_read(my_type, &i,&j, sock,direction);
168 if (direction == READ || direction == RW || direction == COPY) {
169 xbt_assert2(*i == *j,"*i != *j (%d != %d)",*i,*j);
176 *** string (dynamic array)
178 void test_string(gras_socket_t sock, int direction) {
179 char *i=xbt_strdup("Some data"), *j=NULL;
182 INFO0("---- Test on string (ref to dynamic array) ----");
183 write_read(gras_datadesc_by_name("string"), &i,&j,
185 if (direction == READ || direction == RW || direction == COPY) {
186 for (cpt=0; cpt<strlen(i); cpt++) {
187 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
188 cpt,i[cpt],cpt,j[cpt]);
197 *** homogeneous struct
202 void test_homostruct(gras_socket_t sock, int direction) {
203 gras_datadesc_type_t my_type;
206 INFO0("---- Test on homogeneous structure ----");
207 /* create descriptor */
208 my_type=gras_datadesc_struct("homostruct");
209 gras_datadesc_struct_append(my_type,"a",
210 gras_datadesc_by_name("signed int"));
211 gras_datadesc_struct_append(my_type,"b",
212 gras_datadesc_by_name("int"));
213 gras_datadesc_struct_append(my_type,"c",
214 gras_datadesc_by_name("int"));
215 gras_datadesc_struct_append(my_type,"d",
216 gras_datadesc_by_name("int"));
217 gras_datadesc_struct_close(my_type);
218 my_type=gras_datadesc_ref("homostruct*",
219 gras_datadesc_by_name("homostruct"));
221 /* init a value, exchange it and check its validity*/
222 i=xbt_new(homostruct,1);
223 i->a = 2235; i->b = 433425;
224 i->c = -23423; i->d = -235235;
226 write_read(my_type, &i,&j, sock,direction);
227 if (direction == READ || direction == RW || direction == COPY) {
228 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
229 xbt_assert(i->b == j->b);
230 xbt_assert(i->c == j->c);
231 xbt_assert(i->d == j->d);
238 *** heterogeneous struct
242 unsigned long int l1;
244 unsigned long int l2;
246 void test_hetestruct(gras_socket_t sock, int direction) {
247 gras_datadesc_type_t my_type;
250 INFO0("---- Test on heterogeneous structure ----");
251 /* create descriptor */
252 my_type=gras_datadesc_struct("hetestruct");
253 gras_datadesc_struct_append(my_type,"c1",
254 gras_datadesc_by_name("unsigned char"));
255 gras_datadesc_struct_append(my_type,"l1",
256 gras_datadesc_by_name("unsigned long int"));
257 gras_datadesc_struct_append(my_type,"c2",
258 gras_datadesc_by_name("unsigned char"));
259 gras_datadesc_struct_append(my_type,"l2",
260 gras_datadesc_by_name("unsigned long int"));
261 gras_datadesc_struct_close(my_type);
262 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
265 /* init a value, exchange it and check its validity*/
266 i=xbt_new(hetestruct,1);
267 i->c1 = 's'; i->l1 = 123455;
268 i->c2 = 'e'; i->l2 = 774531;
270 write_read(my_type, &i,&j, sock,direction);
271 if (direction == READ || direction == RW || direction == COPY) {
272 xbt_assert(i->c1 == j->c1);
273 xbt_assert(i->c2 == j->c2);
274 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
275 xbt_assert(i->l2 == j->l2);
288 void test_nestedstruct(gras_socket_t sock, int direction) {
289 gras_datadesc_type_t my_type;
292 INFO0("---- Test on nested structures ----");
293 /* create descriptor */
294 my_type=gras_datadesc_struct("nestedstruct");
296 gras_datadesc_struct_append(my_type,"hete",
297 gras_datadesc_by_name("hetestruct"));
298 gras_datadesc_struct_append(my_type,"homo",
299 gras_datadesc_by_name("homostruct"));
300 gras_datadesc_struct_close(my_type);
301 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
303 /* init a value, exchange it and check its validity*/
304 i=xbt_new(nestedstruct,1);
305 i->homo.a = 235231; i->homo.b = -124151;
306 i->homo.c = 211551; i->homo.d = -664222;
307 i->hete.c1 = 's'; i->hete.l1 = 123455;
308 i->hete.c2 = 'e'; i->hete.l2 = 774531;
310 write_read(my_type, &i,&j, sock,direction);
311 if (direction == READ || direction == RW || direction == COPY) {
312 xbt_assert(i->homo.a == j->homo.a);
313 xbt_assert(i->homo.b == j->homo.b);
314 xbt_assert(i->homo.c == j->homo.c);
315 xbt_assert(i->homo.d == j->homo.d);
316 xbt_assert(i->hete.c1 == j->hete.c1);
317 xbt_assert(i->hete.c2 == j->hete.c2);
318 xbt_assert(i->hete.l1 == j->hete.l1);
319 xbt_assert(i->hete.l2 == j->hete.l2);
328 typedef struct s_chained_list chained_list_t;
329 struct s_chained_list {
333 void declare_chained_list_type(void);
334 chained_list_t *cons(int v, chained_list_t *l);
335 void list_free(chained_list_t *l);
336 int list_eq(chained_list_t*i,chained_list_t*j);
338 void declare_chained_list_type(void) {
339 gras_datadesc_type_t my_type,ref_my_type;
341 my_type=gras_datadesc_struct("chained_list_t");
342 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
344 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
345 gras_datadesc_struct_append(my_type,"l", ref_my_type);
346 gras_datadesc_struct_close(my_type);
349 chained_list_t * cons(int v, chained_list_t *l) {
350 chained_list_t *nl = xbt_new(chained_list_t,1);
357 void list_free(chained_list_t*l) {
363 int list_eq(chained_list_t*i,chained_list_t*j) {
364 if (!i || !j) return i == j;
367 return list_eq(i->l, j->l);
369 void test_chain_list(gras_socket_t sock, int direction) {
370 chained_list_t *i, *j;
372 INFO0("---- Test on chained list ----");
374 /* init a value, exchange it and check its validity*/
375 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
378 write_read(gras_datadesc_by_name("chained_list_t*"),
379 &i,&j, sock,direction);
380 if (direction == READ || direction == RW || direction == COPY) {
381 xbt_assert(list_eq(i,j));
390 void test_graph(gras_socket_t sock, int direction) {
391 chained_list_t *i, *j;
393 INFO0("---- Test on graph (cyclique chained list) ----");
394 /* init a value, exchange it and check its validity*/
395 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
399 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
400 write_read(gras_datadesc_by_name("chained_list_t*"),
401 &i,&j, sock,direction);
402 if (direction == READ || direction == RW || direction == COPY) {
405 DEBUG1("i->l=%p" ,i->l);
406 DEBUG1("i->l->l=%p" ,i->l->l);
407 DEBUG1("i->l->l->l=%p",i->l->l->l);
409 DEBUG1("j->l=%p" ,j->l);
410 DEBUG1("j->l->l=%p" ,j->l->l);
411 DEBUG1("j->l->l->l=%p",j->l->l->l);
412 xbt_assert4(j->l->l->l == j,
413 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
419 xbt_assert(list_eq(i,j));
423 i->l->l->l = NULL; /* do this even in WRITE mode */
428 /*** Dynar of references ***/
429 static void free_string(void *d){ /* used to free the data in dynar */
432 void test_dynar_ref(gras_socket_t sock, int direction){
433 gras_datadesc_type_t my_type;
439 INFO0("---- Test on dynar containing integers ----");
440 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
442 i=xbt_dynar_new(sizeof(char*),&free_string);
443 for (cpt=0; cpt< 64; cpt++) {
444 sprintf(buf,"%d",cpt);
446 xbt_dynar_push(i,&s1);
449 write_read(my_type, &i,&j, sock, direction);
450 if (direction == READ || direction == RW || direction == COPY) {
451 for (cpt=0; cpt< 64; cpt++) {
452 sprintf(buf,"%d",cpt);
453 xbt_dynar_shift(j,&s2);
454 xbt_assert2 (!strcmp(buf,s2),
455 "The retrieved value is not the same than the injected one (%s!=%s)",
466 GRAS_DEFINE_TYPE(s_pbio,
467 struct s_pbio{ /* structure presented in the IEEE article */
478 double Cdfgrd0[373][3];
479 double Cdfgrd1[3][3];
481 double Cddsdde[106][106];
484 typedef struct s_pbio pbio_t;
486 void test_pbio(gras_socket_t sock, int direction) {
489 gras_datadesc_type_t pbio_type;
492 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
493 pbio_type = gras_datadesc_by_symbol(s_pbio);
495 /* Fill in that damn struct */
497 for (cpt=0; cpt<12; cpt++)
498 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
500 for (cpt=0; cpt<110; cpt++)
501 i.Cprops[cpt] = cpt * 100.0;
502 for (cpt=0; cpt<4; cpt++)
503 i.Cndi[cpt] = cpt * 23262;
506 i.Cdtime = -11515.662;
507 i.Ctime[0] = 332523.226;
508 i.Ctime[1] = -26216.113;
509 i.Cntens = 235211411;
511 for (cpt=0; cpt<3; cpt++) {
512 for (cpt2=0; cpt2<373; cpt2++)
513 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
514 for (cpt2=0; cpt2<3; cpt2++)
515 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
517 for (cpt=0; cpt<106; cpt++) {
518 i.Cstress[cpt]=(double)cpt * 22.113;
519 for (cpt2=0; cpt2<106; cpt2++)
520 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
522 write_read(gras_datadesc_by_symbol(s_pbio),
523 &i,&j, sock,direction);
524 if (direction == READ || direction == RW || direction == COPY) {
525 /* Check that the data match */
526 xbt_assert(i.Cnstatv == j.Cnstatv);
527 for (cpt=0; cpt<12; cpt++)
528 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
529 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
530 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
531 xbt_assert(i.Cnprops == j.Cnprops);
532 for (cpt=0; cpt<110; cpt++)
533 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
534 for (cpt=0; cpt<4; cpt++)
535 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
536 xbt_assert(i.Cnshr == j.Cnshr);
537 xbt_assert(i.Cnpt == j.Cnpt);
538 xbt_assert(i.Cdtime == j.Cdtime);
539 xbt_assert(i.Ctime[0] == j.Ctime[0]);
540 xbt_assert(i.Ctime[1] == j.Ctime[1]);
541 xbt_assert(i.Cntens == j.Cntens);
542 for (cpt=0; cpt<3; cpt++) {
543 for (cpt2=0; cpt2<373; cpt2++)
544 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
545 for (cpt2=0; cpt2<3; cpt2++)
546 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
548 for (cpt=0; cpt<106; cpt++) {
549 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
550 for (cpt2=0; cpt2<106; cpt2++)
551 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
552 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
553 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
558 GRAS_DEFINE_TYPE(s_clause,
561 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
563 typedef struct s_clause Clause;
565 void test_clause(gras_socket_t sock, int direction) {
566 gras_datadesc_type_t ddt;
570 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
572 /* create and fill the struct */
576 i->literals = xbt_new(int, i->num_lits);
577 for (cpt=0; cpt<i->num_lits; cpt++)
578 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
579 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
580 DEBUG1("created count=%d",i->num_lits);
582 /* create the damn type descriptor */
583 ddt = gras_datadesc_by_symbol(s_clause);
584 /* gras_datadesc_type_dump(ddt); */
586 ddt=gras_datadesc_ref("Clause*",ddt);
588 write_read(ddt, &i,&j, sock,direction);
589 if (direction == READ || direction == RW || direction == COPY) {
590 xbt_assert(i->num_lits == j->num_lits);
591 for (cpt=0; cpt<i->num_lits; cpt++)
592 xbt_assert(i->literals[cpt] == j->literals[cpt]);
601 int main(int argc,char *argv[]) {
602 gras_socket_t sock=NULL;
605 char r_arch_char = gras_arch_selfid();
607 gras_init(&argc,argv);
609 for (cpt=1; cpt<argc; cpt++) {
610 if (!strcmp(argv[cpt], "--read")) {
612 } else if (!strcmp(argv[cpt], "--write")) {
614 } else if (!strcmp(argv[cpt], "--copy")) {
621 if (direction == WRITE) {
622 sock = gras_socket_client_from_file(filename);
623 gras_datadesc_send(sock, gras_datadesc_by_name("char"),
626 if (direction == READ) {
627 sock = gras_socket_server_from_file(filename);
628 gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
629 gras_arch_selfid(), &r_arch_char);
630 INFO3("This datafile was generated on %s (%d), I'm %s.",
631 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
632 gras_datadesc_arch_name(gras_arch_selfid()));
634 r_arch = (int)r_arch_char;
636 test_int(sock,direction);
637 test_float(sock,direction);
638 test_double(sock,direction);
639 test_array(sock,direction);
640 test_intref(sock,direction);
642 test_string(sock,direction);
643 test_dynar_scal(sock,direction);
645 test_structures(sock,direction);
647 test_homostruct(sock,direction);
648 test_hetestruct(sock,direction);
649 test_nestedstruct(sock,direction);
651 declare_chained_list_type();
652 test_chain_list(sock,direction);
653 test_graph(sock,direction);
654 test_dynar_ref(sock,direction);
656 test_pbio(sock,direction);
658 test_clause(sock,direction);
660 if (direction != RW && direction != COPY)
661 gras_socket_close(sock);