1 /* datadesc: test of data description (using file transport). */
3 /* Copyright (c) 2007, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "..\..\..\..\include\gras\datadesc.h"
17 #include "gras/DataDesc/datadesc_interface.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this test");
26 const char *filename = "datadesc_usage.out";
29 write_read(gras_datadesc_type_t type, void *src, void *dst,
30 gras_socket_t sock, int direction);
33 write_read(gras_datadesc_type_t type, void *src, void *dst,
34 gras_socket_t sock, int direction)
39 sock = gras_socket_client_from_file(filename);
41 if (direction == WRITE || direction == RW)
42 gras_datadesc_send(sock, type, src);
45 gras_socket_close(sock);
49 sock = gras_socket_server_from_file(filename);
51 if (direction == READ || direction == RW)
52 gras_datadesc_recv(sock, type, r_arch, dst);
55 gras_socket_close(sock);
57 if (direction == COPY)
58 gras_datadesc_copy(type, src, dst);
61 void test_int(gras_socket_t sock, int direction);
62 void test_float(gras_socket_t sock, int direction);
63 void test_double(gras_socket_t sock, int direction);
64 void test_array(gras_socket_t sock, int direction);
65 void test_dynar_scal(gras_socket_t sock, int direction);
66 void test_intref(gras_socket_t sock, int direction);
67 void test_string(gras_socket_t sock, int direction);
69 void test_homostruct(gras_socket_t sock, int direction);
70 void test_hetestruct(gras_socket_t sock, int direction);
71 void test_nestedstruct(gras_socket_t sock, int direction);
72 void test_chain_list(gras_socket_t sock, int direction);
73 void test_graph(gras_socket_t sock, int direction);
74 void test_dynar_ref(gras_socket_t sock, int direction);
76 void test_pbio(gras_socket_t sock, int direction);
77 void test_clause(gras_socket_t sock, int direction);
79 /* defined in datadesc_structures.c, which in perl generated */
80 void test_structures(gras_socket_t sock, int direction);
82 void test_int(gras_socket_t sock, int direction)
86 INFO0("---- Test on integer ----");
88 write_read(gras_datadesc_by_name("int"), &i, &j, sock, direction);
90 if (direction == READ || direction == RW || direction == COPY)
94 void test_float(gras_socket_t sock, int direction)
98 INFO0("---- Test on float ----");
99 write_read(gras_datadesc_by_name("float"), &i, &j, sock, direction);
101 if (direction == READ || direction == RW || direction == COPY)
102 xbt_assert2(i == j, "%f != %f", i, j);
105 void test_double(gras_socket_t sock, int direction)
107 double i = -3252355.1234, j;
109 INFO0("---- Test on double ----");
110 write_read(gras_datadesc_by_name("double"), &i, &j, sock, direction);
112 if (direction == READ || direction == RW || direction == COPY)
113 xbt_assert2(i == j, "%f != %f", i, j);
117 typedef int array[SIZE];
119 void test_array(gras_socket_t sock, int direction)
121 gras_datadesc_type_t my_type;
123 array i = { 35212, -6226, 74337, 11414, 7733 };
127 INFO0("---- Test on fixed array ----");
130 gras_datadesc_array_fixed("fixed int array",
131 gras_datadesc_by_name("int"), SIZE);
133 write_read(my_type, &i, &j, sock, direction);
135 if (direction == READ || direction == RW || direction == COPY) {
136 for (cpt = 0; cpt < SIZE; cpt++) {
137 DEBUG1("Test spot %d", cpt);
138 xbt_assert4(i[cpt] == j[cpt], "i[%d]=%d != j[%d]=%d", cpt, i[cpt],
144 /*** Dynar of scalar ***/
146 void test_dynar_scal(gras_socket_t sock, int direction)
148 gras_datadesc_type_t my_type;
152 INFO0("---- Test on dynar containing integers ----");
153 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"), NULL);
154 i = xbt_dynar_new(sizeof(int), NULL);
156 for (cpt = 0; cpt < 64; cpt++) {
157 xbt_dynar_push_as(i, int, cpt);
158 DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(i));
161 /* xbt_dynar_dump(i); */
162 write_read(my_type, &i, &j, sock, direction);
164 /* xbt_dynar_dump(j); */
165 if (direction == READ || direction == RW || direction == COPY) {
166 for (cpt = 0; cpt < 64; cpt++) {
167 int ret = xbt_dynar_get_as(j, cpt, int);
171 ("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",
183 void test_intref(gras_socket_t sock, int direction)
185 gras_datadesc_type_t my_type;
191 INFO1("---- Test on a reference to an integer (%p) ----", i);
193 my_type = gras_datadesc_ref("int*", gras_datadesc_by_name("int"));
195 write_read(my_type, &i, &j, sock, direction);
196 if (direction == READ || direction == RW || direction == COPY) {
197 xbt_assert2(*i == *j, "*i != *j (%d != %d)", *i, *j);
205 *** string (dynamic array)
207 void test_string(gras_socket_t sock, int direction)
209 char *i = xbt_strdup("Some data"), *j = NULL;
212 INFO0("---- Test on string (ref to dynamic array) ----");
213 write_read(gras_datadesc_by_name("string"), &i, &j, sock, direction);
215 if (direction == READ || direction == RW || direction == COPY) {
216 for (cpt = 0; cpt < strlen(i); cpt++) {
217 xbt_assert4(i[cpt] == j[cpt], "i[%d]=%c != j[%d]=%c", cpt, i[cpt],
229 *** homogeneous struct
235 void test_homostruct(gras_socket_t sock, int direction)
237 gras_datadesc_type_t my_type;
240 INFO0("---- Test on homogeneous structure ----");
241 /* create descriptor */
242 my_type = gras_datadesc_struct("homostruct");
243 gras_datadesc_struct_append(my_type, "a",
244 gras_datadesc_by_name("signed int"));
245 gras_datadesc_struct_append(my_type, "b", gras_datadesc_by_name("int"));
246 gras_datadesc_struct_append(my_type, "c", gras_datadesc_by_name("int"));
247 gras_datadesc_struct_append(my_type, "d", gras_datadesc_by_name("int"));
248 gras_datadesc_struct_close(my_type);
249 my_type = gras_datadesc_ref("homostruct*",
250 gras_datadesc_by_name("homostruct"));
252 /* init a value, exchange it and check its validity */
253 i = xbt_new(homostruct, 1);
259 write_read(my_type, &i, &j, sock, direction);
260 if (direction == READ || direction == RW || direction == COPY) {
261 xbt_assert2(i->a == j->a, "i->a=%d != j->a=%d", i->a, j->a);
262 xbt_assert(i->b == j->b);
263 xbt_assert(i->c == j->c);
264 xbt_assert(i->d == j->d);
271 *** heterogeneous struct
275 unsigned long int l1;
277 unsigned long int l2;
279 void test_hetestruct(gras_socket_t sock, int direction)
281 gras_datadesc_type_t my_type;
284 INFO0("---- Test on heterogeneous structure ----");
285 /* create descriptor */
286 my_type = gras_datadesc_struct("hetestruct");
287 gras_datadesc_struct_append(my_type, "c1",
288 gras_datadesc_by_name("unsigned char"));
289 gras_datadesc_struct_append(my_type, "l1",
290 gras_datadesc_by_name("unsigned long int"));
291 gras_datadesc_struct_append(my_type, "c2",
292 gras_datadesc_by_name("unsigned char"));
293 gras_datadesc_struct_append(my_type, "l2",
294 gras_datadesc_by_name("unsigned long int"));
295 gras_datadesc_struct_close(my_type);
297 gras_datadesc_ref("hetestruct*",
298 gras_datadesc_by_name("hetestruct"));
301 /* init a value, exchange it and check its validity */
302 i = xbt_new(hetestruct, 1);
308 write_read(my_type, &i, &j, sock, direction);
309 if (direction == READ || direction == RW || direction == COPY) {
310 xbt_assert(i->c1 == j->c1);
311 xbt_assert(i->c2 == j->c2);
312 xbt_assert2(i->l1 == j->l1, "i->l1(=%ld) != j->l1(=%ld)", i->l1,
314 xbt_assert(i->l2 == j->l2);
327 void test_nestedstruct(gras_socket_t sock, int direction)
329 gras_datadesc_type_t my_type;
332 INFO0("---- Test on nested structures ----");
333 /* create descriptor */
334 my_type = gras_datadesc_struct("nestedstruct");
336 gras_datadesc_struct_append(my_type, "hete",
337 gras_datadesc_by_name("hetestruct"));
338 gras_datadesc_struct_append(my_type, "homo",
339 gras_datadesc_by_name("homostruct"));
340 gras_datadesc_struct_close(my_type);
342 gras_datadesc_ref("nestedstruct*",
343 gras_datadesc_by_name("nestedstruct"));
345 /* init a value, exchange it and check its validity */
346 i = xbt_new(nestedstruct, 1);
356 write_read(my_type, &i, &j, sock, direction);
357 if (direction == READ || direction == RW || direction == COPY) {
358 xbt_assert(i->homo.a == j->homo.a);
359 xbt_assert(i->homo.b == j->homo.b);
360 xbt_assert(i->homo.c == j->homo.c);
361 xbt_assert(i->homo.d == j->homo.d);
362 xbt_assert(i->hete.c1 == j->hete.c1);
363 xbt_assert(i->hete.c2 == j->hete.c2);
364 xbt_assert(i->hete.l1 == j->hete.l1);
365 xbt_assert(i->hete.l2 == j->hete.l2);
374 typedef struct s_chained_list chained_list_t;
375 struct s_chained_list {
379 void declare_chained_list_type(void);
380 chained_list_t *cons(int v, chained_list_t * l);
381 void list_free(chained_list_t * l);
382 int list_eq(chained_list_t * i, chained_list_t * j);
384 void declare_chained_list_type(void)
386 gras_datadesc_type_t my_type, ref_my_type;
388 my_type = gras_datadesc_struct("chained_list_t");
389 ref_my_type = gras_datadesc_ref("chained_list_t*", my_type);
391 gras_datadesc_struct_append(my_type, "v", gras_datadesc_by_name("int"));
392 gras_datadesc_struct_append(my_type, "l", ref_my_type);
393 gras_datadesc_struct_close(my_type);
396 chained_list_t *cons(int v, chained_list_t * l)
398 chained_list_t *nl = xbt_new(chained_list_t, 1);
406 void list_free(chained_list_t * l)
414 int list_eq(chained_list_t * i, chained_list_t * j)
420 return list_eq(i->l, j->l);
423 void test_chain_list(gras_socket_t sock, int direction)
425 chained_list_t *i, *j;
427 INFO0("---- Test on chained list ----");
429 /* init a value, exchange it and check its validity */
430 i = cons(12355, cons(246264, cons(23263, NULL)));
433 write_read(gras_datadesc_by_name("chained_list_t*"),
434 &i, &j, sock, direction);
435 if (direction == READ || direction == RW || direction == COPY) {
436 xbt_assert(list_eq(i, j));
446 void test_graph(gras_socket_t sock, int direction)
448 chained_list_t *i, *j;
450 INFO0("---- Test on graph (cyclique chained list of 3 items) ----");
451 /* init a value, exchange it and check its validity */
452 i = cons(1151515, cons(-232362, cons(222552, NULL)));
456 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
457 write_read(gras_datadesc_by_name("chained_list_t*"),
458 &i, &j, sock, direction);
459 if (direction == READ || direction == RW || direction == COPY) {
462 DEBUG1("i->l=%p", i->l);
463 DEBUG1("i->l->l=%p", i->l->l);
464 DEBUG1("i->l->l->l=%p", i->l->l->l);
466 DEBUG1("j->l=%p", j->l);
467 DEBUG1("j->l->l=%p", j->l->l);
468 DEBUG1("j->l->l->l=%p", j->l->l->l);
469 xbt_assert4(j->l->l->l == j,
470 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
471 "j=%p; &j=%p", j, j->l->l->l, j, &j);
474 xbt_assert(list_eq(i, j));
478 i->l->l->l = NULL; /* do this even in WRITE mode */
483 /*** Dynar of references ***/
484 static void free_string(void *d)
485 { /* used to free the data in dynar */
489 void test_dynar_ref(gras_socket_t sock, int direction)
491 gras_datadesc_type_t my_type;
497 INFO0("---- Test on dynar containing integers ----");
499 gras_datadesc_dynar(gras_datadesc_by_name("string"), &free_string);
501 i = xbt_dynar_new(sizeof(char *), &free_string);
502 for (cpt = 0; cpt < 64; cpt++) {
503 sprintf(buf, "%d", cpt);
505 xbt_dynar_push(i, &s1);
508 write_read(my_type, &i, &j, sock, direction);
509 if (direction == READ || direction == RW || direction == COPY) {
510 for (cpt = 0; cpt < 64; cpt++) {
511 sprintf(buf, "%d", cpt);
512 xbt_dynar_shift(j, &s2);
513 xbt_assert2(!strcmp(buf, s2),
514 "The retrieved value is not the same than the injected one (%s!=%s)",
525 GRAS_DEFINE_TYPE(s_pbio, struct s_pbio { /* structure presented in the IEEE article */
536 double Cdfgrd0[373][3];
537 double Cdfgrd1[3][3];
538 double Cstress[106]; double Cddsdde[106][106];};)
539 typedef struct s_pbio pbio_t;
541 void test_pbio(gras_socket_t sock, int direction)
545 gras_datadesc_type_t pbio_type;
549 ("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
550 pbio_type = gras_datadesc_by_symbol(s_pbio);
552 /* Fill in that damn struct */
554 for (cpt = 0; cpt < 12; cpt++)
555 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
557 for (cpt = 0; cpt < 110; cpt++)
558 i.Cprops[cpt] = cpt * 100.0;
559 for (cpt = 0; cpt < 4; cpt++)
560 i.Cndi[cpt] = cpt * 23262;
563 i.Cdtime = -11515.662;
564 i.Ctime[0] = 332523.226;
565 i.Ctime[1] = -26216.113;
566 i.Cntens = 235211411;
568 for (cpt = 0; cpt < 3; cpt++) {
569 for (cpt2 = 0; cpt2 < 373; cpt2++)
570 i.Cdfgrd0[cpt2][cpt] = ((double) cpt) * ((double) cpt2);
571 for (cpt2 = 0; cpt2 < 3; cpt2++)
572 i.Cdfgrd1[cpt][cpt2] = -((double) cpt) * ((double) cpt2);
574 for (cpt = 0; cpt < 106; cpt++) {
575 i.Cstress[cpt] = (double) cpt *22.113;
576 for (cpt2 = 0; cpt2 < 106; cpt2++)
577 i.Cddsdde[cpt][cpt2] = ((double) cpt) * ((double) cpt2);
579 write_read(gras_datadesc_by_symbol(s_pbio), &i, &j, sock, direction);
580 if (direction == READ || direction == RW || direction == COPY) {
581 /* Check that the data match */
582 xbt_assert(i.Cnstatv == j.Cnstatv);
583 for (cpt = 0; cpt < 12; cpt++)
584 xbt_assert4(i.Cstatev[cpt] == j.Cstatev[cpt],
585 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
586 cpt, i.Cstatev[cpt], cpt, j.Cstatev[cpt]);
587 xbt_assert(i.Cnprops == j.Cnprops);
588 for (cpt = 0; cpt < 110; cpt++)
589 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
590 for (cpt = 0; cpt < 4; cpt++)
591 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
592 xbt_assert(i.Cnshr == j.Cnshr);
593 xbt_assert(i.Cnpt == j.Cnpt);
594 xbt_assert(i.Cdtime == j.Cdtime);
595 xbt_assert(i.Ctime[0] == j.Ctime[0]);
596 xbt_assert(i.Ctime[1] == j.Ctime[1]);
597 xbt_assert(i.Cntens == j.Cntens);
598 for (cpt = 0; cpt < 3; cpt++) {
599 for (cpt2 = 0; cpt2 < 373; cpt2++)
600 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
601 for (cpt2 = 0; cpt2 < 3; cpt2++)
602 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
604 for (cpt = 0; cpt < 106; cpt++) {
605 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
606 for (cpt2 = 0; cpt2 < 106; cpt2++)
607 xbt_assert4(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
608 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
609 i.Cddsdde[cpt][cpt2], cpt, cpt2, j.Cddsdde[cpt][cpt2]);
614 GRAS_DEFINE_TYPE(s_clause, struct s_clause {
615 int num_lits; int *literals GRAS_ANNOTE(size, num_lits); /* Tells GRAS where to find the size */
619 typedef struct s_clause Clause;
621 void test_clause(gras_socket_t sock, int direction)
623 gras_datadesc_type_t ddt;
628 ("---- Test on struct containing dynamic array and its size (cbps test) ----");
630 /* create and fill the struct */
631 i = xbt_new(Clause, 1);
634 i->literals = xbt_new(int, i->num_lits);
635 for (cpt = 0; cpt < i->num_lits; cpt++)
636 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
637 DEBUG3("created data=%p (within %p @%p)", &(i->num_lits), i, &i);
638 DEBUG1("created count=%d", i->num_lits);
640 /* create the damn type descriptor */
641 ddt = gras_datadesc_by_symbol(s_clause);
642 /* gras_datadesc_type_dump(ddt); */
644 ddt = gras_datadesc_ref("Clause*", ddt);
646 write_read(ddt, &i, &j, sock, direction);
647 if (direction == READ || direction == RW || direction == COPY) {
648 xbt_assert(i->num_lits == j->num_lits);
649 for (cpt = 0; cpt < i->num_lits; cpt++)
650 xbt_assert(i->literals[cpt] == j->literals[cpt]);
661 int main(int argc, char *argv[])
663 gras_socket_t sock = NULL;
664 int direction = WRITE; /* RW; */
666 char r_arch_char = gras_arch_selfid();
668 gras_init(&argc, argv);
670 for (cpt = 1; cpt < argc; cpt++) {
671 if (!strcmp(argv[cpt], "--read")) {
673 } else if (!strcmp(argv[cpt], "--write")) {
675 } else if (!strcmp(argv[cpt], "--copy")) {
678 filename = argv[cpt];
682 if (direction == WRITE) {
683 sock = gras_socket_client_from_file(filename);
684 gras_datadesc_send(sock, gras_datadesc_by_name("char"), &r_arch_char);
686 if (direction == READ) {
687 sock = gras_socket_server_from_file(filename);
688 gras_datadesc_recv(sock, gras_datadesc_by_name("char"),
689 gras_arch_selfid(), &r_arch_char);
690 INFO3("This datafile was generated on %s (%d), I'm %s.",
691 gras_datadesc_arch_name(r_arch_char), (int) r_arch_char,
692 gras_datadesc_arch_name(gras_arch_selfid()));
694 r_arch = (int) r_arch_char;
696 test_int(sock, direction);
698 test_float(sock, direction);
700 test_double(sock, direction);
702 test_array(sock, direction);
704 test_intref(sock, direction);
706 test_string(sock, direction);
708 test_dynar_scal(sock, direction);
710 test_structures(sock, direction);
712 test_homostruct(sock, direction);
714 test_hetestruct(sock, direction);
716 test_nestedstruct(sock, direction);
718 declare_chained_list_type();
720 test_chain_list(sock, direction);
722 test_graph(sock, direction);
724 test_dynar_ref(sock, direction);
726 test_pbio(sock, direction);
728 test_clause(sock, direction);
730 if (direction != RW && direction != COPY)
731 gras_socket_close(sock);