-#include "cache.h"
+#include "git-compat-util.h"
#include "hashmap.h"
-#include <stdio.h>
struct test_entry
{
static unsigned int hash(unsigned int method, unsigned int i, const char *key)
{
- unsigned int hash;
+ unsigned int hash = 0;
switch (method & 3)
{
case HASH_METHOD_FNV:
for (j = 0; j < rounds; j++) {
for (i = 0; i < TEST_SIZE; i++) {
- struct hashmap_entry key;
- hashmap_entry_init(&key, hashes[i]);
- hashmap_get(&map, &key, entries[i]->key);
+ hashmap_get_from_hash(&map, hashes[i],
+ entries[i]->key);
}
}
}
}
-struct hash_entry
-{
- struct hash_entry *next;
- char key[FLEX_ARRAY];
-};
-
-/*
- * Test performance of hash.[ch]
- * Usage: time echo "perfhash method rounds" | test-hashmap
- */
-static void perf_hash(unsigned int method, unsigned int rounds)
-{
- struct hash_table map;
- char buf[16];
- struct hash_entry **entries, **res, *entry;
- unsigned int *hashes;
- unsigned int i, j;
-
- entries = malloc(TEST_SIZE * sizeof(struct hash_entry *));
- hashes = malloc(TEST_SIZE * sizeof(int));
- for (i = 0; i < TEST_SIZE; i++) {
- snprintf(buf, sizeof(buf), "%i", i);
- entries[i] = malloc(sizeof(struct hash_entry) + strlen(buf) + 1);
- strcpy(entries[i]->key, buf);
- hashes[i] = hash(method, i, entries[i]->key);
- }
-
- if (method & TEST_ADD) {
- /* test adding to the map */
- for (j = 0; j < rounds; j++) {
- init_hash(&map);
-
- /* add entries */
- for (i = 0; i < TEST_SIZE; i++) {
- res = (struct hash_entry **) insert_hash(
- hashes[i], entries[i], &map);
- if (res) {
- entries[i]->next = *res;
- *res = entries[i];
- } else {
- entries[i]->next = NULL;
- }
- }
-
- free_hash(&map);
- }
- } else {
- /* test map lookups */
- init_hash(&map);
-
- /* fill the map (sparsely if specified) */
- j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
- for (i = 0; i < j; i++) {
- res = (struct hash_entry **) insert_hash(hashes[i],
- entries[i], &map);
- if (res) {
- entries[i]->next = *res;
- *res = entries[i];
- } else {
- entries[i]->next = NULL;
- }
- }
-
- for (j = 0; j < rounds; j++) {
- for (i = 0; i < TEST_SIZE; i++) {
- entry = lookup_hash(hashes[i], &map);
- while (entry) {
- if (!strcmp(entries[i]->key, entry->key))
- break;
- entry = entry->next;
- }
- }
- }
-
- free_hash(&map);
-
- }
-}
-
#define DELIM " \t\r\n"
/*
* size -> tablesize numentries
*
* perfhashmap method rounds -> test hashmap.[ch] performance
- * perfhash method rounds -> test hash.[ch] performance
*/
int main(int argc, char *argv[])
{
} else if (!strcmp("get", cmd) && l1) {
- /* setup static key */
- struct hashmap_entry key;
- hashmap_entry_init(&key, hash);
-
/* lookup entry in hashmap */
- entry = hashmap_get(&map, &key, p1);
+ entry = hashmap_get_from_hash(&map, hash, p1);
/* print result */
if (!entry)
/* print table sizes */
printf("%u %u\n", map.tablesize, map.size);
- } else if (!strcmp("perfhashmap", cmd) && l1 && l2) {
+ } else if (!strcmp("intern", cmd) && l1) {
- perf_hashmap(atoi(p1), atoi(p2));
+ /* test that strintern works */
+ const char *i1 = strintern(p1);
+ const char *i2 = strintern(p1);
+ if (strcmp(i1, p1))
+ printf("strintern(%s) returns %s\n", p1, i1);
+ else if (i1 == p1)
+ printf("strintern(%s) returns input pointer\n", p1);
+ else if (i1 != i2)
+ printf("strintern(%s) != strintern(%s)", i1, i2);
+ else
+ printf("%s\n", i1);
- } else if (!strcmp("perfhash", cmd) && l1 && l2) {
+ } else if (!strcmp("perfhashmap", cmd) && l1 && l2) {
- perf_hash(atoi(p1), atoi(p2));
+ perf_hashmap(atoi(p1), atoi(p2));
} else {