Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
- Reduce the number of system headers loaded, overload some more system
[simgrid.git] / testsuite / gras / datadesc_usage.c
index 253b03e..25e9d30 100644 (file)
@@ -11,8 +11,8 @@
 #include <stdio.h>
 #include <gras.h>
 
-#include "../DataDesc/datadesc_interface.h"
-GRAS_LOG_NEW_DEFAULT_CATEGORY(test);
+#include "gras/DataDesc/datadesc_interface.h"
+GRAS_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
 
 #define READ  0
 #define WRITE 1
@@ -65,6 +65,7 @@ gras_error_t test_chain_list(gras_socket_t *sock, int direction);
 gras_error_t test_graph(gras_socket_t *sock, int direction);
 
 gras_error_t test_pbio(gras_socket_t *sock, int direction);
+gras_error_t test_clause(gras_socket_t *sock, int direction);
 
 /* defined in datadesc_structures.c, which in perl generated */
 gras_error_t test_structures(gras_socket_t *sock, int direction); 
@@ -117,9 +118,9 @@ gras_error_t test_array(gras_socket_t *sock, int direction) {
 
   INFO0("---- Test on fixed array ----");
 
-  TRY(gras_datadesc_declare_array_fixed("fixed int array", 
-                                       gras_datadesc_by_name("int"),
-                                       SIZE, &my_type));
+  TRY(gras_datadesc_array_fixed("fixed int array", 
+                               gras_datadesc_by_name("int"),
+                               SIZE, &my_type));
 
   TRY(write_read(my_type, &i,&j, sock,direction));
   if (direction == READ || direction == RW) {
@@ -136,20 +137,20 @@ gras_error_t test_intref(gras_socket_t *sock, int direction) {
   gras_datadesc_type_t *my_type;
   int *i,*j;
   
-  if (! (i=malloc(sizeof(int))) )
+  if (! (i=gras_new(int,1)) )
     RAISE_MALLOC;
   *i=12345;
 
   INFO1("---- Test on a reference to an integer (%p) ----",i);
 
-  TRY(gras_datadesc_declare_ref("int*",gras_datadesc_by_name("int"),&my_type));
+  TRY(gras_datadesc_ref("int*",gras_datadesc_by_name("int"),&my_type));
 
   TRY(write_read(my_type, &i,&j, sock,direction));
   if (direction == READ || direction == RW) {
     gras_assert(*i == *j);
-    free(j);
+    gras_free(j);
   }
-  free(i);
+  gras_free(i);
   return no_error;
 }
 
@@ -169,9 +170,9 @@ gras_error_t test_string(gras_socket_t *sock, int direction) {
       gras_assert4(i[cpt] == j[cpt],"i[%d]=%c  !=  j[%d]=%c",
                   cpt,i[cpt],cpt,j[cpt]);
     } 
-    free(j);
+    gras_free(j);
   }
-  free(i);
+  gras_free(i);
   return no_error;
 }
 
@@ -189,22 +190,22 @@ gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
 
   INFO0("---- Test on homogeneous structure ----");
   /* create descriptor */
-  TRY(gras_datadesc_declare_struct("homostruct",&my_type));
-  TRY(gras_datadesc_declare_struct_append(my_type,"a",
-                                         gras_datadesc_by_name("signed int")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"b",
-                                         gras_datadesc_by_name("int")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"c",
-                                         gras_datadesc_by_name("int")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"d",
-                                         gras_datadesc_by_name("int")));
-  gras_datadesc_declare_struct_close(my_type);
-  TRY(gras_datadesc_declare_ref("homostruct*",
-                               gras_datadesc_by_name("homostruct"),
-                               &my_type));
+  TRY(gras_datadesc_struct("homostruct",&my_type));
+  TRY(gras_datadesc_struct_append(my_type,"a",
+                                 gras_datadesc_by_name("signed int")));
+  TRY(gras_datadesc_struct_append(my_type,"b",
+                                 gras_datadesc_by_name("int")));
+  TRY(gras_datadesc_struct_append(my_type,"c",
+                                 gras_datadesc_by_name("int")));
+  TRY(gras_datadesc_struct_append(my_type,"d",
+                                 gras_datadesc_by_name("int")));
+  gras_datadesc_struct_close(my_type);
+  TRY(gras_datadesc_ref("homostruct*",
+                       gras_datadesc_by_name("homostruct"),
+                       &my_type));
 
   /* init a value, exchange it and check its validity*/
-  if (! (i=malloc(sizeof(homostruct))) )
+  if (! (i=gras_new(homostruct,1)) )
     RAISE_MALLOC;
   i->a = 2235;    i->b = 433425;
   i->c = -23423;  i->d = -235235;
@@ -215,9 +216,9 @@ gras_error_t test_homostruct(gras_socket_t *sock, int direction) {
     gras_assert(i->b == j->b);
     gras_assert(i->c == j->c);
     gras_assert(i->d == j->d);
-    free(j);
+    gras_free(j);
   }
-  free(i);
+  gras_free(i);
   return no_error;
 }
 
@@ -237,22 +238,21 @@ gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
 
   INFO0("---- Test on heterogeneous structure ----");
   /* create descriptor */
-  TRY(gras_datadesc_declare_struct("hetestruct",&my_type));
-  TRY(gras_datadesc_declare_struct_append(my_type,"c1",
-                                         gras_datadesc_by_name("unsigned char")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"l1",
-                                         gras_datadesc_by_name("unsigned long int")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"c2",
-                                         gras_datadesc_by_name("unsigned char")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"l2",
-                                         gras_datadesc_by_name("unsigned long int")));
-  gras_datadesc_declare_struct_close(my_type);
-  TRY(gras_datadesc_declare_ref("hetestruct*",
-                               gras_datadesc_by_name("hetestruct"),
-                               &my_type));
+  TRY(gras_datadesc_struct("hetestruct",&my_type));
+  TRY(gras_datadesc_struct_append(my_type,"c1",
+                                 gras_datadesc_by_name("unsigned char")));
+  TRY(gras_datadesc_struct_append(my_type,"l1",
+                                 gras_datadesc_by_name("unsigned long int")));
+  TRY(gras_datadesc_struct_append(my_type,"c2",
+                                 gras_datadesc_by_name("unsigned char")));
+  TRY(gras_datadesc_struct_append(my_type,"l2",
+                                 gras_datadesc_by_name("unsigned long int")));
+  gras_datadesc_struct_close(my_type);
+  TRY(gras_datadesc_ref("hetestruct*", gras_datadesc_by_name("hetestruct"),
+                       &my_type));
 
   /* init a value, exchange it and check its validity*/
-  if (! (i=malloc(sizeof(hetestruct))) )
+  if (! (i=gras_new(hetestruct,1)) )
     RAISE_MALLOC;
   i->c1 = 's'; i->l1 = 123455;
   i->c2 = 'e'; i->l2 = 774531;
@@ -261,11 +261,11 @@ gras_error_t test_hetestruct(gras_socket_t *sock, int direction) {
   if (direction == READ || direction == RW) {
     gras_assert(i->c1 == j->c1);
     gras_assert(i->c2 == j->c2);
-    gras_assert2(i->l1 == j->l1,"i->l1(=%d)  !=  j->l1(=%d)",i->l1,j->l1);
+    gras_assert2(i->l1 == j->l1,"i->l1(=%ld)  !=  j->l1(=%ld)",i->l1,j->l1);
     gras_assert(i->l2 == j->l2);
-    free(j);
+    gras_free(j);
   }
-  free(i);
+  gras_free(i);
   return no_error;
 }
 
@@ -283,19 +283,18 @@ gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
 
   INFO0("---- Test on nested structures ----");
   /* create descriptor */
-  TRY(gras_datadesc_declare_struct("nestedstruct",&my_type));
+  TRY(gras_datadesc_struct("nestedstruct",&my_type));
 
-  TRY(gras_datadesc_declare_struct_append(my_type,"hete",
-                                         gras_datadesc_by_name("hetestruct")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"homo",
-                                         gras_datadesc_by_name("homostruct")));
-  gras_datadesc_declare_struct_close(my_type);
-  TRY(gras_datadesc_declare_ref("nestedstruct*",
-                               gras_datadesc_by_name("nestedstruct"),
-                               &my_type));
+  TRY(gras_datadesc_struct_append(my_type,"hete",
+                                 gras_datadesc_by_name("hetestruct")));
+  TRY(gras_datadesc_struct_append(my_type,"homo",
+                                 gras_datadesc_by_name("homostruct")));
+  gras_datadesc_struct_close(my_type);
+  TRY(gras_datadesc_ref("nestedstruct*", gras_datadesc_by_name("nestedstruct"),
+                       &my_type));
 
   /* init a value, exchange it and check its validity*/
-  if (! (i=malloc(sizeof(nestedstruct))) )
+  if (! (i=gras_new(nestedstruct,1)) )
     RAISE_MALLOC;
   i->homo.a = 235231;  i->homo.b = -124151;
   i->homo.c = 211551;  i->homo.d = -664222;
@@ -312,9 +311,9 @@ gras_error_t test_nestedstruct(gras_socket_t *sock, int direction) {
     gras_assert(i->hete.c2 == j->hete.c2);
     gras_assert(i->hete.l1 == j->hete.l1);
     gras_assert(i->hete.l2 == j->hete.l2);
-    free(j);
+    gras_free(j);
   }
-  free(i);
+  gras_free(i);
   return no_error;
 }
 
@@ -335,19 +334,18 @@ gras_error_t declare_chained_list_type(void) {
   gras_error_t errcode;
   gras_datadesc_type_t *my_type,*ref_my_type;
 
-  TRY(gras_datadesc_declare_struct("chained_list_t",&my_type));
-  TRY(gras_datadesc_declare_ref("chained_list_t*",my_type,&ref_my_type));
+  TRY(gras_datadesc_struct("chained_list_t",&my_type));
+  TRY(gras_datadesc_ref("chained_list_t*",my_type,&ref_my_type));
 
-  TRY(gras_datadesc_declare_struct_append(my_type,"v",
-                                         gras_datadesc_by_name("int")));
-  TRY(gras_datadesc_declare_struct_append(my_type,"l",ref_my_type));
-  gras_datadesc_declare_struct_close(my_type);
+  TRY(gras_datadesc_struct_append(my_type,"v", gras_datadesc_by_name("int")));
+  TRY(gras_datadesc_struct_append(my_type,"l", ref_my_type));
+  gras_datadesc_struct_close(my_type);
 
   return no_error;
 }
 
 chained_list_t * cons(int v, chained_list_t *l) {
-  chained_list_t *nl = malloc(sizeof (chained_list_t));
+  chained_list_t *nl = gras_new(chained_list_t,1);
   
   nl->v = v;
   nl->l = l;
@@ -357,7 +355,7 @@ chained_list_t * cons(int v, chained_list_t *l) {
 void list_free(chained_list_t*l) {
   if (l) {
     list_free(l->l);
-    free(l);
+    gras_free(l);
   }
 }
 int list_eq(chained_list_t*i,chained_list_t*j) {
@@ -522,6 +520,53 @@ gras_error_t test_pbio(gras_socket_t *sock, int direction) {
   return no_error;
 }
 
+GRAS_DEFINE_TYPE(s_clause,
+struct s_clause {
+   int num_lits;
+   int *literals GRAS_ANNOTE(size,num_lits); /* Tells GRAS where to find the size */
+};)
+typedef struct s_clause Clause;
+
+gras_error_t test_clause(gras_socket_t *sock, int direction) {
+  gras_error_t errcode;
+  gras_datadesc_type_t *ddt,*array_t;
+  Clause *i,*j;
+  int cpt;
+  
+  INFO0("---- Test on struct containing dynamic array and its size (cbps test) ----");
+
+  /* create and fill the struct */
+  if (! (i=gras_new(Clause,1)) )
+    RAISE_MALLOC;
+
+  i->num_lits = 5432;
+  if (! (i->literals = gras_new(int, i->num_lits)) )
+    RAISE_MALLOC;
+  for (cpt=0; cpt<i->num_lits; cpt++)
+    i->literals[cpt] = cpt * cpt - ((cpt * cpt) / 2);
+  DEBUG3("created data=%p (within %p @%p)",&(i->num_lits),i,&i);
+  DEBUG1("created count=%d",i->num_lits);
+
+  /* create the damn type descriptor */
+  ddt = gras_datadesc_by_symbol(s_clause);
+//  gras_datadesc_type_dump(ddt);
+
+  TRYFAIL(gras_datadesc_ref("Clause*",ddt,&ddt));
+
+  TRY(write_read(ddt, &i,&j, sock,direction));
+  if (direction == READ || direction == RW) {
+    gras_assert(i->num_lits == j->num_lits);
+    for (cpt=0; cpt<i->num_lits; cpt++)
+      gras_assert(i->literals[cpt] == j->literals[cpt]);
+    
+    gras_free(j->literals);
+    gras_free(j);
+  }
+  gras_free(i->literals);
+  gras_free(i);
+  return no_error;
+}
+
 int main(int argc,char *argv[]) {
   gras_error_t errcode;
   gras_socket_t *sock;
@@ -556,7 +601,6 @@ int main(int argc,char *argv[]) {
   }
   r_arch = (int)r_arch_char;
   
-  
   TRYFAIL(test_int(sock,direction));    
   TRYFAIL(test_float(sock,direction));  
   TRYFAIL(test_double(sock,direction));  
@@ -573,10 +617,12 @@ int main(int argc,char *argv[]) {
 
   TRYFAIL(declare_chained_list_type());
   TRYFAIL(test_chain_list(sock,direction));
-  TRYFAIL(test_graph(sock,direction));
+  //  TRYFAIL(test_graph(sock,direction));
 
   TRYFAIL(test_pbio(sock,direction));
 
+  TRYFAIL(test_clause(sock,direction));
+
   if (direction != RW) 
     gras_socket_close(sock);
   gras_exit();