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) {
52 gras_datadesc_type_t *type;
55 INFO0("==== Test on integer ====");
56 TRY(gras_datadesc_by_name("int", &type));
57 TRY(write_read(type, (void*)&i,(void**) &j));
62 gras_error_t test_float(void) {
64 gras_datadesc_type_t *type;
67 INFO0("==== Test on float ====");
68 TRY(gras_datadesc_by_name("float", &type));
69 TRY(write_read(type, (void*)&i,(void**) &j));
76 typedef int array[SIZE];
77 gras_error_t test_array(void) {
79 gras_datadesc_type_t *int_type;
80 gras_datadesc_type_t *my_type;
85 INFO0("==== Test on fixed array ====");
86 for (cpt=0; cpt<SIZE; cpt++) {
91 TRY(gras_datadesc_by_name("int", &int_type));
92 TRY(gras_datadesc_declare_array_fixed("fixed int array",
93 int_type, 5, &my_type));
95 TRY(write_read(my_type, (void*)&i,(void**) &j));
96 for (cpt=0; cpt<SIZE; cpt++)
97 gras_assert(i[cpt] == (*j)[cpt]);
101 gras_error_t test_intref(void) {
102 gras_error_t errcode;
103 gras_datadesc_type_t *int_type;
104 gras_datadesc_type_t *my_type;
107 if (! (i=malloc(sizeof(int))) )
110 INFO1("==== Test on a reference to an integer (%p) ====",i);
112 TRY(gras_datadesc_by_name("int", &int_type));
113 TRY(gras_datadesc_declare_ref("int*",int_type,&my_type));
115 TRY(write_read(my_type, (void*)&i,(void**) &j));
116 gras_assert(*i == **j);
122 *** string (dynamic array)
124 typedef char *string;
125 gras_error_t test_string(void) {
126 gras_error_t errcode;
127 gras_datadesc_type_t *type;
128 char *i=strdup("Some data");
131 INFO0("==== Test on string (dynamic array) ====");
132 TRY(gras_datadesc_by_name("string", &type));
133 TRY(write_read(type, (void*)i,(void**) &j));
134 gras_assert(!strcmp(i,j));
141 *** homogeneous struct
146 gras_error_t test_homostruct(void) {
147 gras_error_t errcode;
148 gras_datadesc_type_t *my_type;
151 INFO0("==== Test on homogeneous structure ====");
152 /* create descriptor */
153 TRY(gras_datadesc_declare_struct("homostruct",&my_type));
154 TRY(gras_datadesc_declare_struct_append_name(my_type,"a","signed int"));
155 TRY(gras_datadesc_declare_struct_append_name(my_type,"b","int"));
156 TRY(gras_datadesc_declare_struct_append_name(my_type,"c","int"));
157 TRY(gras_datadesc_declare_struct_append_name(my_type,"d","int"));
159 /* init a value, exchange it and check its validity*/
160 if (! (i=malloc(sizeof(homostruct))) )
162 i->a = rand(); i->b = rand();
163 i->c = rand(); i->d = rand();
166 TRY(write_read(my_type, (void*)i, (void**)&j));
167 gras_assert(i->a == j->a);
168 gras_assert(i->b == j->b);
169 gras_assert(i->c == j->c);
170 gras_assert(i->d == j->d);
178 *** heterogeneous struct
182 unsigned long int l1;
184 unsigned long int l2;
186 gras_error_t test_hetestruct(void) {
187 gras_error_t errcode;
188 gras_datadesc_type_t *my_type;
191 INFO0("==== Test on heterogeneous structure ====");
192 /* create descriptor */
193 TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
194 TRY(gras_datadesc_declare_struct_append_name(my_type,"c1","unsigned char"));
195 TRY(gras_datadesc_declare_struct_append_name(my_type,"l1","unsigned long int"));
196 TRY(gras_datadesc_declare_struct_append_name(my_type,"c2","unsigned char"));
197 TRY(gras_datadesc_declare_struct_append_name(my_type,"l2","unsigned long int"));
199 /* init a value, exchange it and check its validity*/
200 if (! (i=malloc(sizeof(hetestruct))) )
202 i->c1 = 's'; i->l1 = 123455;
203 i->c2 = 'e'; i->l2 = 774531;
206 TRY(write_read(my_type, (void*)i, (void**)&j));
207 gras_assert(i->c1 == j->c1);
208 gras_assert(i->c2 == j->c2);
209 gras_assert(i->l1 == j->l1);
210 gras_assert(i->l2 == j->l2);
224 gras_error_t test_nestedstruct(void) {
225 gras_error_t errcode;
226 gras_datadesc_type_t *my_type;
229 INFO0("==== Test on nested structures ====");
230 /* create descriptor */
231 TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
232 TRY(gras_datadesc_declare_struct_append_name(my_type,"hete","hetestruct"));
233 TRY(gras_datadesc_declare_struct_append_name(my_type,"homo","homostruct"));
235 /* init a value, exchange it and check its validity*/
236 if (! (i=malloc(sizeof(nestedstruct))) )
238 i->homo.a = rand(); i->homo.b = rand();
239 i->homo.c = rand(); i->homo.d = rand();
240 i->hete.c1 = 's'; i->hete.l1 = 123455;
241 i->hete.c2 = 'e'; i->hete.l2 = 774531;
244 TRY(write_read(my_type, (void*)i, (void**)&j));
245 gras_assert(i->homo.a == j->homo.a);
246 gras_assert(i->homo.b == j->homo.b);
247 gras_assert(i->homo.c == j->homo.c);
248 gras_assert(i->homo.d == j->homo.d);
249 gras_assert(i->hete.c1 == j->hete.c1);
250 gras_assert(i->hete.c2 == j->hete.c2);
251 gras_assert(i->hete.l1 == j->hete.l1);
252 gras_assert(i->hete.l2 == j->hete.l2);
262 typedef struct s_chained_list chained_list_t;
263 struct s_chained_list {
267 chained_list_t *cons(int v, chained_list_t *l);
268 void list_free(chained_list_t *l);
269 int list_eq(chained_list_t*i,chained_list_t*j);
270 chained_list_t * cons(int v, chained_list_t *l) {
271 chained_list_t *nl = malloc(sizeof (chained_list_t));
278 void list_free(chained_list_t*l) {
284 int list_eq(chained_list_t*i,chained_list_t*j) {
285 if (!i || !j) return i == j;
288 return list_eq(i->l, j->l);
290 gras_error_t test_chain_list(void) {
291 gras_error_t errcode;
292 gras_datadesc_type_t *my_type,*ref_my_type;
293 chained_list_t *i, *j;
295 INFO0("==== Test on chained list ====");
296 /* create descriptor */
297 TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
298 TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
300 TRY(gras_datadesc_declare_struct_append_name(my_type,"v","int"));
301 TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
303 /* init a value, exchange it and check its validity*/
304 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
307 TRY(write_read(my_type, (void*)i, (void**)&j));
308 gras_assert(list_eq(i,j));
317 gras_error_t test_graph(void) {
318 gras_error_t errcode;
319 gras_datadesc_type_t *my_type;
320 chained_list_t *i, *j;
322 INFO0("==== Test on graph (cyclique chained list) ====");
323 TRY(gras_datadesc_by_name("chained_list_t*", &my_type));
325 /* init a value, exchange it and check its validity*/
326 i = cons( rand(), cons( rand() , cons( rand(), NULL)));
330 TRY(write_read(my_type, (void*)&i, (void**)&j));
332 INFO1("j->l=%p" ,j->l);
333 INFO1("j->l->l=%p" ,j->l->l);
334 INFO1("j->l->l->l=%p",j->l->l->l);
335 gras_assert4(j->l->l->l == j->l,
336 "Received list is not cyclic. j->l=%p != j->l->l->l=%p\n"
342 gras_assert(list_eq(i,j));
350 typedef struct { /* structure presented in the IEEE article */
355 int Cndi[4], Cnshr, Cnpt;
356 double Cdtime, Ctime[2];
358 double Cdfgrd0[3][373], Cdfgrd1[3][3], Cstress[106], Cddsdde[106][106];
361 int main(int argc,char *argv[]) {
362 gras_error_t errcode;
364 gras_init_defaultlog(&argc,argv,
365 "DataDesc.thresh=verbose"
367 // " set.thresh=debug"
372 TRYFAIL(test_float());
373 TRYFAIL(test_array());
374 TRYFAIL(test_intref());
375 TRYFAIL(test_string());
377 TRYFAIL(test_homostruct());
378 TRYFAIL(test_hetestruct());
379 TRYFAIL(test_nestedstruct());
381 TRYFAIL(test_chain_list());
383 CRITICAL0("Not even test graphs: it dies with awfully scaring messages");
385 // TRYFAIL(test_graph());