/* * Tests are taken form lookup2.c and lookup8.c * by Bob Jenkins, December 1996, Public Domain. * * See http://burtleburtle.net/bob/hash/evahash.html */ #include "torture.h" #include "common/c_jhash.h" #define HASHSTATE 1 #define HASHLEN 1 #define MAXPAIR 80 #define MAXLEN 70 static void check_c_jhash_trials(void **state) { uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1]; uint32_t c[HASHSTATE]; uint32_t d[HASHSTATE]; uint32_t i = 0; uint32_t j = 0; uint32_t k = 0; uint32_t l = 0; uint32_t m = 0; uint32_t z = 0; uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE]; uint32_t x[HASHSTATE],y[HASHSTATE]; uint32_t hlen = 0; (void) state; /* unused */ for (hlen=0; hlen < MAXLEN; ++hlen) { z=0; for (i=0; i>(8-j)); c[0] = c_jhash(a, hlen, m); b[i] ^= ((k+1)<>(8-j)); d[0] = c_jhash(b, hlen, m); /* check every bit is 1, 0, set, and not set at least once */ for (l=0; lz) z=k; if (k==MAXPAIR) { print_error("Some bit didn't change: "); print_error("%.8x %.8x %.8x %.8x %.8x %.8x ", e[0], f[0], g[0], h[0], x[0], y[0]); print_error("i %d j %d m %d len %d\n",i,j,m,hlen); } if (z == MAXPAIR) { if (z < MAXPAIR) { assert_true(z < MAXPAIR); // print_error("%u trials needed, should be less than 40\n", z/2); return; } } } } } } } static void check_c_jhash_alignment_problems(void **state) { uint32_t test = 0; uint8_t buf[MAXLEN+20]; uint8_t *b = NULL; uint32_t len = 0; uint8_t q[] = "This is the time for all good men to come to the aid of their country"; uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country"; uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country"; uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country"; uint32_t h = 0; uint32_t i = 0; uint32_t j = 0; uint32_t ref = 0; uint32_t x = 0; uint32_t y = 0; (void) state; /* unused */ test = c_jhash(q, sizeof(q)-1, (uint32_t)0); assert_true(test == c_jhash(qq+1, sizeof(q)-1, (uint32_t)0)); assert_true(test == c_jhash(qq+1, sizeof(q)-1, (uint32_t)0)); assert_true(test == c_jhash(qqq+2, sizeof(q)-1, (uint32_t)0)); assert_true(test == c_jhash(qqqq+3, sizeof(q)-1, (uint32_t)0)); for (h=0, b=buf+1; h<8; ++h, ++b) { for (i=0; i>(8-j)); c[0] = c_jhash64(a, hlen, m); b[i] ^= ((k+1)<>(8-j)); d[0] = c_jhash64(b, hlen, m); /* check every bit is 1, 0, set, and not set at least once */ for (l=0; lz) z=k; if (k==MAXPAIR) { #if 0 print_error("Some bit didn't change: "); print_error("%.8llx %.8llx %.8llx %.8llx %.8llx %.8llx ", (long long unsigned int) e[0], (long long unsigned int) f[0], (long long unsigned int) g[0], (long long unsigned int) h[0], (long long unsigned int) x[0], (long long unsigned int) y[0]); print_error("i %d j %d m %d len %d\n", (uint32_t)i,(uint32_t)j,(uint32_t)m,(uint32_t)hlen); #endif } if (z == MAXPAIR) { if (z < MAXPAIR) { #if 0 print_error("%lu trials needed, should be less than 40", z/2); #endif assert_true(z < MAXPAIR); } return; } } } } } } static void check_c_jhash64_alignment_problems(void **state) { uint8_t buf[MAXLEN+20]; uint8_t *b = NULL; uint64_t len = 0; uint8_t q[] = "This is the time for all good men to come to the aid of their country"; uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country"; uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country"; uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country"; uint8_t o[] = "xxxxThis is the time for all good men to come to the aid of their country"; uint8_t oo[] = "xxxxxThis is the time for all good men to come to the aid of their country"; uint8_t ooo[] = "xxxxxxThis is the time for all good men to come to the aid of their country"; uint8_t oooo[] = "xxxxxxxThis is the time for all good men to come to the aid of their country"; uint64_t h = 0; uint64_t i = 0; uint64_t j = 0; uint64_t ref = 0; uint64_t t = 0; uint64_t x = 0; uint64_t y = 0; (void) state; /* unused */ h = c_jhash64(q+0, (uint64_t)(sizeof(q)-1), (uint64_t)0); t = h; assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); h = c_jhash64(qq+1, (uint64_t)(sizeof(q)-1), (uint64_t)0); assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); h = c_jhash64(qqq+2, (uint64_t)(sizeof(q)-1), (uint64_t)0); assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); h = c_jhash64(qqqq+3, (uint64_t)(sizeof(q)-1), (uint64_t)0); assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); h = c_jhash64(o+4, (uint64_t)(sizeof(q)-1), (uint64_t)0); assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); h = c_jhash64(oo+5, (uint64_t)(sizeof(q)-1), (uint64_t)0); assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); h = c_jhash64(ooo+6, (uint64_t)(sizeof(q)-1), (uint64_t)0); assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); h = c_jhash64(oooo+7, (uint64_t)(sizeof(q)-1), (uint64_t)0); assert_true(t == h); // , "%.8lx%.8lx\n", (uint32_t)h, (uint32_t)(h>>32)); for (h=0, b=buf+1; h<8; ++h, ++b) { for (i=0; i