1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
* 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 <QTest>
#include "common/c_jhash.h"
#define HASHSTATE 1
#define HASHLEN 1
#define MAXPAIR 80
#define MAXLEN 70
inline std::uint8_t operator"" _u8(unsigned long long value)
{
return static_cast<std::uint8_t>(value);
}
class TestJHash : public QObject
{
Q_OBJECT
private Q_SLOTS:
void check_c_jhash_trials()
{
uint8_t qa[MAXLEN + 1], qb[MAXLEN + 2], *a = &qa[0], *b = &qb[1];
uint64_t c[HASHSTATE], d[HASHSTATE], i, j = 0, k, l, m, z;
uint64_t e[HASHSTATE], f[HASHSTATE], g[HASHSTATE], h[HASHSTATE];
uint64_t x[HASHSTATE], y[HASHSTATE];
uint64_t hlen;
for (hlen = 0; hlen < MAXLEN; ++hlen) {
z = 0;
for (i = 0; i < hlen; ++i) { /*----------------------- for each input byte, */
for (j = 0; j < 8; ++j) { /*------------------------ for each input bit, */
for (m = 1; m < 8; ++m) { /*------------ for serveral possible initvals, */
for (l = 0; l < HASHSTATE; ++l)
e[l] = f[l] = g[l] = h[l] = x[l] = y[l] = ~0ull;
/*---- check that every output bit is affected by that input bit */
for (k = 0; k < MAXPAIR; k += 2) {
uint64_t finished = 1;
/* keys have one bit different */
for (l = 0; l < hlen + 1; ++l) {
a[l] = b[l] = 0;
}
/* have a and b be two keys differing in only one bit */
a[i] ^= (k << j);
a[i] ^= (k >> (8 - j));
c[0] = c_jhash64(a, hlen, m);
b[i] ^= ((k + 1) << j);
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; l < HASHSTATE; ++l) {
e[l] &= (c[l] ^ d[l]);
f[l] &= ~(c[l] ^ d[l]);
g[l] &= c[l];
h[l] &= ~c[l];
x[l] &= d[l];
y[l] &= ~d[l];
if (e[l] | f[l] | g[l] | h[l] | x[l] | y[l])
finished = 0;
}
if (finished)
break;
}
if (k > z)
z = k;
if (k == MAXPAIR) {
auto format = [](int i) {
return QString::number(i, '0', 8);
};
QFAIL(qPrintable(
QStringLiteral("Some bit didn't change: %1 %2 %3 %4 %5 %6 ").arg(format(e[0]), format(f[0]), format(g[0]), format(h[0]), format(x[0]), format(y[0])) //
+ QStringLiteral("i %1 j %2 m %3 len %4").arg(QString::number(i), QString::number(j), QString::number(m), QString::number(hlen))));
}
if (z == MAXPAIR) {
if (z < MAXPAIR) {
QVERIFY(z < MAXPAIR);
// print_error("%u trials needed, should be less than 40\n", z/2);
return;
}
}
}
}
}
}
}
void check_c_jhash_alignment_problems()
{
uint64_t test;
uint8_t buf[MAXLEN + 20], *b;
uint64_t len;
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";
uint64_t h, i, j, ref, x, y;
test = c_jhash64(q, sizeof(q) - 1, 0);
QCOMPARE(test, c_jhash64(qq + 1, sizeof(q) - 1, 0));
QCOMPARE(test, c_jhash64(qq + 1, sizeof(q) - 1, 0));
QCOMPARE(test, c_jhash64(qqq + 2, sizeof(q) - 1, 0));
QCOMPARE(test, c_jhash64(qqqq + 3, sizeof(q) - 1, 0));
for (h = 0, b = buf + 1; h < 8; ++h, ++b) {
for (i = 0; i < MAXLEN; ++i) {
len = i;
for (j = 0; j < i; ++j)
*(b + j) = 0;
/* these should all be equal */
ref = c_jhash64(b, len, 1);
*(b + i) = ~0_u8;
*(b - 1) = ~0_u8;
x = c_jhash64(b, len, 1);
y = c_jhash64(b, len, 1);
QVERIFY(!(ref != x) || (ref != y));
}
}
}
void check_c_jhash_null_strings()
{
uint8_t buf[1];
uint64_t h, i, t;
buf[0] = ~0_u8;
for (i = 0, h = 0; i < 8; ++i) {
t = h;
h = c_jhash64(buf, 0, h);
QVERIFY(t != h);
// print_error("0-byte-string check failed: t = %.8x, h = %.8x", t, h);
}
}
void check_c_jhash64_trials()
{
uint8_t qa[MAXLEN + 1], qb[MAXLEN + 2];
uint8_t *a, *b;
uint64_t c[HASHSTATE], d[HASHSTATE], i, j = 0, k, l, m, z;
uint64_t e[HASHSTATE], f[HASHSTATE], g[HASHSTATE], h[HASHSTATE];
uint64_t x[HASHSTATE], y[HASHSTATE];
uint64_t hlen;
a = &qa[0];
b = &qb[1];
for (hlen = 0; hlen < MAXLEN; ++hlen) {
z = 0;
for (i = 0; i < hlen; ++i) { /*----------------------- for each byte, */
for (j = 0; j < 8; ++j) { /*------------------------ for each bit, */
for (m = 0; m < 8; ++m) { /*-------- for serveral possible levels, */
for (l = 0; l < HASHSTATE; ++l)
e[l] = f[l] = g[l] = h[l] = x[l] = y[l] = ~0ul;
/*---- check that every input bit affects every output bit */
for (k = 0; k < MAXPAIR; k += 2) {
uint64_t finished = 1;
/* keys have one bit different */
for (l = 0; l < hlen + 1; ++l) {
a[l] = b[l] = 0;
}
/* have a and b be two keys differing in only one bit */
a[i] ^= (k << j);
a[i] ^= (k >> (8 - j));
c[0] = c_jhash64(a, hlen, m);
b[i] ^= ((k + 1) << j);
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; l < HASHSTATE; ++l) {
e[l] &= (c[l] ^ d[l]);
f[l] &= ~(c[l] ^ d[l]);
g[l] &= c[l];
h[l] &= ~c[l];
x[l] &= d[l];
y[l] &= ~d[l];
if (e[l] | f[l] | g[l] | h[l] | x[l] | y[l])
finished = 0;
}
if (finished)
break;
}
if (k > z)
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",
i,j,m,hlen);
#endif
}
if (z == MAXPAIR) {
if (z < MAXPAIR) {
#if 0
print_error("%lu trials needed, should be less than 40", z/2);
#endif
QVERIFY(z < MAXPAIR);
}
return;
}
}
}
}
}
}
void check_c_jhash64_alignment_problems(void **state)
{
uint8_t buf[MAXLEN + 20], *b;
uint64_t len;
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, i, j, ref, t, x, y;
(void)state; /* unused */
h = c_jhash64(q + 0, (sizeof(q) - 1), 0);
t = h;
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
h = c_jhash64(qq + 1, (sizeof(q) - 1), 0);
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
h = c_jhash64(qqq + 2, (sizeof(q) - 1), 0);
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
h = c_jhash64(qqqq + 3, (sizeof(q) - 1), 0);
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
h = c_jhash64(o + 4, (sizeof(q) - 1), 0);
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
h = c_jhash64(oo + 5, (sizeof(q) - 1), 0);
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
h = c_jhash64(ooo + 6, (sizeof(q) - 1), 0);
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
h = c_jhash64(oooo + 7, (sizeof(q) - 1), 0);
QCOMPARE(t, h);
// , "%.8lx%.8lx\n", h, (h>>32));
for (h = 0, b = buf + 1; h < 8; ++h, ++b) {
for (i = 0; i < MAXLEN; ++i) {
len = i;
for (j = 0; j < i; ++j)
*(b + j) = 0;
/* these should all be equal */
ref = c_jhash64(b, len, 1);
*(b + i) = ~0_u8;
*(b - 1) = ~0_u8;
x = c_jhash64(b, len, 1);
y = c_jhash64(b, len, 1);
QVERIFY(!(ref != x) || (ref != y));
#if 0
print_error("alignment error: %.8lx %.8lx %.8lx %ld %ld\n", ref, x, y, h, i);
#endif
}
}
}
void check_c_jhash64_null_strings()
{
uint8_t buf[1];
uint64_t h, i, t;
buf[0] = ~0_u8;
for (i = 0, h = 0; i < 8; ++i) {
t = h;
h = c_jhash64(buf, 0, h);
QVERIFY(t != h);
#if 0
print_error("0-byte-string check failed: t = %.8lx, h = %.8lx", t, h);
#endif
}
}
};
QTEST_APPLESS_MAIN(TestJHash)
#include "testjhash.moc"
|