On Sun, 2015-03-22 at 22:03 +0000, Emil Velikov wrote:
Signed-off-by: Emil Velikov emil.l.velikov@gmail.com
tests/hash.c | 102 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 42 deletions(-)
diff --git a/tests/hash.c b/tests/hash.c index 902919f..fa9264a 100644 --- a/tests/hash.c +++ b/tests/hash.c @@ -73,8 +73,8 @@
#include "xf86drm.h"
-#define HASH_SIZE 512 /* Good for about 100 entries */
/* If you change this value, you probably
+#define HASH_SIZE 512 /* Good for about 100 entries */
/* If you change this value, you probably have to change the HashHash hashing function! */
@@ -87,9 +87,9 @@ typedef struct HashBucket { typedef struct HashTable { unsigned long magic; unsigned long entries;
- unsigned long hits; /* At top of linked list */
- unsigned long partials; /* Not at top of linked list */
- unsigned long misses; /* Not in table */
- unsigned long hits; /* At top of linked list */
- unsigned long partials; /* Not at top of linked list */
- unsigned long misses; /* Not in table */ HashBucketPtr buckets[HASH_SIZE]; int p0; HashBucketPtr p1;
@@ -101,21 +101,25 @@ static int dist[DIST_LIMIT]; static void clear_dist(void) { int i;
- for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
- for (i = 0; i < DIST_LIMIT; i++)
dist[i] = 0;
}
static int count_entries(HashBucketPtr bucket) {
- int count = 0;
- int count;
- for (; bucket; bucket = bucket->next) ++count;
- for (count = 0; bucket; bucket = bucket->next)
++count;
I personally prefer to initialize early, especially since it's not for-loop iterating variable, but I don't insist.
Reviewed-by: Jan Vesely jan.vesely@rutgers.edu
return count;
}
static void update_dist(int count) {
- if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
- else ++dist[count];
- if (count >= DIST_LIMIT)
++dist[DIST_LIMIT-1];
- else
++dist[count];
}
static void compute_dist(HashTablePtr table) @@ -124,43 +128,45 @@ static void compute_dist(HashTablePtr table) HashBucketPtr bucket;
printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
table->entries, table->hits, table->partials, table->misses);
clear_dist(); for (i = 0; i < HASH_SIZE; i++) {table->entries, table->hits, table->partials, table->misses);
- bucket = table->buckets[i];
- update_dist(count_entries(bucket));
bucket = table->buckets[i];
} for (i = 0; i < DIST_LIMIT; i++) {update_dist(count_entries(bucket));
- if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
- else printf("other %10d\n", dist[i]);
if (i != DIST_LIMIT-1)
printf("%5d %10d\n", i, dist[i]);
else
}printf("other %10d\n", dist[i]);
}
static void check_table(HashTablePtr table,
unsigned long key, unsigned long value)
unsigned long key, unsigned long value)
{ unsigned long *retval; int retcode = drmHashLookup(table, key, (void **)&retval);
switch (retcode) { case -1:
- printf("Bad magic = 0x%08lx:"
" key = %lu, expected = %lu, returned = %lu\n",
table->magic, key, value, *retval);
- break;
printf("Bad magic = 0x%08lx:"
" key = %lu, expected = %lu, returned = %lu\n",
table->magic, key, value, *retval);
case 1:break;
- printf("Not found: key = %lu, expected = %lu, returned = %lu\n",
key, value, *retval);
- break;
printf("Not found: key = %lu, expected = %lu, returned = %lu\n",
key, value, *retval);
case 0:break;
- if (value != *retval)
printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
key, value, *retval);
- break;
if (value != *retval)
printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
key, value, *retval);
default:break;
- printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
retcode, key, value, *retval);
- break;
printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
retcode, key, value, *retval);
}break;
}
@@ -171,44 +177,56 @@ int main(void)
printf("\n***** 256 consecutive integers ****\n"); table = drmHashCreate();
- for (i = 0; i < 256; i++) drmHashInsert(table, i, (void *)&i);
- for (i = 0; i < 256; i++) check_table(table, i, i);
for (i = 0; i < 256; i++)
drmHashInsert(table, i, (void *)&i);
for (i = 0; i < 256; i++)
check_table(table, i, i);
compute_dist(table); drmHashDestroy(table);
printf("\n***** 1024 consecutive integers ****\n"); table = drmHashCreate();
- for (i = 0; i < 1024; i++) drmHashInsert(table, i, (void *)&i);
- for (i = 0; i < 1024; i++) check_table(table, i, i);
for (i = 0; i < 1024; i++)
drmHashInsert(table, i, (void *)&i);
for (i = 0; i < 1024; i++)
check_table(table, i, i);
compute_dist(table); drmHashDestroy(table);
printf("\n***** 1024 consecutive page addresses (4k pages) ****\n"); table = drmHashCreate();
- for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, (void *)&i);
- for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
for (i = 0; i < 1024; i++)
drmHashInsert(table, i*4096, (void *)&i);
for (i = 0; i < 1024; i++)
check_table(table, i*4096, i);
compute_dist(table); drmHashDestroy(table);
printf("\n***** 1024 random integers ****\n"); table = drmHashCreate(); srandom(0xbeefbeef);
- for (i = 0; i < 1024; i++) drmHashInsert(table, random(), (void *)&i);
- for (i = 0; i < 1024; i++)
srandom(0xbeefbeef);drmHashInsert(table, random(), (void *)&i);
- for (i = 0; i < 1024; i++) check_table(table, random(), i);
- for (i = 0; i < 1024; i++)
srandom(0xbeefbeef);check_table(table, random(), i);
- for (i = 0; i < 1024; i++) check_table(table, random(), i);
for (i = 0; i < 1024; i++)
check_table(table, random(), i);
compute_dist(table); drmHashDestroy(table);
printf("\n***** 5000 random integers ****\n"); table = drmHashCreate(); srandom(0xbeefbeef);
- for (i = 0; i < 5000; i++) drmHashInsert(table, random(), (void *)&i);
- for (i = 0; i < 5000; i++)
srandom(0xbeefbeef);drmHashInsert(table, random(), (void *)&i);
- for (i = 0; i < 5000; i++) check_table(table, random(), i);
- for (i = 0; i < 5000; i++)
srandom(0xbeefbeef);check_table(table, random(), i);
- for (i = 0; i < 5000; i++) check_table(table, random(), i);
- for (i = 0; i < 5000; i++)
compute_dist(table); drmHashDestroy(table);check_table(table, random(), i);