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");
21 const char *filename = "datadesc_usage.out";
24 write_read(gras_datadesc_type_t type,void *src, void *dst,
25 gras_socket_t sock, int direction);
28 write_read(gras_datadesc_type_t type,void *src, void *dst,
29 gras_socket_t sock, int direction) {
33 sock = gras_socket_client_from_file(filename);
34 if (direction == WRITE || direction == RW)
35 gras_datadesc_send(sock, type, src);
36 /* gras_datadesc_gen_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);
51 void test_int(gras_socket_t sock, int direction);
52 void test_float(gras_socket_t sock, int direction);
53 void test_double(gras_socket_t sock, int direction);
54 void test_array(gras_socket_t sock, int direction);
55 void test_dynar_scal(gras_socket_t sock, int direction);
56 void test_intref(gras_socket_t sock, int direction);
57 void test_string(gras_socket_t sock, int direction);
59 void test_homostruct(gras_socket_t sock, int direction);
60 void test_hetestruct(gras_socket_t sock, int direction);
61 void test_nestedstruct(gras_socket_t sock, int direction);
62 void test_chain_list(gras_socket_t sock, int direction);
63 void test_graph(gras_socket_t sock, int direction);
64 void test_dynar_ref(gras_socket_t sock, int direction);
66 void test_pbio(gras_socket_t sock, int direction);
67 void test_clause(gras_socket_t sock, int direction);
69 /* defined in datadesc_structures.c, which in perl generated */
70 void test_structures(gras_socket_t sock, int direction);
74 void test_int(gras_socket_t sock, int direction) {
77 INFO0("---- Test on integer ----");
78 write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction);
79 if (direction == READ || direction == RW)
82 void test_float(gras_socket_t sock, int direction) {
85 INFO0("---- Test on float ----");
86 write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction);
87 if (direction == READ || direction == RW)
88 xbt_assert2(i == j,"%f != %f",i,j);
90 void test_double(gras_socket_t sock, int direction) {
91 double i=-3252355.1234,j;
93 INFO0("---- Test on double ----");
94 write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction);
95 if (direction == READ || direction == RW)
96 xbt_assert2(i == j,"%f != %f",i,j);
100 typedef int array[SIZE];
101 void test_array(gras_socket_t sock, int direction) {
102 gras_datadesc_type_t my_type;
104 array i = { 35212,-6226,74337,11414,7733};
108 INFO0("---- Test on fixed array ----");
110 my_type=gras_datadesc_array_fixed("fixed int array",
111 gras_datadesc_by_name("int"),
114 write_read(my_type, &i,&j, sock,direction);
115 if (direction == READ || direction == RW) {
116 for (cpt=0; cpt<SIZE; cpt++) {
117 DEBUG1("Test spot %d",cpt);
118 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
119 cpt,i[cpt],cpt,j[cpt]);
123 /*** Dynar of scalar ***/
125 void test_dynar_scal(gras_socket_t sock, int direction){
126 gras_datadesc_type_t my_type;
130 INFO0("---- Test on dynar containing integers ----");
131 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"),NULL);
132 i = xbt_dynar_new(sizeof(int),NULL);
133 for (cpt=0; cpt<64; cpt++) {
134 xbt_dynar_push_as(i,int,cpt);
135 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(i));
137 /* xbt_dynar_dump(i);*/
138 write_read(my_type, &i,&j, sock, direction);
139 /* xbt_dynar_dump(j);*/
140 if (direction == READ || direction == RW) {
141 for (cpt=0; cpt<64; cpt++){
142 int ret=xbt_dynar_get_as(j,cpt,int);
144 CRITICAL3("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",
153 void test_intref(gras_socket_t sock, int direction) {
154 gras_datadesc_type_t my_type;
160 INFO1("---- Test on a reference to an integer (%p) ----",i);
162 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
164 write_read(my_type, &i,&j, sock,direction);
165 if (direction == READ || direction == RW) {
166 xbt_assert(*i == *j);
173 *** string (dynamic array)
175 void test_string(gras_socket_t sock, int direction) {
176 char *i=xbt_strdup("Some data"), *j=NULL;
179 INFO0("---- Test on string (ref to dynamic array) ----");
180 write_read(gras_datadesc_by_name("string"), &i,&j,
182 if (direction == READ || direction == RW) {
183 for (cpt=0; cpt<strlen(i); cpt++) {
184 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
185 cpt,i[cpt],cpt,j[cpt]);
194 *** homogeneous struct
199 void test_homostruct(gras_socket_t sock, int direction) {
200 gras_datadesc_type_t my_type;
203 INFO0("---- Test on homogeneous structure ----");
204 /* create descriptor */
205 my_type=gras_datadesc_struct("homostruct");
206 gras_datadesc_struct_append(my_type,"a",
207 gras_datadesc_by_name("signed int"));
208 gras_datadesc_struct_append(my_type,"b",
209 gras_datadesc_by_name("int"));
210 gras_datadesc_struct_append(my_type,"c",
211 gras_datadesc_by_name("int"));
212 gras_datadesc_struct_append(my_type,"d",
213 gras_datadesc_by_name("int"));
214 gras_datadesc_struct_close(my_type);
215 my_type=gras_datadesc_ref("homostruct*",
216 gras_datadesc_by_name("homostruct"));
218 /* init a value, exchange it and check its validity*/
219 i=xbt_new(homostruct,1);
220 i->a = 2235; i->b = 433425;
221 i->c = -23423; i->d = -235235;
223 write_read(my_type, &i,&j, sock,direction);
224 if (direction == READ || direction == RW) {
225 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
226 xbt_assert(i->b == j->b);
227 xbt_assert(i->c == j->c);
228 xbt_assert(i->d == j->d);
235 *** heterogeneous struct
239 unsigned long int l1;
241 unsigned long int l2;
243 void test_hetestruct(gras_socket_t sock, int direction) {
244 gras_datadesc_type_t my_type;
247 INFO0("---- Test on heterogeneous structure ----");
248 /* create descriptor */
249 my_type=gras_datadesc_struct("hetestruct");
250 gras_datadesc_struct_append(my_type,"c1",
251 gras_datadesc_by_name("unsigned char"));
252 gras_datadesc_struct_append(my_type,"l1",
253 gras_datadesc_by_name("unsigned long int"));
254 gras_datadesc_struct_append(my_type,"c2",
255 gras_datadesc_by_name("unsigned char"));
256 gras_datadesc_struct_append(my_type,"l2",
257 gras_datadesc_by_name("unsigned long int"));
258 gras_datadesc_struct_close(my_type);
259 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
262 /* init a value, exchange it and check its validity*/
263 i=xbt_new(hetestruct,1);
264 i->c1 = 's'; i->l1 = 123455;
265 i->c2 = 'e'; i->l2 = 774531;
267 write_read(my_type, &i,&j, sock,direction);
268 if (direction == READ || direction == RW) {
269 xbt_assert(i->c1 == j->c1);
270 xbt_assert(i->c2 == j->c2);
271 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
272 xbt_assert(i->l2 == j->l2);
285 void test_nestedstruct(gras_socket_t sock, int direction) {
286 gras_datadesc_type_t my_type;
289 INFO0("---- Test on nested structures ----");
290 /* create descriptor */
291 my_type=gras_datadesc_struct("nestedstruct");
293 gras_datadesc_struct_append(my_type,"hete",
294 gras_datadesc_by_name("hetestruct"));
295 gras_datadesc_struct_append(my_type,"homo",
296 gras_datadesc_by_name("homostruct"));
297 gras_datadesc_struct_close(my_type);
298 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
300 /* init a value, exchange it and check its validity*/
301 i=xbt_new(nestedstruct,1);
302 i->homo.a = 235231; i->homo.b = -124151;
303 i->homo.c = 211551; i->homo.d = -664222;
304 i->hete.c1 = 's'; i->hete.l1 = 123455;
305 i->hete.c2 = 'e'; i->hete.l2 = 774531;
307 write_read(my_type, &i,&j, sock,direction);
308 if (direction == READ || direction == RW) {
309 xbt_assert(i->homo.a == j->homo.a);
310 xbt_assert(i->homo.b == j->homo.b);
311 xbt_assert(i->homo.c == j->homo.c);
312 xbt_assert(i->homo.d == j->homo.d);
313 xbt_assert(i->hete.c1 == j->hete.c1);
314 xbt_assert(i->hete.c2 == j->hete.c2);
315 xbt_assert(i->hete.l1 == j->hete.l1);
316 xbt_assert(i->hete.l2 == j->hete.l2);
325 typedef struct s_chained_list chained_list_t;
326 struct s_chained_list {
330 void declare_chained_list_type(void);
331 chained_list_t *cons(int v, chained_list_t *l);
332 void list_free(chained_list_t *l);
333 int list_eq(chained_list_t*i,chained_list_t*j);
335 void declare_chained_list_type(void) {
336 gras_datadesc_type_t my_type,ref_my_type;
338 my_type=gras_datadesc_struct("chained_list_t");
339 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
341 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
342 gras_datadesc_struct_append(my_type,"l", ref_my_type);
343 gras_datadesc_struct_close(my_type);
346 chained_list_t * cons(int v, chained_list_t *l) {
347 chained_list_t *nl = xbt_new(chained_list_t,1);
354 void list_free(chained_list_t*l) {
360 int list_eq(chained_list_t*i,chained_list_t*j) {
361 if (!i || !j) return i == j;
364 return list_eq(i->l, j->l);
366 void test_chain_list(gras_socket_t sock, int direction) {
367 chained_list_t *i, *j;
369 INFO0("---- Test on chained list ----");
371 /* init a value, exchange it and check its validity*/
372 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
375 write_read(gras_datadesc_by_name("chained_list_t*"),
376 &i,&j, sock,direction);
377 if (direction == READ || direction == RW) {
378 xbt_assert(list_eq(i,j));
387 void test_graph(gras_socket_t sock, int direction) {
388 chained_list_t *i, *j;
390 INFO0("---- Test on graph (cyclique chained list) ----");
391 /* init a value, exchange it and check its validity*/
392 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
396 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
397 write_read(gras_datadesc_by_name("chained_list_t*"),
398 &i,&j, sock,direction);
399 if (direction == READ || direction == RW) {
402 DEBUG1("i->l=%p" ,i->l);
403 DEBUG1("i->l->l=%p" ,i->l->l);
404 DEBUG1("i->l->l->l=%p",i->l->l->l);
406 DEBUG1("j->l=%p" ,j->l);
407 DEBUG1("j->l->l=%p" ,j->l->l);
408 DEBUG1("j->l->l->l=%p",j->l->l->l);
409 xbt_assert4(j->l->l->l == j,
410 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
416 xbt_assert(list_eq(i,j));
420 i->l->l->l = NULL; /* do this even in WRITE mode */
425 /*** Dynar of references ***/
426 static void free_string(void *d){ /* used to free the data in dynar */
429 void test_dynar_ref(gras_socket_t sock, int direction){
430 gras_datadesc_type_t my_type;
436 INFO0("---- Test on dynar containing integers ----");
437 my_type = gras_datadesc_dynar(gras_datadesc_by_name("string"),&free_string);
439 i=xbt_dynar_new(sizeof(char*),&free_string);
440 for (cpt=0; cpt< 64; cpt++) {
441 sprintf(buf,"%d",cpt);
443 xbt_dynar_push(i,&s1);
446 write_read(my_type, &i,&j, sock, direction);
447 if (direction == READ || direction == RW) {
448 for (cpt=0; cpt< 64; cpt++) {
449 sprintf(buf,"%d",cpt);
450 xbt_dynar_shift(j,&s2);
451 xbt_assert2 (!strcmp(buf,s2),
452 "The retrieved value is not the same than the injected one (%s!=%s)",
463 GRAS_DEFINE_TYPE(s_pbio,
464 struct s_pbio{ /* structure presented in the IEEE article */
475 double Cdfgrd0[373][3];
476 double Cdfgrd1[3][3];
478 double Cddsdde[106][106];
481 typedef struct s_pbio pbio_t;
483 void test_pbio(gras_socket_t sock, int direction) {
486 gras_datadesc_type_t pbio_type;
489 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
490 pbio_type = gras_datadesc_by_symbol(s_pbio);
492 /* Fill in that damn struct */
494 for (cpt=0; cpt<12; cpt++)
495 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
497 for (cpt=0; cpt<110; cpt++)
498 i.Cprops[cpt] = cpt * 100.0;
499 for (cpt=0; cpt<4; cpt++)
500 i.Cndi[cpt] = cpt * 23262;
503 i.Cdtime = -11515.662;
504 i.Ctime[0] = 332523.226;
505 i.Ctime[1] = -26216.113;
506 i.Cntens = 235211411;
508 for (cpt=0; cpt<3; cpt++) {
509 for (cpt2=0; cpt2<373; cpt2++)
510 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
511 for (cpt2=0; cpt2<3; cpt2++)
512 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
514 for (cpt=0; cpt<106; cpt++) {
515 i.Cstress[cpt]=(double)cpt * 22.113;
516 for (cpt2=0; cpt2<106; cpt2++)
517 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
519 write_read(gras_datadesc_by_symbol(s_pbio),
520 &i,&j, sock,direction);
521 if (direction == READ || direction == RW) {
522 /* Check that the data match */
523 xbt_assert(i.Cnstatv == j.Cnstatv);
524 for (cpt=0; cpt<12; cpt++)
525 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
526 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
527 cpt,i.Cstatev[cpt],cpt,j.Cstatev[cpt]);
528 xbt_assert(i.Cnprops == j.Cnprops);
529 for (cpt=0; cpt<110; cpt++)
530 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
531 for (cpt=0; cpt<4; cpt++)
532 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
533 xbt_assert(i.Cnshr == j.Cnshr);
534 xbt_assert(i.Cnpt == j.Cnpt);
535 xbt_assert(i.Cdtime == j.Cdtime);
536 xbt_assert(i.Ctime[0] == j.Ctime[0]);
537 xbt_assert(i.Ctime[1] == j.Ctime[1]);
538 xbt_assert(i.Cntens == j.Cntens);
539 for (cpt=0; cpt<3; cpt++) {
540 for (cpt2=0; cpt2<373; cpt2++)
541 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
542 for (cpt2=0; cpt2<3; cpt2++)
543 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
545 for (cpt=0; cpt<106; cpt++) {
546 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
547 for (cpt2=0; cpt2<106; cpt2++)
548 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
549 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
550 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
555 GRAS_DEFINE_TYPE(s_clause,
558 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
560 typedef struct s_clause Clause;
562 void test_clause(gras_socket_t sock, int direction) {
563 gras_datadesc_type_t ddt;
567 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
569 /* create and fill the struct */
573 i->literals = xbt_new(int, i->num_lits);
574 for (cpt=0; cpt<i->num_lits; cpt++)
575 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
576 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
577 DEBUG1("created count=%d",i->num_lits);
579 /* create the damn type descriptor */
580 ddt = gras_datadesc_by_symbol(s_clause);
581 /* gras_datadesc_type_dump(ddt); */
583 ddt=gras_datadesc_ref("Clause*",ddt);
585 write_read(ddt, &i,&j, sock,direction);
586 if (direction == READ || direction == RW) {
587 xbt_assert(i->num_lits == j->num_lits);
588 for (cpt=0; cpt<i->num_lits; cpt++)
589 xbt_assert(i->literals[cpt] == j->literals[cpt]);
598 int main(int argc,char *argv[]) {
602 char r_arch_char = gras_arch_selfid();
604 gras_init(&argc,argv);
606 for (cpt=1; cpt<argc; cpt++) {
607 if (!strcmp(argv[cpt], "--read")) {
609 } else if (!strcmp(argv[cpt], "--write")) {
616 if (direction == WRITE) {
617 sock = gras_socket_client_from_file(filename);
618 gras_datadesc_send(sock, gras_datadesc_by_name("char"),
621 if (direction == READ) {
622 sock = gras_socket_server_from_file(filename);
623 gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
624 gras_arch_selfid(), &r_arch_char);
625 INFO3("This datafile was generated on %s (%d), I'm %s.",
626 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
627 gras_datadesc_arch_name(gras_arch_selfid()));
629 r_arch = (int)r_arch_char;
631 test_int(sock,direction);
632 test_float(sock,direction);
633 test_double(sock,direction);
634 test_array(sock,direction);
635 test_dynar_scal(sock,direction);
636 test_intref(sock,direction);
638 test_string(sock,direction);
640 test_structures(sock,direction);
642 test_homostruct(sock,direction);
643 test_hetestruct(sock,direction);
644 test_nestedstruct(sock,direction);
646 declare_chained_list_type();
647 test_chain_list(sock,direction);
648 test_graph(sock,direction);
649 test_dynar_ref(sock,direction);
651 test_pbio(sock,direction);
653 test_clause(sock,direction);
656 gras_socket_close(sock);