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);
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("datadesc_usage.out",&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("datadesc_usage.out",&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 gras_assert4(i[cpt] == j[cpt],"i[%d]=%d != j[%d]=%d",
111 cpt,i[cpt],cpt,j[cpt]);
115 gras_error_t test_intref(gras_socket_t *sock, int direction) {
116 gras_error_t errcode;
117 gras_datadesc_type_t *my_type;
120 if (! (i=malloc(sizeof(int))) )
124 INFO1("==== Test on a reference to an integer (%p) ====",i);
126 TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
128 TRY(write_read(my_type, &i,&j, sock,direction));
129 if (direction == READ || direction == RW) {
130 gras_assert(*i == *j);
138 *** string (dynamic array)
140 gras_error_t test_string(gras_socket_t *sock, int direction) {
141 gras_error_t errcode;
142 char *i=strdup("Some data"), *j=NULL;
145 INFO0("==== Test on string (ref to dynamic array) ====");
146 TRY(write_read(gras_datadesc_by_name("string"), &i,&j,
148 if (direction == READ || direction == RW) {
149 for (cpt=0; cpt<strlen(i); cpt++) {
150 gras_assert4(i[cpt] == j[cpt],"i[%d]=%c != j[%d]=%c",
151 cpt,i[cpt],cpt,j[cpt]);
161 *** homogeneous struct
166 gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
167 gras_error_t errcode;
168 gras_datadesc_type_t *my_type;
171 INFO0("==== Test on homogeneous structure ====");
172 /* create descriptor */
173 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
174 TRY(gras_datadesc_declare_struct_append(my_type,"a",
175 gras_datadesc_by_name("signed int")));
176 TRY(gras_datadesc_declare_struct_append(my_type,"b",
177 gras_datadesc_by_name("int")));
178 TRY(gras_datadesc_declare_struct_append(my_type,"c",
179 gras_datadesc_by_name("int")));
180 TRY(gras_datadesc_declare_struct_append(my_type,"d",
181 gras_datadesc_by_name("int")));
182 TRY(gras_datadesc_declare_ref("homostruct*",
183 gras_datadesc_by_name("homostruct"),
186 /* init a value, exchange it and check its validity*/
187 if (! (i=malloc(sizeof(homostruct))) )
189 i->a = rand(); i->b = rand();
190 i->c = rand(); i->d = rand();
192 TRY(write_read(my_type, &i,&j, sock,direction));
193 if (direction == READ || direction == RW) {
194 gras_assert(i->a == j->a);
195 gras_assert(i->b == j->b);
196 gras_assert(i->c == j->c);
197 gras_assert(i->d == j->d);
205 *** heterogeneous struct
209 unsigned long int l1;
211 unsigned long int l2;
213 gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
214 gras_error_t errcode;
215 gras_datadesc_type_t *my_type;
218 INFO0("==== Test on heterogeneous structure ====");
219 /* create descriptor */
220 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
221 TRY(gras_datadesc_declare_struct_append(my_type,"c1",
222 gras_datadesc_by_name("unsigned char")));
223 TRY(gras_datadesc_declare_struct_append(my_type,"l1",
224 gras_datadesc_by_name("unsigned long int")));
225 TRY(gras_datadesc_declare_struct_append(my_type,"c2",
226 gras_datadesc_by_name("unsigned char")));
227 TRY(gras_datadesc_declare_struct_append(my_type,"l2",
228 gras_datadesc_by_name("unsigned long int")));
229 TRY(gras_datadesc_declare_ref("hetestruct*",
230 gras_datadesc_by_name("hetestruct"),
233 /* init a value, exchange it and check its validity*/
234 if (! (i=malloc(sizeof(hetestruct))) )
236 i->c1 = 's'; i->l1 = 123455;
237 i->c2 = 'e'; i->l2 = 774531;
239 TRY(write_read(my_type, &i,&j, sock,direction));
240 if (direction == READ || direction == RW) {
241 gras_assert(i->c1 == j->c1);
242 gras_assert(i->c2 == j->c2);
243 gras_assert(i->l1 == j->l1);
244 gras_assert(i->l2 == j->l2);
258 gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
259 gras_error_t errcode;
260 gras_datadesc_type_t *my_type;
263 INFO0("==== Test on nested structures ====");
264 /* create descriptor */
265 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
267 TRY(gras_datadesc_declare_struct_append(my_type,"hete",
268 gras_datadesc_by_name("hetestruct")));
269 TRY(gras_datadesc_declare_struct_append(my_type,"homo",
270 gras_datadesc_by_name("homostruct")));
271 TRY(gras_datadesc_declare_ref("nestedstruct*",
272 gras_datadesc_by_name("nestedstruct"),
275 /* init a value, exchange it and check its validity*/
276 if (! (i=malloc(sizeof(nestedstruct))) )
278 i->homo.a = rand(); i->homo.b = rand();
279 i->homo.c = rand(); i->homo.d = rand();
280 i->hete.c1 = 's'; i->hete.l1 = 123455;
281 i->hete.c2 = 'e'; i->hete.l2 = 774531;
283 TRY(write_read(my_type, &i,&j, sock,direction));
284 if (direction == READ || direction == RW) {
285 gras_assert(i->homo.a == j->homo.a);
286 gras_assert(i->homo.b == j->homo.b);
287 gras_assert(i->homo.c == j->homo.c);
288 gras_assert(i->homo.d == j->homo.d);
289 gras_assert(i->hete.c1 == j->hete.c1);
290 gras_assert(i->hete.c2 == j->hete.c2);
291 gras_assert(i->hete.l1 == j->hete.l1);
292 gras_assert(i->hete.l2 == j->hete.l2);
302 typedef struct s_chained_list chained_list_t;
303 struct s_chained_list {
307 gras_error_t declare_chained_list_type(void);
308 chained_list_t *cons(int v, chained_list_t *l);
309 void list_free(chained_list_t *l);
310 int list_eq(chained_list_t*i,chained_list_t*j);
312 gras_error_t declare_chained_list_type(void) {
313 gras_error_t errcode;
314 gras_datadesc_type_t *my_type,*ref_my_type;
316 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
317 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
319 TRY(gras_datadesc_declare_struct_append(my_type,"v",
320 gras_datadesc_by_name("int")));
321 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
326 chained_list_t * cons(int v, chained_list_t *l) {
327 chained_list_t *nl = malloc(sizeof (chained_list_t));
334 void list_free(chained_list_t*l) {
340 int list_eq(chained_list_t*i,chained_list_t*j) {
341 if (!i || !j) return i == j;
344 return list_eq(i->l, j->l);
346 gras_error_t test_chain_list(gras_socket_t *sock, int direction) {
347 gras_error_t errcode;
348 chained_list_t *i, *j;
350 INFO0("==== Test on chained list ====");
352 /* init a value, exchange it and check its validity*/
353 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
356 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
359 if (direction == READ || direction == RW) {
360 gras_assert(list_eq(i,j));
370 gras_error_t test_graph(gras_socket_t *sock, int direction) {
371 gras_error_t errcode;
372 chained_list_t *i, *j;
374 INFO0("==== Test on graph (cyclique chained list) ====");
375 /* init a value, exchange it and check its validity*/
376 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
380 TRY(write_read(gras_datadesc_by_name("chained_list_t*"),
381 &i,&j, sock,direction));
382 if (direction == READ || direction == RW) {
385 DEBUG1("i->l=%p" ,i->l);
386 DEBUG1("i->l->l=%p" ,i->l->l);
387 DEBUG1("i->l->l->l=%p",i->l->l->l);
389 DEBUG1("j->l=%p" ,j->l);
390 DEBUG1("j->l->l=%p" ,j->l->l);
391 DEBUG1("j->l->l->l=%p",j->l->l->l);
392 gras_assert4(j->l->l->l == j,
393 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
399 gras_assert(list_eq(i,j));
403 i->l->l->l = NULL; /* do this even in READ mode */
409 typedef struct { /* structure presented in the IEEE article */
414 int Cndi[4], Cnshr, Cnpt;
415 double Cdtime, Ctime[2];
417 double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
420 int main(int argc,char *argv[]) {
421 gras_error_t errcode;
424 char r_arch_char = gras_arch_selfid();
426 gras_init_defaultlog(&argc,argv,
427 "DataDesc.thresh=verbose"
429 //" set.thresh=debug"
432 if (!strcmp(argv[1], "--read"))
434 if (!strcmp(argv[1], "--write"))
438 if (direction == WRITE) {
439 TRYFAIL(gras_socket_client_from_file("datadesc_usage.out",&sock));
440 TRY(gras_datadesc_send(sock, gras_datadesc_by_name("char"),
443 if (direction == READ) {
444 TRYFAIL(gras_socket_server_from_file("datadesc_usage.out",&sock));
445 TRY(gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
446 gras_arch_selfid(), &r_arch_char));
447 INFO1("This datafile was generated on %s",
448 gras_datadesc_arch_name(r_arch_char));
450 r_arch = (int)r_arch_char;
452 TRYFAIL(test_int(sock,direction));
453 TRYFAIL(test_float(sock,direction));
454 TRYFAIL(test_array(sock,direction));
455 TRYFAIL(test_intref(sock,direction));
457 TRYFAIL(test_string(sock,direction));
459 TRYFAIL(test_homostruct(sock,direction));
460 TRYFAIL(test_hetestruct(sock,direction));
461 TRYFAIL(test_nestedstruct(sock,direction));
463 TRYFAIL(declare_chained_list_type());
464 TRYFAIL(test_chain_list(sock,direction));
465 TRYFAIL(test_graph(sock,direction));
468 gras_socket_close(sock);