3 /* datadesc: test of data description (using file transport). */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 the OURAGAN project. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "../DataDesc/datadesc_interface.h"
15 GRAS_LOG_NEW_DEFAULT_CATEGORY(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) {
35 TRY(gras_socket_client_from_file(filename,&sock));
36 if (direction == WRITE || direction == RW)
37 TRY(gras_datadesc_send(sock, type, src));
39 gras_socket_close(sock);
43 TRY(gras_socket_server_from_file(filename,&sock));
45 if (direction == READ || direction == RW)
46 TRY(gras_datadesc_recv(sock, type, r_arch, dst));
49 gras_socket_close(sock);
54 gras_error_t test_int(gras_socket_t *sock, int direction);
55 gras_error_t test_float(gras_socket_t *sock, int direction);
56 gras_error_t test_double(gras_socket_t *sock, int direction);
57 gras_error_t test_array(gras_socket_t *sock, int direction);
58 gras_error_t test_intref(gras_socket_t *sock, int direction);
59 gras_error_t test_string(gras_socket_t *sock, int direction);
61 gras_error_t test_homostruct(gras_socket_t *sock, int direction);
62 gras_error_t test_hetestruct(gras_socket_t *sock, int direction);
63 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction);
64 gras_error_t test_chain_list(gras_socket_t *sock, int direction);
65 gras_error_t test_graph(gras_socket_t *sock, int direction);
67 gras_error_t test_pbio(gras_socket_t *sock, int direction);
69 /* defined in datadesc_structures.c, which in perl generated */
70 gras_error_t test_structures(gras_socket_t *sock, int direction);
74 gras_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 gras_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 gras_assert2(i == j,"%f != %f",i,j);
96 gras_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 gras_assert2(i == j,"%f != %f",i,j);
109 typedef int array[SIZE];
110 gras_error_t test_array(gras_socket_t *sock, int direction) {
111 gras_error_t errcode;
112 gras_datadesc_type_t *my_type;
114 array i = { 35212,-6226,74337,11414,7733};
118 INFO0("---- Test on fixed array ----");
120 TRY(gras_datadesc_declare_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 gras_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
129 cpt,i[cpt],cpt,j[cpt]);
134 gras_error_t test_intref(gras_socket_t *sock, int direction) {
135 gras_error_t errcode;
136 gras_datadesc_type_t *my_type;
139 if (! (i=malloc(sizeof(int))) )
143 INFO1("---- Test on a reference to an integer (%p) ----",i);
145 TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
147 TRY(write_read(my_type, &i,&j, sock,direction));
148 if (direction == READ || direction == RW) {
149 gras_assert(*i == *j);
157 *** string (dynamic array)
159 gras_error_t test_string(gras_socket_t *sock, int direction) {
160 gras_error_t errcode;
161 char *i=strdup("Some data"), *j=NULL;
164 INFO0("---- Test on string (ref to dynamic array) ----");
165 TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
167 if (direction == READ || direction == RW) {
168 for (cpt=0; cpt<strlen(i); cpt++) {
169 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
170 cpt,i[cpt],cpt,j[cpt]);
180 *** homogeneous struct
185 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
186 gras_error_t errcode;
187 gras_datadesc_type_t *my_type;
190 INFO0("---- Test on homogeneous structure ----");
191 /* create descriptor */
192 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
193 TRY(gras_datadesc_declare_struct_append(my_type,"a",
194 gras_datadesc_by_name("signed int")));
195 TRY(gras_datadesc_declare_struct_append(my_type,"b",
196 gras_datadesc_by_name("int")));
197 TRY(gras_datadesc_declare_struct_append(my_type,"c",
198 gras_datadesc_by_name("int")));
199 TRY(gras_datadesc_declare_struct_append(my_type,"d",
200 gras_datadesc_by_name("int")));
201 gras_datadesc_declare_struct_close(my_type);
202 TRY(gras_datadesc_declare_ref("homostruct*",
203 gras_datadesc_by_name("homostruct"),
206 /* init a value, exchange it and check its validity*/
207 if (! (i=malloc(sizeof(homostruct))) )
209 i->a = 2235; i->b = 433425;
210 i->c = -23423; i->d = -235235;
212 TRY(write_read(my_type, &i,&j, sock,direction));
213 if (direction == READ || direction == RW) {
214 gras_assert(i->a == j->a);
215 gras_assert(i->b == j->b);
216 gras_assert(i->c == j->c);
217 gras_assert(i->d == j->d);
225 *** heterogeneous struct
229 unsigned long int l1;
231 unsigned long int l2;
233 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
234 gras_error_t errcode;
235 gras_datadesc_type_t *my_type;
238 INFO0("---- Test on heterogeneous structure ----");
239 /* create descriptor */
240 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
241 TRY(gras_datadesc_declare_struct_append(my_type,"c1",
242 gras_datadesc_by_name("unsigned char")));
243 TRY(gras_datadesc_declare_struct_append(my_type,"l1",
244 gras_datadesc_by_name("unsigned long int")));
245 TRY(gras_datadesc_declare_struct_append(my_type,"c2",
246 gras_datadesc_by_name("unsigned char")));
247 TRY(gras_datadesc_declare_struct_append(my_type,"l2",
248 gras_datadesc_by_name("unsigned long int")));
249 gras_datadesc_declare_struct_close(my_type);
250 TRY(gras_datadesc_declare_ref("hetestruct*",
251 gras_datadesc_by_name("hetestruct"),
254 /* init a value, exchange it and check its validity*/
255 if (! (i=malloc(sizeof(hetestruct))) )
257 i->c1 = 's'; i->l1 = 123455;
258 i->c2 = 'e'; i->l2 = 774531;
260 TRY(write_read(my_type, &i,&j, sock,direction));
261 if (direction == READ || direction == RW) {
262 gras_assert(i->c1 == j->c1);
263 gras_assert(i->c2 == j->c2);
264 gras_assert2(i->l1 == j->l1,"i->l1(=%d) != j->l1(=%d)",i->l1,j->l1);
265 gras_assert(i->l2 == j->l2);
279 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
280 gras_error_t errcode;
281 gras_datadesc_type_t *my_type;
284 INFO0("---- Test on nested structures ----");
285 /* create descriptor */
286 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
288 TRY(gras_datadesc_declare_struct_append(my_type,"hete",
289 gras_datadesc_by_name("hetestruct")));
290 TRY(gras_datadesc_declare_struct_append(my_type,"homo",
291 gras_datadesc_by_name("homostruct")));
292 gras_datadesc_declare_struct_close(my_type);
293 TRY(gras_datadesc_declare_ref("nestedstruct*",
294 gras_datadesc_by_name("nestedstruct"),
297 /* init a value, exchange it and check its validity*/
298 if (! (i=malloc(sizeof(nestedstruct))) )
300 i->homo.a = 235231; i->homo.b = -124151;
301 i->homo.c = 211551; i->homo.d = -664222;
302 i->hete.c1 = 's'; i->hete.l1 = 123455;
303 i->hete.c2 = 'e'; i->hete.l2 = 774531;
305 TRY(write_read(my_type, &i,&j, sock,direction));
306 if (direction == READ || direction == RW) {
307 gras_assert(i->homo.a == j->homo.a);
308 gras_assert(i->homo.b == j->homo.b);
309 gras_assert(i->homo.c == j->homo.c);
310 gras_assert(i->homo.d == j->homo.d);
311 gras_assert(i->hete.c1 == j->hete.c1);
312 gras_assert(i->hete.c2 == j->hete.c2);
313 gras_assert(i->hete.l1 == j->hete.l1);
314 gras_assert(i->hete.l2 == j->hete.l2);
324 typedef struct s_chained_list chained_list_t;
325 struct s_chained_list {
329 gras_error_t declare_chained_list_type(void);
330 chained_list_t *cons(int v, chained_list_t *l);
331 void list_free(chained_list_t *l);
332 int list_eq(chained_list_t*i,chained_list_t*j);
334 gras_error_t declare_chained_list_type(void) {
335 gras_error_t errcode;
336 gras_datadesc_type_t *my_type,*ref_my_type;
338 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
339 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
341 TRY(gras_datadesc_declare_struct_append(my_type,"v",
342 gras_datadesc_by_name("int")));
343 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
344 gras_datadesc_declare_struct_close(my_type);
349 chained_list_t * cons(int v, chained_list_t *l) {
350 chained_list_t *nl = malloc(sizeof (chained_list_t));
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 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
370 gras_error_t errcode;
371 chained_list_t *i, *j;
373 INFO0("---- Test on chained list ----");
375 /* init a value, exchange it and check its validity*/
376 i = cons( 12355, cons( 246264 , cons( 23263, NULL)));
379 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
382 if (direction == READ || direction == RW) {
383 gras_assert(list_eq(i,j));
393 gras_error_t test_graph(gras_socket_t *sock, int direction) {
394 gras_error_t errcode;
395 chained_list_t *i, *j;
397 INFO0("---- Test on graph (cyclique chained list) ----");
398 /* init a value, exchange it and check its validity*/
399 i = cons( 1151515, cons( -232362 , cons( 222552, NULL)));
403 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
404 &i,&j, sock,direction));
405 if (direction == READ || direction == RW) {
408 DEBUG1("i->l=%p" ,i->l);
409 DEBUG1("i->l->l=%p" ,i->l->l);
410 DEBUG1("i->l->l->l=%p",i->l->l->l);
412 DEBUG1("j->l=%p" ,j->l);
413 DEBUG1("j->l->l=%p" ,j->l->l);
414 DEBUG1("j->l->l->l=%p",j->l->l->l);
415 gras_assert4(j->l->l->l == j,
416 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
422 gras_assert(list_eq(i,j));
426 i->l->l->l = NULL; /* do this even in WRITE mode */
432 GRAS_DEFINE_TYPE(s_pbio,
433 struct s_pbio{ /* structure presented in the IEEE article */
444 double Cdfgrd0[373][3];
445 double Cdfgrd1[3][3];
447 double Cddsdde[106][106];
450 typedef struct s_pbio pbio_t;
452 gras_error_t test_pbio(gras_socket_t *sock, int direction) {
453 gras_error_t errcode;
457 gras_datadesc_type_t *pbio_type;
459 INFO0("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
460 pbio_type = gras_datadesc_by_symbol(s_pbio);
462 /* Fill in that damn struct */
464 for (cpt=0; cpt<12; cpt++)
465 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
467 for (cpt=0; cpt<110; cpt++)
468 i.Cprops[cpt] = cpt * 100.0;
469 for (cpt=0; cpt<4; cpt++)
470 i.Cndi[cpt] = cpt * 23262;
473 i.Cdtime = -11515.662;
474 i.Ctime[0] = 332523.226;
475 i.Ctime[1] = -26216.113;
476 i.Cntens = 235211411;
478 for (cpt=0; cpt<3; cpt++) {
479 for (cpt2=0; cpt2<373; cpt2++)
480 i.Cdfgrd0[cpt2][cpt] = ((double)cpt) * ((double)cpt2);
481 for (cpt2=0; cpt2<3; cpt2++)
482 i.Cdfgrd1[cpt][cpt2] = -((double)cpt) * ((double)cpt2);
484 for (cpt=0; cpt<106; cpt++) {
485 i.Cstress[cpt]=(double)cpt * 22.113;
486 for (cpt2=0; cpt2<106; cpt2++)
487 i.Cddsdde[cpt][cpt2] = ((double)cpt) * ((double)cpt2);
489 TRY(write_read(gras_datadesc_by_symbol(s_pbio),
490 &i,&j, sock,direction));
491 if (direction == READ || direction == RW) {
492 /* Check that the data match */
493 gras_assert(i.Cnstatv == j.Cnstatv);
494 for (cpt=0; cpt<12; cpt++)
495 gras_assert(i.Cstatev[cpt] == j.Cstatev[cpt]);
496 gras_assert(i.Cnprops == j.Cnprops);
497 for (cpt=0; cpt<110; cpt++)
498 gras_assert(i.Cprops[cpt] == j.Cprops[cpt]);
499 for (cpt=0; cpt<4; cpt++)
500 gras_assert(i.Cndi[cpt] == j.Cndi[cpt]);
501 gras_assert(i.Cnshr == j.Cnshr);
502 gras_assert(i.Cnpt == j.Cnpt);
503 gras_assert(i.Cdtime == j.Cdtime);
504 gras_assert(i.Ctime[0] == j.Ctime[0]);
505 gras_assert(i.Ctime[1] == j.Ctime[1]);
506 gras_assert(i.Cntens == j.Cntens);
507 for (cpt=0; cpt<3; cpt++) {
508 for (cpt2=0; cpt2<373; cpt2++)
509 gras_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
510 for (cpt2=0; cpt2<3; cpt2++)
511 gras_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
513 for (cpt=0; cpt<106; cpt++) {
514 gras_assert(i.Cstress[cpt] == j.Cstress[cpt]);
515 for (cpt2=0; cpt2<106; cpt2++)
516 gras_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
517 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
518 i.Cddsdde[cpt][cpt2],cpt,cpt2,j.Cddsdde[cpt][cpt2]);
525 int main(int argc,char *argv[]) {
526 gras_error_t errcode;
530 char r_arch_char = gras_arch_selfid();
532 gras_init_defaultlog(&argc,argv,NULL);
534 for (cpt=1; cpt<argc; cpt++) {
535 if (!strcmp(argv[cpt], "--read")) {
537 } else if (!strcmp(argv[cpt], "--write")) {
544 if (direction == WRITE) {
545 TRYFAIL(gras_socket_client_from_file(filename,&sock));
546 TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
549 if (direction == READ) {
550 TRYFAIL(gras_socket_server_from_file(filename,&sock));
551 TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
552 gras_arch_selfid(), &r_arch_char));
553 INFO3("This datafile was generated on %s (%d), I'm %s.",
554 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char,
555 gras_datadesc_arch_name(gras_arch_selfid()));
557 r_arch = (int)r_arch_char;
560 TRYFAIL(test_int(sock,direction));
561 TRYFAIL(test_float(sock,direction));
562 TRYFAIL(test_double(sock,direction));
563 TRYFAIL(test_array(sock,direction));
564 TRYFAIL(test_intref(sock,direction));
566 TRYFAIL(test_string(sock,direction));
568 TRYFAIL(test_structures(sock,direction));
570 TRYFAIL(test_homostruct(sock,direction));
571 TRYFAIL(test_hetestruct(sock,direction));
572 TRYFAIL(test_nestedstruct(sock,direction));
574 TRYFAIL(declare_chained_list_type());
575 TRYFAIL(test_chain_list(sock,direction));
576 TRYFAIL(test_graph(sock,direction));
578 TRYFAIL(test_pbio(sock,direction));
581 gras_socket_close(sock);