Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Try harder to break that pile of crap when storing NULL
[simgrid.git] / testsuite / xbt / dict_usage.c
index 18d3450..6f2a23e 100644 (file)
@@ -2,35 +2,33 @@
 
 /* dict_usage - A test of normal usage of a dictionnary                     */
 
-/* Authors: Martin Quinson                                                  */
-/* Copyright (C) 2003 the OURAGAN project.                                  */
+/* Copyright (c) 2003,2004 Martin Quinson. All rights reserved.             */
 
 /* This program is free software; you can redistribute it and/or modify it
  under the terms of the license (GNU LGPL) which comes with this package. */
* under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include <stdio.h>
-#include <assert.h>
 
-#include <gras.h>
+#include "gras.h"
 
-GRAS_LOG_EXTERNAL_CATEGORY(dict);
-GRAS_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
+XBT_LOG_EXTERNAL_CATEGORY(dict);
+XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
 
-static void fill(gras_dict_t *head);
-static void debuged_add(gras_dict_t head,const char*key);
-static gras_error_t search(gras_dict_t head,const char*key);
-static gras_error_t debuged_remove(gras_dict_t head,const char*key);
-static gras_error_t traverse(gras_dict_t head);
+static void fill(xbt_dict_t *head);
+static void debuged_add(xbt_dict_t head,const char*key);
+static xbt_error_t search(xbt_dict_t head,const char*key);
+static xbt_error_t debuged_remove(xbt_dict_t head,const char*key);
+static xbt_error_t traverse(xbt_dict_t head);
 
 static void print_str(void *str);
 static void print_str(void *str) {
   printf("%s",(char*)str);
 }
 
-static void fill(gras_dict_t *head) {
+static void fill(xbt_dict_t *head) {
   printf("\n Fill in the dictionnary\n");
 
-  *head = gras_dict_new();
+  *head = xbt_dict_new();
   debuged_add(*head,"12");
   debuged_add(*head,"12a");
   debuged_add(*head,"12b");
@@ -43,60 +41,62 @@ static void fill(gras_dict_t *head) {
 
 }
 
-static void debuged_add(gras_dict_t head,const char*key)
+static void debuged_add(xbt_dict_t head,const char*key)
 {
-  char *data=gras_strdup(key);
+  char *data=xbt_strdup(key);
 
   printf("   - Add %s\n",key);
-  gras_dict_set(head,key,data,&free);
-  if (GRAS_LOG_ISENABLED(dict,gras_log_priority_debug)) {
-    gras_dict_dump(head,(void (*)(void*))&printf);
+  xbt_dict_set(head,key,data,&free);
+  if (XBT_LOG_ISENABLED(dict,xbt_log_priority_debug)) {
+    xbt_dict_dump(head,(void (*)(void*))&printf);
     fflush(stdout);
   }
 }
 
-static gras_error_t search(gras_dict_t head,const char*key) {
+static xbt_error_t search(xbt_dict_t head,const char*key) {
   void *data;
-  gras_error_t errcode;
+  xbt_error_t errcode;
 
   
-  errcode=gras_dict_get(head,key,&data);
-  printf("   - Search %s. Found %s\n",key,data?(char*)data:"(null)");fflush(stdout);
+  errcode=xbt_dict_get(head,key,&data);
+  printf("   - Search %s. Found %s\n",key,data?(char*)data:"NULL");fflush(stdout);
+  if (!data)
+     return errcode;
   if (strcmp((char*)data,key)) 
     return mismatch_error;
   return errcode;
 }
 
-static gras_error_t debuged_remove(gras_dict_t head,const char*key)
+static xbt_error_t debuged_remove(xbt_dict_t head,const char*key)
 {
-  gras_error_t errcode;
+  xbt_error_t errcode;
 
   printf("   Remove '%s'\n",key);fflush(stdout);
-  errcode=gras_dict_remove(head,key);
-  /*  gras_dict_dump(head,(void (*)(void*))&printf); */
+  errcode=xbt_dict_remove(head,key);
+  /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
   return errcode;
 }
 
 
-static gras_error_t traverse(gras_dict_t head) {
-  gras_dict_cursor_t cursor=NULL;
+static xbt_error_t traverse(xbt_dict_t head) {
+  xbt_dict_cursor_t cursor=NULL;
   char *key;
   char *data;
 
-  gras_dict_foreach(head,cursor,key,data) {
+  xbt_dict_foreach(head,cursor,key,data) {
     printf("   - Seen:  %s->%s\n",key,data);
-    gras_assert2(!strcmp(key,data),
+    xbt_assert2(!data || !strcmp(key,data),
                 "Key(%s) != value(%s). Abording\n",key,data);
   }
   return no_error;
 }
 
 int main(int argc,char **argv) {
-  gras_error_t errcode;
-  gras_dict_t head=NULL;
+  xbt_error_t errcode;
+  xbt_dict_t head=NULL;
   char *data;
 
-  gras_init_defaultlog(&argc,argv,"dict.thresh=verbose");
+  xbt_init_defaultlog(&argc,argv,"dict.thresh=verbose");
    
   printf("\nGeneric dictionnary: USAGE test:\n");
 
@@ -105,33 +105,53 @@ int main(int argc,char **argv) {
 
   fill(&head);
   printf(" Free the dictionnary (twice)\n");
-  gras_dict_free(&head);
-  gras_dict_free(&head);
+  xbt_dict_free(&head);
+  xbt_dict_free(&head);
   
   fill(&head);
 
+  /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
+  printf(" - Test that it works with NULL data\n");
+  printf("   - Store NULL under 'null'\n");fflush(stdout);
+  xbt_dict_set(head,"null",NULL,NULL);
+  TRYFAIL(search(head,"null"));
+  /* xbt_dict_dump(head,(void (*)(void*))&printf); */
+  printf("   Check whether I see it while traversing\n");fflush(stdout);
+  {
+     xbt_dict_cursor_t cursor=NULL;
+     char *key;
+     int found=0;
+     
+     xbt_dict_foreach(head,cursor,key,data) {
+       printf("   - Seen:  %s->%s\n",key,data);fflush(stdout);
+       if (!strcmp(key,"null"))
+         found = 1;
+     }
+     xbt_assert0(found,"the key 'null', associated to NULL is not found");
+  }
+   
   printf(" - Change some values\n");
   printf("   - Change 123 to 'Changed 123'\n");
-  gras_dict_set(head,"123",strdup("Changed 123"),&free);
+  xbt_dict_set(head,"123",strdup("Changed 123"),&free);
   printf("   - Change 123 back to '123'\n");
-  gras_dict_set(head,"123",strdup("123"),&free);
+  xbt_dict_set(head,"123",strdup("123"),&free);
   printf("   - Change 12a to 'Dummy 12a'\n");
-  gras_dict_set(head,"12a",strdup("Dummy 12a"),&free);
+  xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
   printf("   - Change 12a to '12a'\n");
-  gras_dict_set(head,"12a",strdup("12a"),&free);
+  xbt_dict_set(head,"12a",strdup("12a"),&free);
 
-  /*  gras_dict_dump(head,(void (*)(void*))&printf); */
+  /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
   printf(" - Traverse the resulting dictionnary\n");
   TRYFAIL(traverse(head));
 
   printf(" - Retrive values\n");
-  TRYFAIL(gras_dict_get(head,"123",(void**)&data));
-  assert(data);
+  TRYFAIL(xbt_dict_get(head,"123",(void**)&data));
+  xbt_assert(data);
   TRYFAIL(strcmp("123",data));
 
-  TRYEXPECT(gras_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
-  TRYEXPECT(gras_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
-  TRYEXPECT(gras_dict_get(head,"12345678 NOT",(void**)&data),mismatch_error);
+  TRYEXPECT(xbt_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
+  TRYEXPECT(xbt_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
+  TRYEXPECT(xbt_dict_get(head,"12345678 NOT",(void**)&data),mismatch_error);
 
   TRYFAIL(search(head,"12a"));
   TRYFAIL(search(head,"12b"));
@@ -143,11 +163,11 @@ int main(int argc,char **argv) {
   printf(" - Traverse the resulting dictionnary\n");
   TRYFAIL(traverse(head));
 
-  /*  gras_dict_dump(head,(void (*)(void*))&printf); */
+  /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
 
   printf(" Free the dictionnary twice\n");
-  gras_dict_free(&head);
-  gras_dict_free(&head);
+  xbt_dict_free(&head);
+  xbt_dict_free(&head);
 
   printf(" - Traverse the resulting dictionnary\n");
   TRYFAIL(traverse(head));
@@ -158,7 +178,7 @@ int main(int argc,char **argv) {
   TRYEXPECT(debuged_remove(head,"Does not exist"),mismatch_error);
   TRYFAIL(traverse(head));
 
-  gras_dict_free(&head);
+  xbt_dict_free(&head);
 
   printf(" - Remove data from the NULL dict (error message expected)\n");
   TRYCATCH(debuged_remove(head,"12345"),mismatch_error);
@@ -176,8 +196,8 @@ int main(int argc,char **argv) {
   TRYEXPECT(debuged_remove(head,"12346"),mismatch_error);  TRYFAIL(traverse(head));
   
   printf(" - Free the dictionnary twice\n");
-  gras_dict_free(&head);
-  gras_dict_free(&head);
-  gras_exit();
+  xbt_dict_free(&head);
+  xbt_dict_free(&head);
+  xbt_exit();
   return 0;
 }