ref: d789157f513b2c3f324e00401f7e72e6010b6211
dir: /test.c/
#include "hash.c"
typedef struct Tnode {
Hnode;
char *key;
char *value;
} Tnode;
int
tfmt(Fmt *f)
{
Tnode *t;
t = va_arg(f->args, Tnode*);
return fmtprint(f, "%s %s\n", t->key, t->value);
}
uvlong
thash(void *n)
{
Tnode *t;
uvlong hash;
char *s;
t = n;
hash = 7;
s = t->key;
for(; *s; s++)
hash = hash*31 + *s;
return hash;
}
int
tcmp(void *_a, void *_b)
{
Tnode *a, *b;
a = _a;
b = _b;
return strcmp(a->key, b->key);
}
void
main(int argc, char **argv)
{
int i;
Hmap *h;
Tnode t, *r;
Tnode tab[] = {
{.key "key1", .value "value1" },
{.key "key2", .value "value2" },
{.key "key3", .value "value3" },
{.key "key4", .value "value4" },
{.key "key5", .value "value5" },
{.key "key6", .value "value6" },
};
fmtinstall('T', tfmt);
fmtinstall('H', hmapfmt);
h = allochmap(thash, tcmp, 2, sizeof(Tnode));
h->nodeverb = 'T';
for(i=0; i < nelem(tab); i++)
hmapset(&h, &tab[i]);
for(i=0; i < nelem(tab); i++){
t = tab[i];
t.value = "";
r = hmapget(h, &t);
assert(r);
assert(strcmp(r->key, tab[i].key) == 0);
assert(strcmp(r->value, tab[i].value) == 0);
}
print("%H", h);
print("len, cap: %d %d\n", h->len, h->cap);
h = hmaprehash(h, 0);
for(i=0; i < nelem(tab); i++){
t = tab[i];
t.value = "";
r = hmapget(h, &t);
assert(r);
assert(strcmp(r->key, tab[i].key) == 0);
assert(strcmp(r->value, tab[i].value) == 0);
}
print("len, cap: %d %d\n", h->len, h->cap);
}