X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhash%2Ftest%2Frun.c;h=31d4ecb59a6ab7c82776f0576994d06b28c79450;hb=e9c5dc689f2bfdc9276588a61a775f2648e77e7b;hp=1857739af5931f7044d2b0a4195ffb3ba1e333a3;hpb=41069549813a04a2875063cb12e8057f5d54d7f1;p=ccan diff --git a/ccan/hash/test/run.c b/ccan/hash/test/run.c index 1857739a..31d4ecb5 100644 --- a/ccan/hash/test/run.c +++ b/ccan/hash/test/run.c @@ -1,6 +1,6 @@ -#include "hash/hash.h" -#include "tap/tap.h" -#include "hash/hash.c" +#include +#include +#include #include #include @@ -17,7 +17,7 @@ int main(int argc, char *argv[]) for (i = 0; i < ARRAY_WORDS; i++) array[i] = i; - plan_tests(22); + plan_tests(39); /* Hash should be the same, indep of memory alignment. */ val = hash(array, sizeof(array), 0); for (i = 0; i < sizeof(uint32_t); i++) { @@ -51,6 +51,31 @@ int main(int argc, char *argv[]) diag("Byte %i, range %u-%u", i, lowest, highest); } + /* Hash of random values should have random distribution: + * check one byte at a time. */ + for (i = 0; i < sizeof(uint64_t); i++) { + unsigned int lowest = -1U, highest = 0; + + memset(results, 0, sizeof(results)); + + for (j = 0; j < 256000; j++) { + for (k = 0; k < ARRAY_WORDS; k++) + array[k] = random(); + results[(hash64(array, sizeof(array), 0) >> i*8)&0xFF]++; + } + + for (j = 0; j < 256; j++) { + if (results[j] < lowest) + lowest = results[j]; + if (results[j] > highest) + highest = results[j]; + } + /* Expect within 20% */ + ok(lowest > 800, "Byte %i lowest %i", i, lowest); + ok(highest < 1200, "Byte %i highest %i", i, highest); + diag("Byte %i, range %u-%u", i, lowest, highest); + } + /* Hash of pointer values should also have random distribution. */ for (i = 0; i < sizeof(uint32_t); i++) { unsigned int lowest = -1U, highest = 0; @@ -75,6 +100,13 @@ int main(int argc, char *argv[]) diag("hash_pointer byte %i, range %u-%u", i, lowest, highest); } + if (sizeof(long) == sizeof(uint32_t)) + ok1(hashl(array, sizeof(array), 0) + == hash(array, sizeof(array), 0)); + else + ok1(hashl(array, sizeof(array), 0) + == hash64(array, sizeof(array), 0)); + /* String hash: weak, so only test bottom byte */ for (i = 0; i < 1; i++) { unsigned int num = 0, cursor, lowest = -1U, highest = 0;