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 write_read(gras_datadesc_type_t *type,void *src, void *dst,
23 gras_socket_t *sock, int direction);
26 write_read(gras_datadesc_type_t *type,void *src, void *dst,
27 gras_socket_t *sock, int direction) {
32 TRY(gras_socket_client_from_file("datadesc_usage.out",&sock));
33 if (direction == WRITE || direction == RW)
34 TRY(gras_datadesc_send(sock, type, src));
36 gras_socket_close(&sock);
40 TRY(gras_socket_server_from_file("datadesc_usage.out",&sock));
42 if (direction == READ || direction == RW)
43 TRY(gras_datadesc_recv(sock, type, gras_arch_selfid(), dst));
46 gras_socket_close(&sock);
51 gras_error_t test_int(gras_socket_t *sock, int direction);
52 gras_error_t test_float(gras_socket_t *sock, int direction);
53 gras_error_t test_array(gras_socket_t *sock, int direction);
54 gras_error_t test_intref(gras_socket_t *sock, int direction);
55 gras_error_t test_string(gras_socket_t *sock, int direction);
57 gras_error_t test_homostruct(gras_socket_t *sock, int direction);
58 gras_error_t test_hetestruct(gras_socket_t *sock, int direction);
59 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction);
60 gras_error_t test_chain_list(gras_socket_t *sock, int direction);
61 gras_error_t test_graph(gras_socket_t *sock, int direction);
63 gras_error_t test_int(gras_socket_t *sock, int direction) {
67 INFO0("==== Test on integer ====");
68 TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
69 if (direction == READ || direction == RW) {
74 gras_error_t test_float(gras_socket_t *sock, int direction) {
78 INFO0("==== Test on float ====");
79 TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
80 if (direction == READ || direction == RW) {
87 typedef int array[SIZE];
88 gras_error_t test_array(gras_socket_t *sock, int direction) {
90 gras_datadesc_type_t *my_type;
95 INFO0("==== Test on fixed array ====");
96 for (cpt=0; cpt<SIZE; cpt++) {
100 TRY(gras_datadesc_declare_array_fixed("fixed int array",
101 gras_datadesc_by_name("int"),
104 TRY(write_read(my_type, &i,&j, sock,direction));
105 if (direction == READ || direction == RW) {
106 for (cpt=0; cpt<SIZE; cpt++)
107 gras_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
108 cpt,i[cpt],cpt,j[cpt]);
112 gras_error_t test_intref(gras_socket_t *sock, int direction) {
113 gras_error_t errcode;
114 gras_datadesc_type_t *my_type;
117 if (! (i=malloc(sizeof(int))) )
121 INFO1("==== Test on a reference to an integer (%p) ====",i);
123 TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
125 TRY(write_read(my_type, &i,&j, sock,direction));
126 if (direction == READ || direction == RW) {
127 gras_assert(*i == *j);
135 *** string (dynamic array)
137 gras_error_t test_string(gras_socket_t *sock, int direction) {
138 gras_error_t errcode;
139 gras_datadesc_type_t *my_type;
140 char *i=strdup("Some data"), *j=NULL;
143 INFO0("==== Test on string (dynamic array) ====");
144 TRY(gras_datadesc_declare_ref("string*",
145 gras_datadesc_by_name("string"),
148 TRY(write_read(gras_datadesc_by_name("string*"), &i,&j,
150 if (direction == READ || direction == RW) {
151 for (cpt=0; cpt<strlen(i); cpt++) {
152 fprintf(stderr,"%d ", cpt);
153 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
154 cpt,i[cpt],cpt,j[cpt]);
164 *** homogeneous struct
169 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
170 gras_error_t errcode;
171 gras_datadesc_type_t *my_type;
174 INFO0("==== Test on homogeneous structure ====");
175 /* create descriptor */
176 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
177 TRY(gras_datadesc_declare_struct_append(my_type,"a",
178 gras_datadesc_by_name("signed int")));
179 TRY(gras_datadesc_declare_struct_append(my_type,"b",
180 gras_datadesc_by_name("int")));
181 TRY(gras_datadesc_declare_struct_append(my_type,"c",
182 gras_datadesc_by_name("int")));
183 TRY(gras_datadesc_declare_struct_append(my_type,"d",
184 gras_datadesc_by_name("int")));
185 TRY(gras_datadesc_declare_ref("homostruct*",
186 gras_datadesc_by_name("homostruct"),
189 /* init a value, exchange it and check its validity*/
190 if (! (i=malloc(sizeof(homostruct))) )
192 i->a = rand(); i->b = rand();
193 i->c = rand(); i->d = rand();
195 TRY(write_read(my_type, &i,&j, sock,direction));
196 if (direction == READ || direction == RW) {
197 gras_assert(i->a == j->a);
198 gras_assert(i->b == j->b);
199 gras_assert(i->c == j->c);
200 gras_assert(i->d == j->d);
208 *** heterogeneous struct
212 unsigned long int l1;
214 unsigned long int l2;
216 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
217 gras_error_t errcode;
218 gras_datadesc_type_t *my_type;
221 INFO0("==== Test on heterogeneous structure ====");
222 /* create descriptor */
223 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
224 TRY(gras_datadesc_declare_struct_append(my_type,"c1",
225 gras_datadesc_by_name("unsigned char")));
226 TRY(gras_datadesc_declare_struct_append(my_type,"l1",
227 gras_datadesc_by_name("unsigned long int")));
228 TRY(gras_datadesc_declare_struct_append(my_type,"c2",
229 gras_datadesc_by_name("unsigned char")));
230 TRY(gras_datadesc_declare_struct_append(my_type,"l2",
231 gras_datadesc_by_name("unsigned long int")));
232 TRY(gras_datadesc_declare_ref("hetestruct*",
233 gras_datadesc_by_name("hetestruct"),
236 /* init a value, exchange it and check its validity*/
237 if (! (i=malloc(sizeof(hetestruct))) )
239 i->c1 = 's'; i->l1 = 123455;
240 i->c2 = 'e'; i->l2 = 774531;
242 TRY(write_read(my_type, &i,&j, sock,direction));
243 if (direction == READ || direction == RW) {
244 gras_assert(i->c1 == j->c1);
245 gras_assert(i->c2 == j->c2);
246 gras_assert(i->l1 == j->l1);
247 gras_assert(i->l2 == j->l2);
261 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
262 gras_error_t errcode;
263 gras_datadesc_type_t *my_type;
266 INFO0("==== Test on nested structures ====");
267 /* create descriptor */
268 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
270 TRY(gras_datadesc_declare_struct_append(my_type,"hete",
271 gras_datadesc_by_name("hetestruct")));
272 TRY(gras_datadesc_declare_struct_append(my_type,"homo",
273 gras_datadesc_by_name("homostruct")));
274 TRY(gras_datadesc_declare_ref("nestedstruct*",
275 gras_datadesc_by_name("nestedstruct"),
278 /* init a value, exchange it and check its validity*/
279 if (! (i=malloc(sizeof(nestedstruct))) )
281 i->homo.a = rand(); i->homo.b = rand();
282 i->homo.c = rand(); i->homo.d = rand();
283 i->hete.c1 = 's'; i->hete.l1 = 123455;
284 i->hete.c2 = 'e'; i->hete.l2 = 774531;
286 TRY(write_read(my_type, &i,&j, sock,direction));
287 if (direction == READ || direction == RW) {
288 gras_assert(i->homo.a == j->homo.a);
289 gras_assert(i->homo.b == j->homo.b);
290 gras_assert(i->homo.c == j->homo.c);
291 gras_assert(i->homo.d == j->homo.d);
292 gras_assert(i->hete.c1 == j->hete.c1);
293 gras_assert(i->hete.c2 == j->hete.c2);
294 gras_assert(i->hete.l1 == j->hete.l1);
295 gras_assert(i->hete.l2 == j->hete.l2);
305 typedef struct s_chained_list chained_list_t;
306 struct s_chained_list {
310 gras_error_t declare_chained_list_type(void);
311 chained_list_t *cons(int v, chained_list_t *l);
312 void list_free(chained_list_t *l);
313 int list_eq(chained_list_t*i,chained_list_t*j);
315 gras_error_t declare_chained_list_type(void) {
316 gras_error_t errcode;
317 gras_datadesc_type_t *my_type,*ref_my_type;
319 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
320 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
322 TRY(gras_datadesc_declare_struct_append(my_type,"v",
323 gras_datadesc_by_name("int")));
324 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
329 chained_list_t * cons(int v, chained_list_t *l) {
330 chained_list_t *nl = malloc(sizeof (chained_list_t));
337 void list_free(chained_list_t*l) {
343 int list_eq(chained_list_t*i,chained_list_t*j) {
344 if (!i || !j) return i == j;
347 return list_eq(i->l, j->l);
349 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
350 gras_error_t errcode;
351 chained_list_t *i, *j;
353 INFO0("==== Test on chained list ====");
355 /* init a value, exchange it and check its validity*/
356 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
359 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
362 if (direction == READ || direction == RW) {
363 gras_assert(list_eq(i,j));
373 gras_error_t test_graph(gras_socket_t *sock, int direction) {
374 gras_error_t errcode;
375 chained_list_t *i, *j;
377 INFO0("==== Test on graph (cyclique chained list) ====");
378 /* init a value, exchange it and check its validity*/
379 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
383 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
384 &i,&j, sock,direction));
385 if (direction == READ || direction == RW) {
388 DEBUG1("i->l=%p" ,i->l);
389 DEBUG1("i->l->l=%p" ,i->l->l);
390 DEBUG1("i->l->l->l=%p",i->l->l->l);
392 DEBUG1("j->l=%p" ,j->l);
393 DEBUG1("j->l->l=%p" ,j->l->l);
394 DEBUG1("j->l->l->l=%p",j->l->l->l);
395 gras_assert4(j->l->l->l == j,
396 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
402 gras_assert(list_eq(i,j));
411 typedef struct { /* structure presented in the IEEE article */
416 int Cndi[4], Cnshr, Cnpt;
417 double Cdtime, Ctime[2];
419 double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
422 int main(int argc,char *argv[]) {
423 gras_error_t errcode;
425 int direction = RW; // READ; // WRITE
427 gras_init_defaultlog(&argc,argv,
428 "DataDesc.thresh=verbose"
430 //" set.thresh=debug"
433 if (!strcmp(argv[1], "--read"))
435 if (!strcmp(argv[1], "--write"))
439 if (direction == WRITE)
440 TRYFAIL(gras_socket_client_from_file("datadesc_usage.out",&sock));
441 if (direction == READ)
442 TRYFAIL(gras_socket_server_from_file("datadesc_usage.out",&sock));
444 TRYFAIL(test_int(sock,direction));
445 TRYFAIL(test_float(sock,direction));
446 TRYFAIL(test_array(sock,direction));
447 TRYFAIL(test_intref(sock,direction));
449 TRYFAIL(test_string(sock,direction));
451 TRYFAIL(test_homostruct(sock,direction));
452 TRYFAIL(test_hetestruct(sock,direction));
453 TRYFAIL(test_nestedstruct(sock,direction));
455 TRYFAIL(declare_chained_list_type());
456 TRYFAIL(test_chain_list(sock,direction));
457 TRYFAIL(test_graph(sock,direction));
460 gras_socket_close(&sock);