diff options
author | Stef Walter <stefw@gnome.org> | 2013-03-20 09:33:04 +0100 |
---|---|---|
committer | Stef Walter <stefw@gnome.org> | 2013-03-20 10:54:00 +0100 |
commit | f45942a4fc3e1c5219e9b5201b82203337ee7280 (patch) | |
tree | f83313676d1c8de9dbc48d161e16c13264bc8049 /common/tests | |
parent | 1dc227b4fce16fcc721276925492f4ba4db00b4f (diff) |
hash: Add the murmur2 hash and start using it
Add implementation of the murmur2 hash function, and start using
it for our dictionaries. Our implementation is incremental
like our other hash functions.
Also remove p11_oid_hash() which wasn't being used.
In addition fix several tests whose success was based on the
way that the dictionary hashed. This was a hidden testing bug.
Diffstat (limited to 'common/tests')
-rw-r--r-- | common/tests/test-hash.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/common/tests/test-hash.c b/common/tests/test-hash.c index 5e32c85..f57988e 100644 --- a/common/tests/test-hash.c +++ b/common/tests/test-hash.c @@ -35,6 +35,8 @@ #include "config.h" #include "CuTest.h" +#include <assert.h> +#include <stdint.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -128,6 +130,73 @@ test_md5 (CuTest *cu) } } +static void +test_murmur2 (CuTest *cu) +{ + struct { + const char *input; + const char *input2; + int hash; + } fixtures[] = { + { "one", NULL, 1910179066 }, + { "two", NULL, 396151652 }, + { "four", NULL, -2034170174 }, + { "seven", NULL, -588341181 }, + /* Note that these are identical output */ + { "eleven", NULL, -37856894 }, + { "ele", "ven", -37856894 }, + { NULL }, + }; + + uint32_t first; + uint32_t second; + int i; + + assert (sizeof (first) == P11_HASH_MURMUR2_LEN); + for (i = 0; fixtures[i].input != NULL; i++) { + p11_hash_murmur2 ((unsigned char *)&first, + fixtures[i].input, + strlen (fixtures[i].input), + fixtures[i].input2, + fixtures[i].input2 ? strlen (fixtures[i].input2) : 0, + NULL); + + p11_hash_murmur2 ((unsigned char *)&second, + fixtures[i].input, + strlen (fixtures[i].input), + fixtures[i].input2, + fixtures[i].input2 ? strlen (fixtures[i].input2) : 0, + NULL); + + CuAssertIntEquals (cu, fixtures[i].hash, first); + CuAssertIntEquals (cu, fixtures[i].hash, second); + } +} + +static void +test_murmur2_incr (CuTest *cu) +{ + uint32_t first, second; + + p11_hash_murmur2 ((unsigned char *)&first, + "this is the long input!", 23, + NULL); + + p11_hash_murmur2 ((unsigned char *)&second, + "this", 4, + " ", 1, + "is ", 3, + "the long ", 9, + "in", 2, + "p", 1, + "u", 1, + "t", 1, + "!", 1, + NULL); + + CuAssertIntEquals (cu, first, second); +} + int main (void) { @@ -138,6 +207,8 @@ main (void) SUITE_ADD_TEST (suite, test_sha1); SUITE_ADD_TEST (suite, test_sha1_long); SUITE_ADD_TEST (suite, test_md5); + SUITE_ADD_TEST (suite, test_murmur2); + SUITE_ADD_TEST (suite, test_murmur2_incr); CuSuiteRun (suite); CuSuiteSummary (suite, output); |