1 /* datadesc: test of data description (using file transport). */
3 /* Copyright (c) 2007, 2009, 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. */
16 #include "gras/DataDesc/datadesc_interface.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this test");
23 const char *filename = "datadesc_usage.out";
26 write_read(const char *msgtype, void *src, void *dst,
27 gras_socket_t sock, int direction);
30 write_read(const char *msgtype, void *src, void *dst,
31 gras_socket_t sock, int direction)
36 gras_msg_wait(15, msgtype, NULL, dst);
40 gras_msg_send(sock, msgtype, src);
44 gras_datadesc_memcpy(gras_datadesc_by_name(msgtype), src, dst);
50 /* defined in datadesc_structures.c, which in perl generated */
51 void register_structures(void);
52 void test_structures(gras_socket_t sock, int direction);
54 /************************
55 * Each and every tests *
56 ************************/
58 static void test_int(gras_socket_t sock, int direction)
62 XBT_INFO("---- Test on integer ----");
63 write_read("int", &i, &j, sock, direction);
64 if (direction == READ || direction == COPY)
68 static void test_float(gras_socket_t sock, int direction)
72 XBT_INFO("---- Test on float ----");
73 write_read("float", &i, &j, sock, direction);
74 if (direction == READ || direction == COPY)
75 xbt_assert(i == j, "%f != %f", i, j);
78 static void test_double(gras_socket_t sock, int direction)
80 double i = -3252355.1234, j;
82 XBT_INFO("---- Test on double ----");
83 write_read("double", &i, &j, sock, direction);
84 if (direction == READ || direction == COPY)
85 xbt_assert(i == j, "%f != %f", i, j);
88 #define FIXED_ARRAY_SIZE 5
89 typedef int array[FIXED_ARRAY_SIZE];
90 static void test_array(gras_socket_t sock, int direction)
92 array i = { 35212, -6226, 74337, 11414, 7733 };
96 XBT_INFO("---- Test on fixed array ----");
98 write_read("fixed int array", &i, &j, sock, direction);
99 if (direction == READ || direction == COPY) {
100 for (cpt = 0; cpt < FIXED_ARRAY_SIZE; cpt++) {
101 XBT_DEBUG("Test spot %d", cpt);
102 xbt_assert(i[cpt] == j[cpt], "i[%d]=%d != j[%d]=%d",
103 cpt, i[cpt], cpt, j[cpt]);
108 /*** Dynar of scalar ***/
110 static void test_dynar_scal(gras_socket_t sock, int direction)
115 XBT_INFO("---- Test on dynar containing integers ----");
116 i = xbt_dynar_new(sizeof(int), NULL);
117 for (cpt = 0; cpt < 64; cpt++) {
118 xbt_dynar_push_as(i, int, cpt);
119 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(i));
121 /* xbt_dynar_dump(i); */
122 write_read("xbt_dynar_of_int", &i, &j, sock, direction);
123 /* xbt_dynar_dump(j); */
124 if (direction == READ || direction == COPY) {
125 for (cpt = 0; cpt < 64; cpt++) {
126 int ret = xbt_dynar_get_as(j, cpt, int);
129 ("The retrieved value for cpt=%d is not the same than the injected one (%d!=%d)",
139 /*** Empty dynar ***/
141 static void test_dynar_empty(gras_socket_t sock, int direction)
145 XBT_INFO("---- Test on empty dynar of integers ----");
146 i = xbt_dynar_new(sizeof(int), NULL);
147 write_read("xbt_dynar_of_int", &i, &j, sock, direction);
148 /* xbt_dynar_dump(j); */
149 if (direction == READ || direction == COPY) {
150 xbt_assert(xbt_dynar_length(j) == 0);
156 static void test_intref(gras_socket_t sock, int direction)
163 XBT_INFO("---- Test on a reference to an integer ----");
165 write_read("int*", &i, &j, sock, direction);
166 if (direction == READ || direction == COPY) {
167 xbt_assert(*i == *j, "*i != *j (%d != %d)", *i, *j);
174 *** string (dynamic array)
176 static void test_string(gras_socket_t sock, int direction)
178 char *i = xbt_strdup("Some data"), *j = NULL;
181 XBT_INFO("---- Test on string (ref to dynamic array) ----");
182 write_read("string", &i, &j, sock, direction);
183 if (direction == READ || direction == COPY) {
184 for (cpt = 0; cpt < strlen(i); cpt++) {
185 xbt_assert(i[cpt] == j[cpt], "i[%d]=%c != j[%d]=%c",
186 cpt, i[cpt], cpt, j[cpt]);
195 *** homogeneous struct
200 static void test_homostruct(gras_socket_t sock, int direction)
204 XBT_INFO("---- Test on homogeneous structure ----");
205 /* init a value, exchange it and check its validity */
206 i = xbt_new(homostruct, 1);
212 write_read("homostruct*", &i, &j, sock, direction);
213 if (direction == READ || direction == COPY) {
214 xbt_assert(i->a == j->a, "i->a=%d != j->a=%d", i->a, j->a);
215 xbt_assert(i->b == j->b);
216 xbt_assert(i->c == j->c);
217 xbt_assert(i->d == j->d);
224 *** heterogeneous struct
228 unsigned long int l1;
230 unsigned long int l2;
232 static void test_hetestruct(gras_socket_t sock, int direction)
236 XBT_INFO("---- Test on heterogeneous structure ----");
237 /* init a value, exchange it and check its validity */
238 i = xbt_new(hetestruct, 1);
244 write_read("hetestruct*", &i, &j, sock, direction);
245 if (direction == READ || direction == COPY) {
246 xbt_assert(i->c1 == j->c1);
247 xbt_assert(i->c2 == j->c2);
248 xbt_assert(i->l1 == j->l1, "i->l1(=%ld) != j->l1(=%ld)", i->l1,
250 xbt_assert(i->l2 == j->l2);
256 static void test_hetestruct_array(gras_socket_t sock, int direction)
258 hetestruct *i, *j, *p, *q;
261 XBT_INFO("---- Test on heterogeneous structure arrays ----");
262 /* init a value, exchange it and check its validity */
263 i = xbt_malloc(sizeof(hetestruct) * 10);
264 for (cpt = 0, p = i; cpt < 10; cpt++, p++) {
266 p->l1 = 123455 + cpt;
268 p->l2 = 774531 + cpt;
271 write_read("hetestruct[10]*", &i, &j, sock, direction);
272 if (direction == READ || direction == COPY) {
273 for (cpt = 0, p = i, q = j; cpt < 10; cpt++, p++, q++) {
274 xbt_assert(p->c1 == q->c1);
275 xbt_assert(p->c2 == q->c2);
276 xbt_assert(p->l1 == p->l1,
277 "for cpt=%d i->l1(=%ld) != j->l1(=%ld)", cpt, p->l1,
279 xbt_assert(q->l2 == p->l2);
293 static void test_nestedstruct(gras_socket_t sock, int direction)
297 XBT_INFO("---- Test on nested structures ----");
298 /* init a value, exchange it and check its validity */
299 i = xbt_new(nestedstruct, 1);
309 write_read("nestedstruct*", &i, &j, sock, direction);
310 if (direction == READ || direction == COPY) {
311 xbt_assert(i->homo.a == j->homo.a);
312 xbt_assert(i->homo.b == j->homo.b);
313 xbt_assert(i->homo.c == j->homo.c);
314 xbt_assert(i->homo.d == j->homo.d);
315 xbt_assert(i->hete.c1 == j->hete.c1);
316 xbt_assert(i->hete.c2 == j->hete.c2);
317 xbt_assert(i->hete.l1 == j->hete.l1);
318 xbt_assert(i->hete.l2 == j->hete.l2);
327 typedef struct s_chained_list chained_list_t;
328 struct s_chained_list {
332 chained_list_t *cons(int v, chained_list_t * l);
333 void list_free(chained_list_t * l);
334 int list_eq(chained_list_t * i, chained_list_t * j);
336 chained_list_t *cons(int v, chained_list_t * l)
338 chained_list_t *nl = xbt_new(chained_list_t, 1);
346 void list_free(chained_list_t * l)
354 int list_eq(chained_list_t * i, chained_list_t * j)
360 return list_eq(i->l, j->l);
363 static void test_chain_list(gras_socket_t sock, int direction)
365 chained_list_t *i, *j;
367 XBT_INFO("---- Test on chained list ----");
369 /* init a value, exchange it and check its validity */
370 i = cons(12355, cons(246264, cons(23263, NULL)));
373 write_read("chained_list_t*", &i, &j, sock, direction);
374 if (direction == READ || direction == COPY) {
375 xbt_assert(list_eq(i, j));
385 static void test_graph(gras_socket_t sock, int direction)
387 chained_list_t *i, *j;
389 XBT_INFO("---- Test on graph (cyclique chained list of 3 items) ----");
390 /* init a value, exchange it and check its validity */
391 i = cons(1151515, cons(-232362, cons(222552, NULL)));
395 write_read("chained_list_t*", &i, &j, sock, direction);
396 if (direction == READ || direction == COPY) {
398 XBT_DEBUG("i=%p", i);
399 XBT_DEBUG("i->l=%p", i->l);
400 XBT_DEBUG("i->l->l=%p", i->l->l);
401 XBT_DEBUG("i->l->l->l=%p", i->l->l->l);
402 XBT_DEBUG("j=%p", j);
403 XBT_DEBUG("j->l=%p", j->l);
404 XBT_DEBUG("j->l->l=%p", j->l->l);
405 XBT_DEBUG("j->l->l->l=%p", j->l->l->l);
406 xbt_assert(j->l->l->l == j,
407 "Received list is not cyclic. j=%p != j->l->l->l=%p\n"
408 "j=%p; &j=%p", j, j->l->l->l, j, &j);
411 xbt_assert(list_eq(i, j));
415 i->l->l->l = NULL; /* do this even in WRITE mode */
420 /*** Dynar of references ***/
421 static void free_string(void *d)
422 { /* used to free the data in dynar */
426 static void test_dynar_ref(gras_socket_t sock, int direction)
433 XBT_INFO("---- Test on dynar containing integers ----");
435 i = xbt_dynar_new(sizeof(char *), &free_string);
436 for (cpt = 0; cpt < 64; cpt++) {
437 sprintf(buf, "%d", cpt);
439 xbt_dynar_push(i, &s1);
442 write_read("xbt_dynar_of_string", &i, &j, sock, direction);
443 if (direction == READ || direction == COPY) {
444 for (cpt = 0; cpt < 64; cpt++) {
445 sprintf(buf, "%d", cpt);
446 xbt_dynar_shift(j, &s2);
447 xbt_assert(!strcmp(buf, s2),
448 "The retrieved value is not the same than the injected one (%s!=%s)",
459 GRAS_DEFINE_TYPE(s_pbio, struct s_pbio { /* structure presented in the IEEE article */
461 double Cstatev[12]; int Cnprops; double Cprops[110];
464 int Cnpt; double Cdtime; double Ctime[2]; int Cntens;
465 double Cdfgrd0[373][3];
466 double Cdfgrd1[3][3]; double Cstress[106];
467 double Cddsdde[106][106];
472 typedef struct s_pbio pbio_t;
474 static void test_pbio(gras_socket_t sock, int direction)
478 gras_datadesc_type_t pbio_type;
482 ("---- Test on the PBIO IEEE struct (also tests GRAS DEFINE TYPE) ----");
483 pbio_type = gras_datadesc_by_symbol(s_pbio);
485 /* Fill in that damn struct */
487 for (cpt = 0; cpt < 12; cpt++)
488 i.Cstatev[cpt] = ((double) cpt) * -2361.11;
490 for (cpt = 0; cpt < 110; cpt++)
491 i.Cprops[cpt] = cpt * 100.0;
492 for (cpt = 0; cpt < 4; cpt++)
493 i.Cndi[cpt] = cpt * 23262;
496 i.Cdtime = -11515.662;
497 i.Ctime[0] = 332523.226;
498 i.Ctime[1] = -26216.113;
499 i.Cntens = 235211411;
501 for (cpt = 0; cpt < 3; cpt++) {
502 for (cpt2 = 0; cpt2 < 373; cpt2++)
503 i.Cdfgrd0[cpt2][cpt] = ((double) cpt) * ((double) cpt2);
504 for (cpt2 = 0; cpt2 < 3; cpt2++)
505 i.Cdfgrd1[cpt][cpt2] = -((double) cpt) * ((double) cpt2);
507 for (cpt = 0; cpt < 106; cpt++) {
508 i.Cstress[cpt] = (double) cpt *22.113;
509 for (cpt2 = 0; cpt2 < 106; cpt2++)
510 i.Cddsdde[cpt][cpt2] = ((double) cpt) * ((double) cpt2);
512 write_read("s_pbio", &i, &j, sock, direction);
513 if (direction == READ || direction == COPY) {
514 /* Check that the data match */
515 xbt_assert(i.Cnstatv == j.Cnstatv);
516 for (cpt = 0; cpt < 12; cpt++)
517 xbt_assert(i.Cstatev[cpt] == j.Cstatev[cpt],
518 "i.Cstatev[%d] (=%f) != j.Cstatev[%d] (=%f)",
519 cpt, i.Cstatev[cpt], cpt, j.Cstatev[cpt]);
520 xbt_assert(i.Cnprops == j.Cnprops);
521 for (cpt = 0; cpt < 110; cpt++)
522 xbt_assert(i.Cprops[cpt] == j.Cprops[cpt]);
523 for (cpt = 0; cpt < 4; cpt++)
524 xbt_assert(i.Cndi[cpt] == j.Cndi[cpt]);
525 xbt_assert(i.Cnshr == j.Cnshr);
526 xbt_assert(i.Cnpt == j.Cnpt);
527 xbt_assert(i.Cdtime == j.Cdtime);
528 xbt_assert(i.Ctime[0] == j.Ctime[0]);
529 xbt_assert(i.Ctime[1] == j.Ctime[1]);
530 xbt_assert(i.Cntens == j.Cntens);
531 for (cpt = 0; cpt < 3; cpt++) {
532 for (cpt2 = 0; cpt2 < 373; cpt2++)
533 xbt_assert(i.Cdfgrd0[cpt2][cpt] == j.Cdfgrd0[cpt2][cpt]);
534 for (cpt2 = 0; cpt2 < 3; cpt2++)
535 xbt_assert(i.Cdfgrd1[cpt][cpt2] == j.Cdfgrd1[cpt][cpt2]);
537 for (cpt = 0; cpt < 106; cpt++) {
538 xbt_assert(i.Cstress[cpt] == j.Cstress[cpt]);
539 for (cpt2 = 0; cpt2 < 106; cpt2++)
540 xbt_assert(i.Cddsdde[cpt][cpt2] == j.Cddsdde[cpt][cpt2],
541 "%f=i.Cddsdde[%d][%d] != j.Cddsdde[cpt][cpt2]=%f",
542 i.Cddsdde[cpt][cpt2], cpt, cpt2, j.Cddsdde[cpt][cpt2]);
547 GRAS_DEFINE_TYPE(s_clause, struct s_clause {
549 int *literals GRAS_ANNOTE(size, num_lits); /* Tells GRAS where to find the size */
552 typedef struct s_clause Clause;
554 static void test_clause(gras_socket_t sock, int direction)
560 ("---- Test on struct containing dynamic array and its size (cbps test) ----");
562 /* create and fill the struct */
563 i = xbt_new(Clause, 1);
566 i->literals = xbt_new(int, i->num_lits);
567 for (cpt = 0; cpt < i->num_lits; cpt++)
568 i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
569 XBT_DEBUG("created data=%p (within %p @%p)", &(i->num_lits), i, &i);
570 XBT_DEBUG("created count=%d", i->num_lits);
572 write_read("Clause*", &i, &j, sock, direction);
573 if (direction == READ || direction == COPY) {
574 xbt_assert(i->num_lits == j->num_lits);
575 for (cpt = 0; cpt < i->num_lits; cpt++)
576 xbt_assert(i->literals[cpt] == j->literals[cpt]);
585 static void test_clause_empty(gras_socket_t sock, int direction)
590 ("---- Test on struct containing dynamic array and its size when size=0 (cbps test) ----");
592 /* create and fill the struct */
593 i = xbt_new(Clause, 1);
597 XBT_DEBUG("created data=%p (within %p @%p)", &(i->num_lits), i, &i);
598 XBT_DEBUG("created count=%d", i->num_lits);
600 write_read("Clause*", &i, &j, sock, direction);
601 if (direction == READ || direction == COPY) {
602 xbt_assert(i->num_lits == j->num_lits);
611 static void register_types(void)
613 gras_datadesc_type_t my_type, ref_my_type;
615 gras_msgtype_declare("char", gras_datadesc_by_name("char"));
616 gras_msgtype_declare("int", gras_datadesc_by_name("int"));
617 gras_msgtype_declare("float", gras_datadesc_by_name("float"));
618 gras_msgtype_declare("double", gras_datadesc_by_name("double"));
620 my_type = gras_datadesc_array_fixed("fixed int array",
621 gras_datadesc_by_name("int"),
623 gras_msgtype_declare("fixed int array", my_type);
625 my_type = gras_datadesc_dynar(gras_datadesc_by_name("int"), NULL);
626 gras_msgtype_declare("xbt_dynar_of_int", my_type);
628 my_type = gras_datadesc_ref("int*", gras_datadesc_by_name("int"));
629 gras_msgtype_declare("int*", my_type);
631 gras_msgtype_declare("string", gras_datadesc_by_name("string"));
633 my_type = gras_datadesc_struct("homostruct");
634 gras_datadesc_struct_append(my_type, "a",
635 gras_datadesc_by_name("signed int"));
636 gras_datadesc_struct_append(my_type, "b", gras_datadesc_by_name("int"));
637 gras_datadesc_struct_append(my_type, "c", gras_datadesc_by_name("int"));
638 gras_datadesc_struct_append(my_type, "d", gras_datadesc_by_name("int"));
639 gras_datadesc_struct_close(my_type);
641 gras_datadesc_ref("homostruct*",
642 gras_datadesc_by_name("homostruct"));
643 gras_msgtype_declare("homostruct*", my_type);
645 my_type = gras_datadesc_struct("hetestruct");
646 gras_datadesc_struct_append(my_type, "c1",
647 gras_datadesc_by_name("unsigned char"));
648 gras_datadesc_struct_append(my_type, "l1",
649 gras_datadesc_by_name("unsigned long int"));
650 gras_datadesc_struct_append(my_type, "c2",
651 gras_datadesc_by_name("unsigned char"));
652 gras_datadesc_struct_append(my_type, "l2",
653 gras_datadesc_by_name("unsigned long int"));
654 gras_datadesc_struct_close(my_type);
656 gras_datadesc_ref("hetestruct*",
657 gras_datadesc_by_name("hetestruct"));
658 gras_msgtype_declare("hetestruct*", my_type);
661 gras_datadesc_array_fixed("hetestruct[10]",
662 gras_datadesc_by_name("hetestruct"), 10);
663 my_type = gras_datadesc_ref("hetestruct[10]*", my_type);
664 gras_msgtype_declare("hetestruct[10]*", my_type);
666 my_type = gras_datadesc_struct("nestedstruct");
667 gras_datadesc_struct_append(my_type, "hete",
668 gras_datadesc_by_name("hetestruct"));
669 gras_datadesc_struct_append(my_type, "homo",
670 gras_datadesc_by_name("homostruct"));
671 gras_datadesc_struct_close(my_type);
673 gras_datadesc_ref("nestedstruct*",
674 gras_datadesc_by_name("nestedstruct"));
675 gras_msgtype_declare("nestedstruct*", my_type);
677 my_type = gras_datadesc_struct("chained_list_t");
678 ref_my_type = gras_datadesc_ref("chained_list_t*", my_type);
679 gras_datadesc_struct_append(my_type, "v", gras_datadesc_by_name("int"));
680 gras_datadesc_struct_append(my_type, "l", ref_my_type);
681 gras_datadesc_struct_close(my_type);
682 gras_datadesc_cycle_set(gras_datadesc_by_name("chained_list_t*"));
683 gras_msgtype_declare("chained_list_t", my_type);
684 gras_msgtype_declare("chained_list_t*", ref_my_type);
687 gras_datadesc_dynar(gras_datadesc_by_name("string"), &free_string);
688 gras_msgtype_declare("xbt_dynar_of_string", my_type);
690 my_type = gras_datadesc_by_symbol(s_pbio);
691 gras_msgtype_declare("s_pbio", my_type);
693 my_type = gras_datadesc_by_symbol(s_clause);
694 my_type = gras_datadesc_ref("Clause*", my_type);
695 gras_msgtype_declare("Clause*", my_type);
703 int main(int argc, char *argv[])
705 gras_socket_t sock = NULL;
706 int direction = WRITE;
708 char local_arch, remote_arch;
710 gras_init(&argc, argv);
712 register_structures();
714 for (cpt = 1; cpt < argc; cpt++) {
715 if (!strcmp(argv[cpt], "--arch")) {
716 XBT_INFO("We are on %s (#%d)",
717 gras_datadesc_arch_name(gras_arch_selfid()),
718 (int) gras_arch_selfid());
720 } else if (!strcmp(argv[cpt], "--help")) {
721 printf("Usage: datadesc_usage [option] [filename]\n");
722 printf(" --arch: display the current architecture and quit.\n");
723 printf(" --read file: reads the description from the given file\n");
724 printf(" --write file: writes the description to the given file\n");
725 printf(" --copy: copy the description in memory\n");
727 (" --regen: write the description to the file of the current architecture\n");
728 printf(" --help: displays this message\n");
730 } else if (!strcmp(argv[cpt], "--regen")) {
733 bprintf("datadesc.%s",
734 gras_datadesc_arch_name(gras_arch_selfid()));
735 } else if (!strcmp(argv[cpt], "--read")) {
737 } else if (!strcmp(argv[cpt], "--write")) {
739 } else if (!strcmp(argv[cpt], "--copy")) {
742 filename = argv[cpt];
746 if (direction == WRITE) {
747 XBT_INFO("Write to file %s",
748 strrchr(filename, '/') ? strrchr(filename, '/') + 1 : filename);
749 sock = gras_socket_client_from_file(filename);
751 if (direction == READ) {
752 XBT_INFO("Read from file %s",
753 strrchr(filename, '/') ? strrchr(filename, '/') + 1 : filename);
754 sock = gras_socket_server_from_file(filename);
756 if (direction == COPY) {
757 XBT_INFO("Memory copy");
760 local_arch = gras_arch_selfid();
761 write_read("char", &local_arch, &remote_arch, sock, direction);
762 if (direction == READ)
763 XBT_VERB("This file was generated on %s (%d)",
764 gras_datadesc_arch_name(remote_arch), (int) remote_arch);
767 test_int(sock, direction);
768 test_float(sock, direction);
769 test_double(sock, direction);
770 test_array(sock, direction);
771 test_intref(sock, direction);
773 test_string(sock, direction);
774 test_dynar_scal(sock, direction);
775 test_dynar_empty(sock, direction);
777 test_structures(sock, direction);
779 test_homostruct(sock, direction);
780 test_hetestruct(sock, direction);
781 test_nestedstruct(sock, direction);
782 test_hetestruct_array(sock, direction);
784 test_chain_list(sock, direction);
785 test_graph(sock, direction);
786 test_dynar_ref(sock, direction);
788 test_pbio(sock, direction);
790 test_clause(sock, direction);
791 test_clause_empty(sock, direction);
793 if (direction != COPY)
794 gras_socket_close(sock);