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);
18 write_read(gras_datadesc_type_t *type,void *src, void *dst);
21 write_read(gras_datadesc_type_t *type,void *src, void *dst) {
26 TRY(gras_socket_client_from_file("datadesc_usage.out",&sock));
27 TRY(gras_datadesc_send(sock, type, src));
28 gras_socket_close(&sock);
31 TRY(gras_socket_server_from_file("datadesc_usage.out",&sock));
32 TRY(gras_datadesc_recv(sock, type, gras_arch_selfid(), dst));
33 gras_socket_close(&sock);
38 gras_error_t test_int(void);
39 gras_error_t test_float(void);
40 gras_error_t test_array(void);
41 gras_error_t test_intref(void);
42 gras_error_t test_string(void);
44 gras_error_t test_homostruct(void);
45 gras_error_t test_hetestruct(void);
46 gras_error_t test_nestedstruct(void);
47 gras_error_t test_chain_list(void);
48 gras_error_t test_graph(void);
50 gras_error_t test_int(void) {
54 INFO0("==== Test on integer ====");
55 TRY(write_read(gras_datadesc_by_name("int"), (void*)&i,(void**) &j));
60 gras_error_t test_float(void) {
64 INFO0("==== Test on float ====");
65 TRY(write_read(gras_datadesc_by_name("float"), (void*)&i,(void**) &j));
72 typedef int array[SIZE];
73 gras_error_t test_array(void) {
75 gras_datadesc_type_t *my_type;
80 INFO0("==== Test on fixed array ====");
81 for (cpt=0; cpt<SIZE; cpt++) {
86 TRY(gras_datadesc_declare_array_fixed("fixed int array",
87 gras_datadesc_by_name("int"),
90 TRY(write_read(my_type, (void*)&i,(void**) &j));
91 for (cpt=0; cpt<SIZE; cpt++)
92 gras_assert(i[cpt] == (*j)[cpt]);
96 gras_error_t test_intref(void) {
98 gras_datadesc_type_t *my_type;
101 if (! (i=malloc(sizeof(int))) )
104 INFO1("==== Test on a reference to an integer (%p) ====",i);
106 TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
108 TRY(write_read(my_type, (void*)&i,(void**) &j));
109 gras_assert(*i == **j);
115 *** string (dynamic array)
117 typedef char *string;
118 gras_error_t test_string(void) {
119 gras_error_t errcode;
120 char *i=strdup("Some data");
123 INFO0("==== Test on string (dynamic array) ====");
124 TRY(write_read(gras_datadesc_by_name("string"), (void*)i,(void**) &j));
125 gras_assert(!strcmp(i,j));
132 *** homogeneous struct
137 gras_error_t test_homostruct(void) {
138 gras_error_t errcode;
139 gras_datadesc_type_t *my_type;
142 INFO0("==== Test on homogeneous structure ====");
143 /* create descriptor */
144 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
145 TRY(gras_datadesc_declare_struct_append(my_type,"a",
146 gras_datadesc_by_name("signed int")));
147 TRY(gras_datadesc_declare_struct_append(my_type,"b",
148 gras_datadesc_by_name("int")));
149 TRY(gras_datadesc_declare_struct_append(my_type,"c",
150 gras_datadesc_by_name("int")));
151 TRY(gras_datadesc_declare_struct_append(my_type,"d",
152 gras_datadesc_by_name("int")));
154 /* init a value, exchange it and check its validity*/
155 if (! (i=malloc(sizeof(homostruct))) )
157 i->a = rand(); i->b = rand();
158 i->c = rand(); i->d = rand();
161 TRY(write_read(my_type, (void*)i, (void**)&j));
162 gras_assert(i->a == j->a);
163 gras_assert(i->b == j->b);
164 gras_assert(i->c == j->c);
165 gras_assert(i->d == j->d);
173 *** heterogeneous struct
177 unsigned long int l1;
179 unsigned long int l2;
181 gras_error_t test_hetestruct(void) {
182 gras_error_t errcode;
183 gras_datadesc_type_t *my_type;
186 INFO0("==== Test on heterogeneous structure ====");
187 /* create descriptor */
188 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
189 TRY(gras_datadesc_declare_struct_append(my_type,"c1",
190 gras_datadesc_by_name("unsigned char")));
191 TRY(gras_datadesc_declare_struct_append(my_type,"l1",
192 gras_datadesc_by_name("unsigned long int")));
193 TRY(gras_datadesc_declare_struct_append(my_type,"c2",
194 gras_datadesc_by_name("unsigned char")));
195 TRY(gras_datadesc_declare_struct_append(my_type,"l2",
196 gras_datadesc_by_name("unsigned long int")));
198 /* init a value, exchange it and check its validity*/
199 if (! (i=malloc(sizeof(hetestruct))) )
201 i->c1 = 's'; i->l1 = 123455;
202 i->c2 = 'e'; i->l2 = 774531;
205 TRY(write_read(my_type, (void*)i, (void**)&j));
206 gras_assert(i->c1 == j->c1);
207 gras_assert(i->c2 == j->c2);
208 gras_assert(i->l1 == j->l1);
209 gras_assert(i->l2 == j->l2);
223 gras_error_t test_nestedstruct(void) {
224 gras_error_t errcode;
225 gras_datadesc_type_t *my_type;
228 INFO0("==== Test on nested structures ====");
229 /* create descriptor */
230 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
231 TRY(gras_datadesc_declare_struct_append(my_type,"hete",
232 gras_datadesc_by_name("hetestruct")));
233 TRY(gras_datadesc_declare_struct_append(my_type,"homo",
234 gras_datadesc_by_name("homostruct")));
236 /* init a value, exchange it and check its validity*/
237 if (! (i=malloc(sizeof(nestedstruct))) )
239 i->homo.a = rand(); i->homo.b = rand();
240 i->homo.c = rand(); i->homo.d = rand();
241 i->hete.c1 = 's'; i->hete.l1 = 123455;
242 i->hete.c2 = 'e'; i->hete.l2 = 774531;
245 TRY(write_read(my_type, (void*)i, (void**)&j));
246 gras_assert(i->homo.a == j->homo.a);
247 gras_assert(i->homo.b == j->homo.b);
248 gras_assert(i->homo.c == j->homo.c);
249 gras_assert(i->homo.d == j->homo.d);
250 gras_assert(i->hete.c1 == j->hete.c1);
251 gras_assert(i->hete.c2 == j->hete.c2);
252 gras_assert(i->hete.l1 == j->hete.l1);
253 gras_assert(i->hete.l2 == j->hete.l2);
263 typedef struct s_chained_list chained_list_t;
264 struct s_chained_list {
268 chained_list_t *cons(int v, chained_list_t *l);
269 void list_free(chained_list_t *l);
270 int list_eq(chained_list_t*i,chained_list_t*j);
271 chained_list_t * cons(int v, chained_list_t *l) {
272 chained_list_t *nl = malloc(sizeof (chained_list_t));
279 void list_free(chained_list_t*l) {
285 int list_eq(chained_list_t*i,chained_list_t*j) {
286 if (!i || !j) return i == j;
289 return list_eq(i->l, j->l);
291 gras_error_t test_chain_list(void) {
292 gras_error_t errcode;
293 gras_datadesc_type_t *my_type,*ref_my_type;
294 chained_list_t *i, *j;
296 INFO0("==== Test on chained list ====");
297 /* create descriptor */
298 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
299 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
301 TRY(gras_datadesc_declare_struct_append(my_type,"v",
302 gras_datadesc_by_name("int")));
303 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
305 /* init a value, exchange it and check its validity*/
306 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
309 TRY(write_read(my_type, (void*)i, (void**)&j));
310 gras_assert(list_eq(i,j));
319 gras_error_t test_graph(void) {
320 gras_error_t errcode;
321 gras_datadesc_type_t *my_type;
322 chained_list_t *i, *j;
324 INFO0("==== Test on graph (cyclique chained list) ====");
325 my_type = gras_datadesc_by_name("chained_list_t*");
326 gras_assert(my_type);
328 /* init a value, exchange it and check its validity*/
329 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
333 TRY(write_read(my_type, (void*)&i, (void**)&j));
335 INFO1("j->l=%p" ,j->l);
336 INFO1("j->l->l=%p" ,j->l->l);
337 INFO1("j->l->l->l=%p",j->l->l->l);
338 gras_assert4(j->l->l->l == j->l,
339 "Received list is not cyclic. j->l=%p != j->l->l->l=%p\n"
345 gras_assert(list_eq(i,j));
353 typedef struct { /* structure presented in the IEEE article */
358 int Cndi[4], Cnshr, Cnpt;
359 double Cdtime, Ctime[2];
361 double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
364 int main(int argc,char *argv[]) {
365 gras_error_t errcode;
367 gras_init_defaultlog(&argc,argv,
368 "DataDesc.thresh=verbose"
370 // " set.thresh=debug"
375 TRYFAIL(test_float());
376 TRYFAIL(test_array());
377 TRYFAIL(test_intref());
378 TRYFAIL(test_string());
380 TRYFAIL(test_homostruct());
381 TRYFAIL(test_hetestruct());
382 TRYFAIL(test_nestedstruct());
384 TRYFAIL(test_chain_list());
386 CRITICAL0("Not even test graphs: it dies with awfully scaring messages");
388 // TRYFAIL(test_graph());