Hash :
3315782c
Author :
Date :
2010-08-08T14:12:17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include "test_lib.h"
#include "test_helpers.h"
#include "commit.h"
#include "hashtable.h"
#include "hash.h"
typedef struct _aux_object {
int __bulk;
git_oid id;
int visited;
} table_item;
uint32_t hash_func(const void *key)
{
uint32_t r;
git_oid *id;
id = (git_oid *)key;
memcpy(&r, id->id, sizeof(r));
return r;
}
int hash_haskey(void *item, const void *key)
{
table_item *obj;
git_oid *oid;
obj = (table_item *)item;
oid = (git_oid *)key;
return (git_oid_cmp(oid, &obj->id) == 0);
}
BEGIN_TEST(table_iterator)
const int objects_n = 32;
int i;
table_item *objects, *ob;
git_hashtable *table = NULL;
git_hashtable_iterator iterator;
table = git_hashtable_alloc(objects_n * 2, hash_func, hash_haskey);
must_be_true(table != NULL);
objects = git__malloc(objects_n * sizeof(table_item));
memset(objects, 0x0, objects_n * sizeof(table_item));
/* populate the hash table */
for (i = 0; i < objects_n; ++i) {
git_hash_buf(&(objects[i].id), &i, sizeof(int));
must_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
}
git_hashtable_iterator_init(table, &iterator);
/* iterate through all nodes, mark as visited */
while ((ob = (table_item *)git_hashtable_iterator_next(&iterator)) != NULL)
ob->visited = 1;
/* make sure all nodes have been visited */
for (i = 0; i < objects_n; ++i)
must_be_true(objects[i].visited);
git_hashtable_free(table);
free(objects);
END_TEST