/**
* Returns the hash code of a string.
*/
-unsigned int xbt_dict_hash(const char *str) {
+static unsigned int xbt_dict_hash(const char *str, int str_len) {
/* fast implementation of djb2 algorithm */
unsigned int hash = 5381;
int c;
- while ((c = *str++)) {
+ while (str_len--) {
+ c = *str++;
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
void_f_pvoid_t *free_ctn) {
xbt_assert(dict);
- unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
+ unsigned int hash_code = xbt_dict_hash(key,key_len) % dict->table_size;
xbt_dictelm_t current, previous = NULL;
current = dict->table[hash_code];
int key_len) {
xbt_assert(dict);
- unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
+ unsigned int hash_code = xbt_dict_hash(key,key_len) % dict->table_size;
xbt_dictelm_t current;
current = dict->table[hash_code];
int key_len) {
xbt_assert(dict);
- unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
+ unsigned int hash_code = xbt_dict_hash(key,key_len) % dict->table_size;
xbt_dictelm_t current, previous = NULL;
current = dict->table[hash_code];
void xbt_dict_add_element(xbt_dict_t dict, xbt_dictelm_t element) {
xbt_assert(dict);
- int hashcode = xbt_dict_hash(element->key) % dict->table_size;
+ int hashcode = xbt_dict_hash(element->key,element->key_len) % dict->table_size;
element->next = dict->table[hashcode];
dict->table[hashcode] = element;
}
printf("%s",(char*)PRINTF_STR(str));
}
-static void debuged_add(xbt_dict_t head,const char*key)
-{
- char *data=xbt_strdup(key);
+static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
+ char *data=xbt_strdup(data_to_fill);
- xbt_test_log1("Add %s",PRINTF_STR(key));
+ xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
xbt_dict_set(head,key,data,&free);
if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
fflush(stdout);
}
}
+static void debuged_add(xbt_dict_t head,const char*key) {
+ debuged_add_ext(head,key,key);
+}
static void fill(xbt_dict_t *head) {
xbt_test_add0("Fill in the dictionnary");
debuged_add(*head,"123457");
}
-static void search(xbt_dict_t head,const char*key) {
- void *data;
+
+static void search_ext(xbt_dict_t head,const char*key, const char *data) {
+ void *found;
xbt_test_add1("Search %s",key);
- data=xbt_dict_get(head,key);
- xbt_test_log1("Found %s",(char *)data);
+ found=xbt_dict_get(head,key);
+ xbt_test_log1("Found %s",(char *)found);
if (data)
- xbt_test_assert0(!strcmp((char*)data,key),"Key and data do not match");
+ xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
+ if (found)
+ xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
+}
+
+static void search(xbt_dict_t head,const char*key) {
+ search_ext(head,key,key);
}
static void debuged_remove(xbt_dict_t head,const char*key) {
}
static void search_not_found(xbt_dict_t head, const char *data) {
+ int ok=0;
xbt_ex_t e;
xbt_test_add1("Search %s (expected not to be found)",data);
TRY {
- data = xbt_dict_get(head,"Can't be found");
+ data = xbt_dict_get(head, data);
THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
} CATCH(e) {
if (e.category != not_found_error)
xbt_test_exception(e);
xbt_ex_free(e);
+ ok=1;
}
+ xbt_test_assert0(ok,"Exception not raised");
}
static void count(xbt_dict_t dict, int length) {
xbt_test_add0("Traverse the full dictionnary");
fill(&head);
count(head, 7);
+
+ debuged_add_ext(head,"toto","tutu");
+ search_ext(head,"toto","tutu");
+ debuged_remove(head,"toto");
search(head,"12a");
traverse(head);
xbt_test_add0("Store NULL under 'null'");
xbt_dict_set(head,"null",NULL,NULL);
- search(head,"null");
+ search_ext(head,"null",NULL);
xbt_test_add0("Check whether I see it while traversing...");
{