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) {
34 TRY(gras_socket_client_from_file(filename,&sock));
35 if (direction == WRITE || direction == RW)
36 TRY(gras_datadesc_send(sock, type, src));
38 gras_socket_close(sock);
42 TRY(gras_socket_server_from_file(filename,&sock));
44 if (direction == READ || direction == RW)
45 TRY(gras_datadesc_recv(sock, type, r_arch, dst));
48 gras_socket_close(sock);
53 xbt_error_t test_int(gras_socket_t sock, int direction);
54 xbt_error_t test_float(gras_socket_t sock, int direction);
55 xbt_error_t test_double(gras_socket_t sock, int direction);
56 xbt_error_t test_array(gras_socket_t sock, int direction);
57 xbt_error_t test_intref(gras_socket_t sock, int direction);
58 xbt_error_t test_string(gras_socket_t sock, int direction);
60 xbt_error_t test_homostruct(gras_socket_t sock, int direction);
61 xbt_error_t test_hetestruct(gras_socket_t sock, int direction);
62 xbt_error_t test_nestedstruct(gras_socket_t sock, int direction);
63 xbt_error_t test_chain_list(gras_socket_t sock, int direction);
64 xbt_error_t test_graph(gras_socket_t sock, int direction);
66 xbt_error_t test_pbio(gras_socket_t sock, int direction);
67 xbt_error_t test_clause(gras_socket_t sock, int direction);
69 /* defined in datadesc_structures.c, which in perl generated */
70 xbt_error_t test_structures(gras_socket_t sock, int direction);
74 xbt_error_t test_int(gras_socket_t sock, int direction) {
78 INFO0("---- Test on integer ----");
79 TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
80 if (direction == READ || direction == RW) {
85 xbt_error_t test_float(gras_socket_t sock, int direction) {
89 INFO0("---- Test on float ----");
90 TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
91 if (direction == READ || direction == RW) {
92 xbt_assert2(i == j,"%f != %f",i,j);
96 xbt_error_t test_double(gras_socket_t sock, int direction) {
98 double i=-3252355.1234,j;
100 INFO0("---- Test on double ----");
101 TRY(write_read(gras_datadesc_by_name("double"), &i,&j, sock,direction));
102 if (direction == READ || direction == RW) {
103 xbt_assert2(i == j,"%f != %f",i,j);
109 typedef int array[SIZE];
110 xbt_error_t test_array(gras_socket_t sock, int direction) {
112 gras_datadesc_type_t my_type;
114 array i = { 35212,-6226,74337,11414,7733};
118 INFO0("---- Test on fixed array ----");
120 my_type=gras_datadesc_array_fixed("fixed int array",
121 gras_datadesc_by_name("int"),
124 TRY(write_read(my_type, &i,&j, sock,direction));
125 if (direction == READ || direction == RW) {
126 for (cpt=0; cpt<SIZE; cpt++) {
127 DEBUG1("Test spot %d",cpt);
128 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
129 cpt,i[cpt],cpt,j[cpt]);
134 xbt_error_t test_intref(gras_socket_t sock, int direction) {
136 gras_datadesc_type_t my_type;
142 INFO1("---- Test on a reference to an integer (%p) ----",i);
144 my_type = gras_datadesc_ref("int*",gras_datadesc_by_name("int"));
146 TRY(write_read(my_type, &i,&j, sock,direction));
147 if (direction == READ || direction == RW) {
148 xbt_assert(*i == *j);
156 *** string (dynamic array)
158 xbt_error_t test_string(gras_socket_t sock, int direction) {
160 char *i=xbt_strdup("Some data"), *j=NULL;
163 INFO0("---- Test on string (ref to dynamic array) ----");
164 TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
166 if (direction == READ || direction == RW) {
167 for (cpt=0; cpt<strlen(i); cpt++) {
168 xbt_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
169 cpt,i[cpt],cpt,j[cpt]);
179 *** homogeneous struct
184 xbt_error_t test_homostruct(gras_socket_t sock, int direction) {
186 gras_datadesc_type_t my_type;
189 INFO0("---- Test on homogeneous structure ----");
190 /* create descriptor */
191 my_type=gras_datadesc_struct("homostruct");
192 gras_datadesc_struct_append(my_type,"a",
193 gras_datadesc_by_name("signed int"));
194 gras_datadesc_struct_append(my_type,"b",
195 gras_datadesc_by_name("int"));
196 gras_datadesc_struct_append(my_type,"c",
197 gras_datadesc_by_name("int"));
198 gras_datadesc_struct_append(my_type,"d",
199 gras_datadesc_by_name("int"));
200 gras_datadesc_struct_close(my_type);
201 my_type=gras_datadesc_ref("homostruct*",
202 gras_datadesc_by_name("homostruct"));
204 /* init a value, exchange it and check its validity*/
205 i=xbt_new(homostruct,1);
206 i->a = 2235; i->b = 433425;
207 i->c = -23423; i->d = -235235;
209 TRY(write_read(my_type, &i,&j, sock,direction));
210 if (direction == READ || direction == RW) {
211 xbt_assert2(i->a == j->a,"i->a=%d != j->a=%d",i->a,j->a);
212 xbt_assert(i->b == j->b);
213 xbt_assert(i->c == j->c);
214 xbt_assert(i->d == j->d);
222 *** heterogeneous struct
226 unsigned long int l1;
228 unsigned long int l2;
230 xbt_error_t test_hetestruct(gras_socket_t sock, int direction) {
232 gras_datadesc_type_t my_type;
235 INFO0("---- Test on heterogeneous structure ----");
236 /* create descriptor */
237 my_type=gras_datadesc_struct("hetestruct");
238 gras_datadesc_struct_append(my_type,"c1",
239 gras_datadesc_by_name("unsigned char"));
240 gras_datadesc_struct_append(my_type,"l1",
241 gras_datadesc_by_name("unsigned long int"));
242 gras_datadesc_struct_append(my_type,"c2",
243 gras_datadesc_by_name("unsigned char"));
244 gras_datadesc_struct_append(my_type,"l2",
245 gras_datadesc_by_name("unsigned long int"));
246 gras_datadesc_struct_close(my_type);
247 my_type=gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"));
250 /* init a value, exchange it and check its validity*/
251 i=xbt_new(hetestruct,1);
252 i->c1 = 's'; i->l1 = 123455;
253 i->c2 = 'e'; i->l2 = 774531;
255 TRY(write_read(my_type, &i,&j, sock,direction));
256 if (direction == READ || direction == RW) {
257 xbt_assert(i->c1 == j->c1);
258 xbt_assert(i->c2 == j->c2);
259 xbt_assert2(i->l1 == j->l1,"i->l1(=%ld) != j->l1(=%ld)",i->l1,j->l1);
260 xbt_assert(i->l2 == j->l2);
274 xbt_error_t test_nestedstruct(gras_socket_t sock, int direction) {
276 gras_datadesc_type_t my_type;
279 INFO0("---- Test on nested structures ----");
280 /* create descriptor */
281 my_type=gras_datadesc_struct("nestedstruct");
283 gras_datadesc_struct_append(my_type,"hete",
284 gras_datadesc_by_name("hetestruct"));
285 gras_datadesc_struct_append(my_type,"homo",
286 gras_datadesc_by_name("homostruct"));
287 gras_datadesc_struct_close(my_type);
288 my_type=gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"));
290 /* init a value, exchange it and check its validity*/
291 i=xbt_new(nestedstruct,1);
292 i->homo.a = 235231; i->homo.b = -124151;
293 i->homo.c = 211551; i->homo.d = -664222;
294 i->hete.c1 = 's'; i->hete.l1 = 123455;
295 i->hete.c2 = 'e'; i->hete.l2 = 774531;
297 TRY(write_read(my_type, &i,&j, sock,direction));
298 if (direction == READ || direction == RW) {
299 xbt_assert(i->homo.a == j->homo.a);
300 xbt_assert(i->homo.b == j->homo.b);
301 xbt_assert(i->homo.c == j->homo.c);
302 xbt_assert(i->homo.d == j->homo.d);
303 xbt_assert(i->hete.c1 == j->hete.c1);
304 xbt_assert(i->hete.c2 == j->hete.c2);
305 xbt_assert(i->hete.l1 == j->hete.l1);
306 xbt_assert(i->hete.l2 == j->hete.l2);
316 typedef struct s_chained_list chained_list_t;
317 struct s_chained_list {
321 xbt_error_t declare_chained_list_type(void);
322 chained_list_t *cons(int v, chained_list_t *l);
323 void list_free(chained_list_t *l);
324 int list_eq(chained_list_t*i,chained_list_t*j);
326 xbt_error_t declare_chained_list_type(void) {
328 gras_datadesc_type_t my_type,ref_my_type;
330 my_type=gras_datadesc_struct("chained_list_t");
331 ref_my_type=gras_datadesc_ref("chained_list_t*",my_type);
333 gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int"));
334 gras_datadesc_struct_append(my_type,"l", ref_my_type);
335 gras_datadesc_struct_close(my_type);
340 chained_list_t * cons(int v, chained_list_t *l) {
341 chained_list_t *nl = xbt_new(chained_list_t,1);
348 void list_free(chained_list_t*l) {
354 int list_eq(chained_list_t*i,chained_list_t*j) {
355 if (!i || !j) return i == j;
358 return list_eq(i->l, j->l);
360 xbt_error_t test_chain_list(gras_socket_t sock, int direction) {
362 chained_list_t *i, *j;
364 INFO0("---- Test on chained list ----");
366 /* init a value, exchange it and check its validity*/
367 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
370 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
373 if (direction == READ || direction == RW) {
374 xbt_assert(list_eq(i,j));
384 xbt_error_t test_graph(gras_socket_t sock, int direction) {
386 chained_list_t *i, *j;
388 INFO0("---- Test on graph (cyclique chained list) ----");
389 /* init a value, exchange it and check its validity*/
390 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
394 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
395 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
396 &i,&j, sock,direction));
397 if (direction == READ || direction == RW) {
400 DEBUG1("i->l=%p" ,i->l);
401 DEBUG1("i->l->l=%p" ,i->l->l);
402 DEBUG1("i->l->l->l=%p",i->l->l->l);
404 DEBUG1("j->l=%p" ,j->l);
405 DEBUG1("j->l->l=%p" ,j->l->l);
406 DEBUG1("j->l->l->l=%p",j->l->l->l);
407 xbt_assert4(j->l->l->l == j,
408 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
414 xbt_assert(list_eq(i,j));
418 i->l->l->l = NULL; /* do this even in WRITE mode */
424 GRAS_DEFINE_TYPE(s_pbio,
425 struct s_pbio{ /* structure presented in the IEEE article */
436 double Cdfgrd0[373][3];
437 double Cdfgrd1[3][3];
439 double Cddsdde[106][106];
442 typedef struct s_pbio pbio_t;
444 xbt_error_t test_pbio(gras_socket_t sock, int direction) {
449 gras_datadesc_type_t pbio_type;
451 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
452 pbio_type = gras_datadesc_by_symbol(s_pbio);
454 /* Fill in that damn struct */
456 for (cpt=0; cpt<12; cpt++)
457 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
459 for (cpt=0; cpt<110; cpt++)
460 i.Cprops[cpt] = cpt * 100.0;
461 for (cpt=0; cpt<4; cpt++)
462 i.Cndi[cpt] = cpt * 23262;
465 i.Cdtime = -11515.662;
466 i.Ctime[0] = 332523.226;
467 i.Ctime[1] = -26216.113;
468 i.Cntens = 235211411;
470 for (cpt=0; cpt<3; cpt++) {
471 for (cpt2=0; cpt2<373; cpt2++)
472 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
473 for (cpt2=0; cpt2<3; cpt2++)
474 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
476 for (cpt=0; cpt<106; cpt++) {
477 i.Cstress[cpt]=(double)cpt * 22.113;
478 for (cpt2=0; cpt2<106; cpt2++)
479 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
481 TRY(write_read(gras_datadesc_by_symbol(s_pbio),
482 &i,&j, sock,direction));
483 if (direction == READ || direction == RW) {
484 /* Check that the data match */
485 xbt_assert(i.Cnstatv == j.Cnstatv);
486 for (cpt=0; cpt<12; cpt++)
487 xbt_assert(i.Cstatev[cpt] == j.Cstatev[cpt]);
488 xbt_assert(i.Cnprops == j.Cnprops);
489 for (cpt=0; cpt<110; cpt++)
490 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
491 for (cpt=0; cpt<4; cpt++)
492 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
493 xbt_assert(i.Cnshr == j.Cnshr);
494 xbt_assert(i.Cnpt == j.Cnpt);
495 xbt_assert(i.Cdtime == j.Cdtime);
496 xbt_assert(i.Ctime[0] == j.Ctime[0]);
497 xbt_assert(i.Ctime[1] == j.Ctime[1]);
498 xbt_assert(i.Cntens == j.Cntens);
499 for (cpt=0; cpt<3; cpt++) {
500 for (cpt2=0; cpt2<373; cpt2++)
501 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
502 for (cpt2=0; cpt2<3; cpt2++)
503 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
505 for (cpt=0; cpt<106; cpt++) {
506 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
507 for (cpt2=0; cpt2<106; cpt2++)
508 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
509 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
510 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
517 GRAS_DEFINE_TYPE(s_clause,
520 int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
522 typedef struct s_clause Clause;
524 xbt_error_t test_clause(gras_socket_t sock, int direction) {
526 gras_datadesc_type_t ddt,array_t;
530 INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
532 /* create and fill the struct */
536 i->literals = xbt_new(int, i->num_lits);
537 for (cpt=0; cpt<i->num_lits; cpt++)
538 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
539 DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
540 DEBUG1("created count=%d",i->num_lits);
542 /* create the damn type descriptor */
543 ddt = gras_datadesc_by_symbol(s_clause);
544 /* gras_datadesc_type_dump(ddt); */
546 ddt=gras_datadesc_ref("Clause*",ddt);
548 TRY(write_read(ddt, &i,&j, sock,direction));
549 if (direction == READ || direction == RW) {
550 xbt_assert(i->num_lits == j->num_lits);
551 for (cpt=0; cpt<i->num_lits; cpt++)
552 xbt_assert(i->literals[cpt] == j->literals[cpt]);
562 int main(int argc,char *argv[]) {
567 char r_arch_char = gras_arch_selfid();
569 xbt_init_defaultlog(&argc,argv,NULL);
571 for (cpt=1; cpt<argc; cpt++) {
572 if (!strcmp(argv[cpt], "--read")) {
574 } else if (!strcmp(argv[cpt], "--write")) {
581 if (direction == WRITE) {
582 TRYFAIL(gras_socket_client_from_file(filename,&sock));
583 TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
586 if (direction == READ) {
587 TRYFAIL(gras_socket_server_from_file(filename,&sock));
588 TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
589 gras_arch_selfid(), &r_arch_char));
590 INFO3("This datafile was generated on %s (%d), I'm %s.",
591 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
592 gras_datadesc_arch_name(gras_arch_selfid()));
594 r_arch = (int)r_arch_char;
596 TRYFAIL(test_int(sock,direction));
597 TRYFAIL(test_float(sock,direction));
598 TRYFAIL(test_double(sock,direction));
599 TRYFAIL(test_array(sock,direction));
600 TRYFAIL(test_intref(sock,direction));
602 TRYFAIL(test_string(sock,direction));
604 TRYFAIL(test_structures(sock,direction));
606 TRYFAIL(test_homostruct(sock,direction));
607 TRYFAIL(test_hetestruct(sock,direction));
608 TRYFAIL(test_nestedstruct(sock,direction));
610 TRYFAIL(declare_chained_list_type());
611 TRYFAIL(test_chain_list(sock,direction));
612 TRYFAIL(test_graph(sock,direction));
614 TRYFAIL(test_pbio(sock,direction));
616 TRYFAIL(test_clause(sock,direction));
619 gras_socket_close(sock);