Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More extensive unit test.

  • Loading branch information...
commit 6d1c6c352c705bd8dd565613cf35e472c472eca3 1 parent 756fe68
@zedshaw authored
Showing with 93 additions and 9 deletions.
  1. +93 −9 tests/hashmap_algos_tests.c
View
102 tests/hashmap_algos_tests.c
@@ -1,5 +1,7 @@
#include <lcthw/bstrlib.h>
+#include <lcthw/hashmap.h>
#include <lcthw/hashmap_algos.h>
+#include <lcthw/darray.h>
#include "minunit.h"
struct tagbstring test1 = bsStatic("test data 1");
@@ -9,13 +11,13 @@ struct tagbstring test3 = bsStatic("xest data 3");
char *test_fnv1a()
{
uint32_t hash = Hashmap_fnv1a_hash(&test1);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
hash = Hashmap_fnv1a_hash(&test2);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
hash = Hashmap_fnv1a_hash(&test3);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
return NULL;
}
@@ -23,13 +25,13 @@ char *test_fnv1a()
char *test_adler32()
{
uint32_t hash = Hashmap_adler32_hash(&test1);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
hash = Hashmap_adler32_hash(&test2);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
hash = Hashmap_adler32_hash(&test3);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
return NULL;
}
@@ -37,13 +39,94 @@ char *test_adler32()
char *test_djb()
{
uint32_t hash = Hashmap_djb_hash(&test1);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
hash = Hashmap_djb_hash(&test2);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
hash = Hashmap_djb_hash(&test3);
- debug("HASH: %u", hash);
+ mu_assert(hash != 0, "Bad hash.");
+
+ return NULL;
+}
+
+#define BUCKETS 100
+#define BUFFER_LEN 20
+#define NUM_KEYS BUCKETS * 1000
+enum { ALGO_FNV1A, ALGO_ADLER32, ALGO_DJB};
+
+int gen_keys(DArray *keys, int num_keys)
+{
+ int i = 0;
+ FILE *urand = fopen("/dev/urandom", "r");
+ check(urand != NULL, "Failed to open /dev/urandom");
+
+ struct bStream *stream = bsopen((bNread)fread, urand);
+ check(stream != NULL, "Failed to open /dev/urandom");
+
+ bstring key = bfromcstr("");
+ int rc = 0;
+
+ // FNV1a histogram
+ for(i = 0; i < num_keys; i++) {
+ rc = bsread(key, stream, BUFFER_LEN);
+ check(rc >= 0, "Failed to read from /dev/urandom.");
+
+ DArray_push(keys, bstrcpy(key));
+ }
+
+ bsclose(stream);
+ fclose(urand);
+ return 0;
+
+error:
+ return -1;
+}
+
+void destroy_keys(DArray *keys)
+{
+ int i = 0;
+ for(i = 0; i < NUM_KEYS; i++) {
+ bdestroy(DArray_get(keys, i));
+ }
+
+ DArray_destroy(keys);
+}
+
+void fill_distribution(int *stats, DArray *keys, Hashmap_hash hash_func)
+{
+ int i = 0;
+ uint32_t hash = 0;
+
+ for(i = 0; i < DArray_count(keys); i++) {
+ hash = hash_func(DArray_get(keys, i));
+ stats[hash % BUCKETS] += 1;
+ }
+
+}
+
+char *test_distribution()
+{
+ int i = 0;
+ int stats[3][BUCKETS] = {{0}};
+ DArray *keys = DArray_create(0, NUM_KEYS);
+
+ mu_assert(gen_keys(keys, NUM_KEYS) == 0, "Failed to generate random keys.");
+
+ fill_distribution(stats[ALGO_FNV1A], keys, Hashmap_fnv1a_hash);
+ fill_distribution(stats[ALGO_ADLER32], keys, Hashmap_adler32_hash);
+ fill_distribution(stats[ALGO_DJB], keys, Hashmap_djb_hash);
+
+ fprintf(stderr, "FNV\tA32\tDJB\n");
+
+ for(i = 0; i < BUCKETS; i++) {
+ fprintf(stderr, "%d\t%d\t%d\n",
+ stats[ALGO_FNV1A][i],
+ stats[ALGO_ADLER32][i],
+ stats[ALGO_DJB][i]);
+ }
+
+ destroy_keys(keys);
return NULL;
}
@@ -55,6 +138,7 @@ char *all_tests()
mu_run_test(test_fnv1a);
mu_run_test(test_adler32);
mu_run_test(test_djb);
+ mu_run_test(test_distribution);
return NULL;
}
Please sign in to comment.
Something went wrong with that request. Please try again.