Skip to content

Commit

Permalink
add BadSeeds test
Browse files Browse the repository at this point in the history
for now just exhaustive, against some simple 16byte keys.
need to check the bad_seeds lists also, as these tests will
actually terminate

See GH #99
  • Loading branch information
rurban committed Mar 29, 2021
1 parent 1c8db23 commit 75c784a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
38 changes: 38 additions & 0 deletions KeysetTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,44 @@ bool PrngTest ( hashfunc<hashtype> hash,
return result;
}

//-----------------------------------------------------------------------------
// Find bad seeds, and test against the known bad hashes.

template< typename hashtype >
bool BadSeedsTest ( hashfunc<hashtype> hash, bool testAll )
{

bool result = true;
if (sizeof(hashtype) > 8) {
printf("BadSeedsTest is designed for hashes <= 64-bit \n");
return false;
}

const size_t max_seed = sizeof(hashtype) == 8 ? 0xffffffffffffffff : 0xffffffff;
// TODO: testAll: checking known or suspected bad seeds
printf("Testing 0x%lx seeds ...\n", max_seed);

for (size_t y=0; y < max_seed; y++) {
static hashtype zero;
hashtype h;
Hash_Seed_init (hash, y);
for (int x = 0; x < 256; x++) {
std::vector<hashtype> hashes;
uint8_t key[16];
memset(&key, x, sizeof(key));
hash(key, 16, y, &h);
if (memcmp(&zero, &h, sizeof(hashtype)) == 0)
printf("Broken seed 0x%x => 0\n", y);
else
hashes.push_back(h);
if (!TestHashList(hashes,false, true, false, false, false, false)) {
printf("Bad seed 0x%x => %d\n", y, x);
result = false;
}
}
}
return result;
}

//-----------------------------------------------------------------------------
// Keyset 'Perlin Noise' - X,Y coordinates on input & seed
Expand Down
20 changes: 13 additions & 7 deletions Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,25 +539,29 @@ hashtype bitreverse(hashtype n, size_t b = sizeof(hashtype) * 8)
template < typename hashtype >
bool TestHashList ( std::vector<hashtype> & hashes, bool drawDiagram,
bool testCollision = true, bool testDist = true,
bool testHighBits = true, bool testLowBits = true)
bool testHighBits = true, bool testLowBits = true,
bool verbose = true)
{
bool result = true;

if (testCollision)
{
size_t const count = hashes.size();
double const expected = EstimateNbCollisions(count, sizeof(hashtype) * 8);
printf("Testing collisions (%3i-bit) - Expected %6.1f, ",
(int)sizeof(hashtype)*8, expected);
if (verbose)
printf("Testing collisions (%3i-bit) - Expected %6.1f, ",
(int)sizeof(hashtype)*8, expected);
const int i_expected = (int)expected;

int collcount = 0;
HashSet<hashtype> collisions;
collcount = FindCollisions(hashes, collisions, 1000, drawDiagram);
double ratio = double(collcount) / expected;
printf("actual %6i (%.2fx)", (int)collcount, expected > 0.0 ? ratio : (double)collcount);
if (ratio > 0.98 && collcount != i_expected)
printf(" (%i)", collcount - i_expected);
if (verbose) {
printf("actual %6i (%.2fx)", (int)collcount, expected > 0.0 ? ratio : (double)collcount);
if (ratio > 0.98 && collcount != i_expected)
printf(" (%i)", collcount - i_expected);
}

if (sizeof(hashtype) <= sizeof(uint32_t))
{
Expand Down Expand Up @@ -606,7 +610,9 @@ bool TestHashList ( std::vector<hashtype> & hashes, bool drawDiagram,
}
}

printf("\n");
if (verbose) {
printf("\n");
}
fflush(NULL);

if (testHighBits) {
Expand Down
15 changes: 14 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bool g_testDiffDist = false;
bool g_testMomentChi2 = false;
bool g_testPrng = false;
bool g_testBIC = false;
bool g_testBadSeeds = false;
//bool g_testLongNeighbors = false;

double g_speed = 0.0;
Expand Down Expand Up @@ -67,6 +68,7 @@ TestOpts g_testopts[] =
{ g_testBIC, "BIC" },
{ g_testMomentChi2, "MomentChi2" },
{ g_testPrng, "Prng" },
{ g_testBadSeeds, "BadSeeds" },
//{ g_testLongNeighbors,"LongNeighbors" }
};

Expand Down Expand Up @@ -1397,7 +1399,6 @@ void test ( hashfunc<hashtype> hash, HashInfo* info )
fflush(NULL);
}


if (g_testPrng || g_testAll)
{
printf("[[[ Prng Tests ]]]\n\n");
Expand Down Expand Up @@ -1465,6 +1466,18 @@ void test ( hashfunc<hashtype> hash, HashInfo* info )
fflush(NULL);
}

if (g_testBadSeeds)
{
printf("[[[ BadSeeds Tests ]]]\n\n");
// g_testExtra: test all seeds, or just some known bad seeds

Seed_init (info, 0);
bool result = BadSeedsTest<hashtype>( hash, g_testExtra );
if(!result) printf("\n*********FAIL*********\n");
printf("\n");
fflush(NULL);
}

}

//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 75c784a

Please sign in to comment.