From 8df169fdffb564ec932fede4216a123a71f1cc9a Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 9 Jan 2012 14:18:12 -0500 Subject: Add a random + identity test for murmurhash3 This test always generate a random string so each time the test is run we will test the hash function with a new value. It also hashes the same string twice and compares the result so that we have a chance of catching if uninitialized variables are getting mixed into the value calculation and end up generating different results for the same input. --- src/tests/util-tests.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/tests') diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c index 557be10e..3dc6ae66 100644 --- a/src/tests/util-tests.c +++ b/src/tests/util-tests.c @@ -417,6 +417,34 @@ START_TEST(test_murmurhash3_check) } END_TEST +START_TEST(test_murmurhash3_random) +{ + char test[16]; + uint32_t result1; + uint32_t result2; + unsigned int init_seed; + unsigned int seed; + size_t len; + int i; + + /* generate a random string so each time we test with different values */ + init_seed = time(0); + seed = init_seed; + /* use also random length (min len = 1) */ + len = 1 + rand_r(&seed) % 14; + for (i = 0; i < len; i++) { + test[i] = 1 + rand_r(&seed) % 254; + } + test[len] = '\0'; /* null terminate */ + + fprintf(stdout, "test_murmurhash3_random seed: %d\n", init_seed); + + result1 = murmurhash3(test, len + 1, init_seed); + result2 = murmurhash3(test, len + 1, init_seed); + fail_if(result1 != result2); +} +END_TEST + Suite *util_suite(void) { Suite *s = suite_create("util"); @@ -440,6 +468,7 @@ Suite *util_suite(void) TCase *tc_mh3 = tcase_create("murmurhash3"); tcase_add_test (tc_mh3, test_murmurhash3_check); + tcase_add_test (tc_mh3, test_murmurhash3_random); tcase_set_timeout(tc_mh3, 60); suite_add_tcase (s, tc_util); -- cgit