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_array(gras_socket_t *sock, int direction);
57 gras_error_t test_intref(gras_socket_t *sock, int direction);
58 gras_error_t test_string(gras_socket_t *sock, int direction);
60 gras_error_t test_homostruct(gras_socket_t *sock, int direction);
61 gras_error_t test_hetestruct(gras_socket_t *sock, int direction);
62 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction);
63 gras_error_t test_chain_list(gras_socket_t *sock, int direction);
64 gras_error_t test_graph(gras_socket_t *sock, int direction);
66 gras_error_t test_int(gras_socket_t *sock, int direction) {
70 INFO0("==== Test on integer ====");
71 TRY(write_read(gras_datadesc_by_name("int"), &i,&j, sock,direction));
72 if (direction == READ || direction == RW) {
77 gras_error_t test_float(gras_socket_t *sock, int direction) {
81 INFO0("==== Test on float ====");
82 TRY(write_read(gras_datadesc_by_name("float"), &i,&j, sock,direction));
83 if (direction == READ || direction == RW) {
90 typedef int array[SIZE];
91 gras_error_t test_array(gras_socket_t *sock, int direction) {
93 gras_datadesc_type_t *my_type;
98 INFO0("==== Test on fixed array ====");
99 for (cpt=0; cpt<SIZE; cpt++) {
103 TRY(gras_datadesc_declare_array_fixed("fixed int array",
104 gras_datadesc_by_name("int"),
107 TRY(write_read(my_type, &i,&j, sock,direction));
108 if (direction == READ || direction == RW) {
109 for (cpt=0; cpt<SIZE; cpt++) {
110 DEBUG1("Test spot %d",cpt);
111 gras_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
112 cpt,i[cpt],cpt,j[cpt]);
117 gras_error_t test_intref(gras_socket_t *sock, int direction) {
118 gras_error_t errcode;
119 gras_datadesc_type_t *my_type;
122 if (! (i=malloc(sizeof(int))) )
126 INFO1("==== Test on a reference to an integer (%p) ====",i);
128 TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
130 TRY(write_read(my_type, &i,&j, sock,direction));
131 if (direction == READ || direction == RW) {
132 gras_assert(*i == *j);
140 *** string (dynamic array)
142 gras_error_t test_string(gras_socket_t *sock, int direction) {
143 gras_error_t errcode;
144 char *i=strdup("Some data"), *j=NULL;
147 INFO0("==== Test on string (ref to dynamic array) ====");
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 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
153 cpt,i[cpt],cpt,j[cpt]);
163 *** homogeneous struct
168 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
169 gras_error_t errcode;
170 gras_datadesc_type_t *my_type;
173 INFO0("==== Test on homogeneous structure ====");
174 /* create descriptor */
175 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
176 TRY(gras_datadesc_declare_struct_append(my_type,"a",
177 gras_datadesc_by_name("signed int")));
178 TRY(gras_datadesc_declare_struct_append(my_type,"b",
179 gras_datadesc_by_name("int")));
180 TRY(gras_datadesc_declare_struct_append(my_type,"c",
181 gras_datadesc_by_name("int")));
182 TRY(gras_datadesc_declare_struct_append(my_type,"d",
183 gras_datadesc_by_name("int")));
184 TRY(gras_datadesc_declare_ref("homostruct*",
185 gras_datadesc_by_name("homostruct"),
188 /* init a value, exchange it and check its validity*/
189 if (! (i=malloc(sizeof(homostruct))) )
191 i->a = rand(); i->b = rand();
192 i->c = rand(); i->d = rand();
194 TRY(write_read(my_type, &i,&j, sock,direction));
195 if (direction == READ || direction == RW) {
196 gras_assert(i->a == j->a);
197 gras_assert(i->b == j->b);
198 gras_assert(i->c == j->c);
199 gras_assert(i->d == j->d);
207 *** heterogeneous struct
211 unsigned long int l1;
213 unsigned long int l2;
215 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
216 gras_error_t errcode;
217 gras_datadesc_type_t *my_type;
220 INFO0("==== Test on heterogeneous structure ====");
221 /* create descriptor */
222 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
223 TRY(gras_datadesc_declare_struct_append(my_type,"c1",
224 gras_datadesc_by_name("unsigned char")));
225 TRY(gras_datadesc_declare_struct_append(my_type,"l1",
226 gras_datadesc_by_name("unsigned long int")));
227 TRY(gras_datadesc_declare_struct_append(my_type,"c2",
228 gras_datadesc_by_name("unsigned char")));
229 TRY(gras_datadesc_declare_struct_append(my_type,"l2",
230 gras_datadesc_by_name("unsigned long int")));
231 TRY(gras_datadesc_declare_ref("hetestruct*",
232 gras_datadesc_by_name("hetestruct"),
235 /* init a value, exchange it and check its validity*/
236 if (! (i=malloc(sizeof(hetestruct))) )
238 i->c1 = 's'; i->l1 = 123455;
239 i->c2 = 'e'; i->l2 = 774531;
241 TRY(write_read(my_type, &i,&j, sock,direction));
242 if (direction == READ || direction == RW) {
243 gras_assert(i->c1 == j->c1);
244 gras_assert(i->c2 == j->c2);
245 gras_assert(i->l1 == j->l1);
246 gras_assert(i->l2 == j->l2);
260 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
261 gras_error_t errcode;
262 gras_datadesc_type_t *my_type;
265 INFO0("==== Test on nested structures ====");
266 /* create descriptor */
267 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
269 TRY(gras_datadesc_declare_struct_append(my_type,"hete",
270 gras_datadesc_by_name("hetestruct")));
271 TRY(gras_datadesc_declare_struct_append(my_type,"homo",
272 gras_datadesc_by_name("homostruct")));
273 TRY(gras_datadesc_declare_ref("nestedstruct*",
274 gras_datadesc_by_name("nestedstruct"),
277 /* init a value, exchange it and check its validity*/
278 if (! (i=malloc(sizeof(nestedstruct))) )
280 i->homo.a = rand(); i->homo.b = rand();
281 i->homo.c = rand(); i->homo.d = rand();
282 i->hete.c1 = 's'; i->hete.l1 = 123455;
283 i->hete.c2 = 'e'; i->hete.l2 = 774531;
285 TRY(write_read(my_type, &i,&j, sock,direction));
286 if (direction == READ || direction == RW) {
287 gras_assert(i->homo.a == j->homo.a);
288 gras_assert(i->homo.b == j->homo.b);
289 gras_assert(i->homo.c == j->homo.c);
290 gras_assert(i->homo.d == j->homo.d);
291 gras_assert(i->hete.c1 == j->hete.c1);
292 gras_assert(i->hete.c2 == j->hete.c2);
293 gras_assert(i->hete.l1 == j->hete.l1);
294 gras_assert(i->hete.l2 == j->hete.l2);
304 typedef struct s_chained_list chained_list_t;
305 struct s_chained_list {
309 gras_error_t declare_chained_list_type(void);
310 chained_list_t *cons(int v, chained_list_t *l);
311 void list_free(chained_list_t *l);
312 int list_eq(chained_list_t*i,chained_list_t*j);
314 gras_error_t declare_chained_list_type(void) {
315 gras_error_t errcode;
316 gras_datadesc_type_t *my_type,*ref_my_type;
318 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
319 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
321 TRY(gras_datadesc_declare_struct_append(my_type,"v",
322 gras_datadesc_by_name("int")));
323 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
328 chained_list_t * cons(int v, chained_list_t *l) {
329 chained_list_t *nl = malloc(sizeof (chained_list_t));
336 void list_free(chained_list_t*l) {
342 int list_eq(chained_list_t*i,chained_list_t*j) {
343 if (!i || !j) return i == j;
346 return list_eq(i->l, j->l);
348 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
349 gras_error_t errcode;
350 chained_list_t *i, *j;
352 INFO0("==== Test on chained list ====");
354 /* init a value, exchange it and check its validity*/
355 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
358 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
361 if (direction == READ || direction == RW) {
362 gras_assert(list_eq(i,j));
372 gras_error_t test_graph(gras_socket_t *sock, int direction) {
373 gras_error_t errcode;
374 chained_list_t *i, *j;
376 INFO0("==== Test on graph (cyclique chained list) ====");
377 /* init a value, exchange it and check its validity*/
378 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
382 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
383 &i,&j, sock,direction));
384 if (direction == READ || direction == RW) {
387 DEBUG1("i->l=%p" ,i->l);
388 DEBUG1("i->l->l=%p" ,i->l->l);
389 DEBUG1("i->l->l->l=%p",i->l->l->l);
391 DEBUG1("j->l=%p" ,j->l);
392 DEBUG1("j->l->l=%p" ,j->l->l);
393 DEBUG1("j->l->l->l=%p",j->l->l->l);
394 gras_assert4(j->l->l->l == j,
395 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
401 gras_assert(list_eq(i,j));
405 i->l->l->l = NULL; /* do this even in READ mode */
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;
427 char r_arch_char = gras_arch_selfid();
429 gras_init_defaultlog(&argc,argv,NULL);
431 for (cpt=1; cpt<argc; cpt++) {
432 if (!strcmp(argv[cpt], "--read")) {
434 } else if (!strcmp(argv[cpt], "--write")) {
441 if (direction == WRITE) {
442 TRYFAIL(gras_socket_client_from_file(filename,&sock));
443 TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
446 if (direction == READ) {
447 TRYFAIL(gras_socket_server_from_file(filename,&sock));
448 TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
449 gras_arch_selfid(), &r_arch_char));
450 INFO2("This datafile was generated on %s (%d)",
451 gras_datadesc_arch_name(r_arch_char),(int)r_arch_char);
453 r_arch = (int)r_arch_char;
455 TRYFAIL(test_int(sock,direction));
456 TRYFAIL(test_float(sock,direction));
457 TRYFAIL(test_array(sock,direction));
458 TRYFAIL(test_intref(sock,direction));
460 TRYFAIL(test_string(sock,direction));
462 TRYFAIL(test_homostruct(sock,direction));
463 TRYFAIL(test_hetestruct(sock,direction));
464 TRYFAIL(test_nestedstruct(sock,direction));
466 TRYFAIL(declare_chained_list_type());
467 TRYFAIL(test_chain_list(sock,direction));
468 TRYFAIL(test_graph(sock,direction));
471 gras_socket_close(sock);