Skip to content

Commit

Permalink
Add singular flags variable to carry reporting options
Browse files Browse the repository at this point in the history
This replaces "bool verbose" and "bool drawDiagram" almost everywhere. I
decided to put flags-related things in TestGlobals.h, so non-testlib code
still uses bools.

This also moves stddev reporting in SpeedTest to be under --verbose.
  • Loading branch information
fwojcik committed Dec 9, 2023
1 parent 2b35dcb commit ed9d133
Show file tree
Hide file tree
Showing 48 changed files with 471 additions and 432 deletions.
86 changes: 44 additions & 42 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@

//-----------------------------------------------------------------------------
// Locally-visible configuration
static bool g_drawDiagram = false;
static bool g_forceSummary = false;

// Setting to test more thoroughly.
Expand Down Expand Up @@ -272,7 +271,8 @@ static HashInfo::endianness parse_endian( const char * str ) {
//-----------------------------------------------------------------------------
// Self-tests - verify that hashes work correctly

static void HashSelfTestAll( bool verbose ) {
static void HashSelfTestAll( flags_t flags ) {
bool verbose = REPORT(VERBOSE, flags);
bool pass = true;

printf("[[[ VerifyAll Tests ]]]\n\n");
Expand All @@ -298,14 +298,14 @@ static bool HashSelfTest( const HashInfo * hinfo ) {
return result;
}

static void HashSanityTestAll( bool verbose ) {
static void HashSanityTestAll( flags_t flags ) {
const uint64_t mask_flags = FLAG_HASH_MOCK | FLAG_HASH_CRYPTOGRAPHIC;
uint64_t prev_flags = FLAG_HASH_MOCK;
std::vector<const HashInfo *> allHashes = findAllHashes();

printf("[[[ SanityAll Tests ]]]\n\n");

SanityTestHeader(verbose);
SanityTestHeader(flags);
for (const HashInfo * h: allHashes) {
if ((h->hash_flags & mask_flags) != prev_flags) {
printf("\n");
Expand All @@ -315,22 +315,22 @@ static void HashSanityTestAll( bool verbose ) {
printf("%s : hash initialization failed!", h->name);
continue;
}
SanityTest(h, true, verbose);
SanityTest(h, flags, true);
}
printf("\n");
}

//-----------------------------------------------------------------------------
// Quickly speed test all hashes

static void HashSpeedTestAll( bool verbose ) {
static void HashSpeedTestAll( flags_t flags ) {
const uint64_t mask_flags = FLAG_HASH_MOCK | FLAG_HASH_CRYPTOGRAPHIC;
uint64_t prev_flags = FLAG_HASH_MOCK;
std::vector<const HashInfo *> allHashes = findAllHashes();

printf("[[[ Short Speed Tests ]]]\n\n");

ShortSpeedTestHeader(verbose);
ShortSpeedTestHeader(flags);
for (const HashInfo * h: allHashes) {
if ((h->hash_flags & mask_flags) != prev_flags) {
printf("\n");
Expand All @@ -340,7 +340,7 @@ static void HashSpeedTestAll( bool verbose ) {
printf("%s : hash initialization failed!", h->name);
continue;
}
ShortSpeedTest(h, verbose);
ShortSpeedTest(h, flags);
}
printf("\n");
}
Expand Down Expand Up @@ -371,7 +371,7 @@ static void print_pvaluecounts( void ) {
//-----------------------------------------------------------------------------

template <typename hashtype>
static bool test( const HashInfo * hInfo ) {
static bool test( const HashInfo * hInfo, const flags_t flags ) {
bool result = true;

if (g_testAll) {
Expand Down Expand Up @@ -416,145 +416,145 @@ static bool test( const HashInfo * hInfo ) {
printf("[[[ Sanity Tests ]]]\n\n");

result &= HashSelfTest(hInfo);
result &= (SanityTest(hInfo) || hInfo->isMock());
result &= (SanityTest(hInfo, flags) || hInfo->isMock());
printf("\n");
}

//-----------------------------------------------------------------------------
// Speed tests

if (g_testSpeed) {
SpeedTest(hInfo);
SpeedTest(hInfo, flags);
}

if (g_testHashmap) {
result &= HashMapTest(hInfo, g_drawDiagram, g_testExtra);
result &= HashMapTest(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Avalanche tests

if (g_testAvalanche) {
result &= AvalancheTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= AvalancheTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Bit Independence Criteria

if (g_testBIC) {
result &= BicTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= BicTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'Zeroes'

if (g_testZeroes) {
result &= ZeroKeyTest<hashtype>(hInfo, g_drawDiagram);
result &= ZeroKeyTest<hashtype>(hInfo, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'Cyclic' - keys of the form "abcdabcdabcd..."

if (g_testCyclic) {
result &= CyclicKeyTest<hashtype>(hInfo, g_drawDiagram);
result &= CyclicKeyTest<hashtype>(hInfo, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'Sparse' - keys with all bits 0 except a few

if (g_testSparse) {
result &= SparseKeyTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= SparseKeyTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'Permutation' - all possible combinations of a set of blocks

if (g_testPermutation) {
result &= PermutedKeyTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= PermutedKeyTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'Text'

if (g_testText) {
result &= TextKeyTest<hashtype>(hInfo, g_drawDiagram);
result &= TextKeyTest<hashtype>(hInfo, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'TwoBytes' - all keys up to N bytes containing two non-zero bytes

if (g_testTwoBytes) {
result &= TwoBytesKeyTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= TwoBytesKeyTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'PerlinNoise'

if (g_testPerlinNoise) {
result &= PerlinNoiseTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= PerlinNoiseTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'Bitflip'

if (g_testBitflip) {
result &= BitflipTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= BitflipTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'SeedZeroes'

if (g_testSeedZeroes) {
result &= SeedZeroKeyTest<hashtype>(hInfo, g_drawDiagram);
result &= SeedZeroKeyTest<hashtype>(hInfo, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'SeedSparse'

if (g_testSeedSparse) {
result &= SeedSparseTest<hashtype>(hInfo, g_drawDiagram);
result &= SeedSparseTest<hashtype>(hInfo, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'SeedBlockLen'

if (g_testSeedBlockLen) {
result &= SeedBlockLenTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= SeedBlockLenTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'SeedBlockOffset'

if (g_testSeedBlockOffset) {
result &= SeedBlockOffsetTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= SeedBlockOffsetTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'Seed'

if (g_testSeed) {
result &= SeedTest<hashtype>(hInfo, g_drawDiagram);
result &= SeedTest<hashtype>(hInfo, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'SeedAvalanche'

if (g_testSeedAvalanche) {
result &= SeedAvalancheTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= SeedAvalancheTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'SeedBIC'

if (g_testSeedBIC) {
result &= SeedBicTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= SeedBicTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
// Keyset 'SeedBitflip'

if (g_testSeedBitflip) {
result &= SeedBitflipTest<hashtype>(hInfo, g_drawDiagram, g_testExtra);
result &= SeedBitflipTest<hashtype>(hInfo, g_testExtra, flags);
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -597,7 +597,7 @@ static bool test( const HashInfo * hInfo ) {

//-----------------------------------------------------------------------------

static bool testHash( const char * name ) {
static bool testHash( const char * name, const flags_t flags ) {
const HashInfo * hInfo;

if ((hInfo = findHash(name)) == NULL) {
Expand All @@ -608,22 +608,22 @@ static bool testHash( const char * name ) {
// If you extend these statements by adding a new bitcount/type, you
// need to adjust HASHTYPELIST in util/Instantiate.h also.
if (hInfo->bits == 32) {
return test<Blob<32>>(hInfo);
return test<Blob<32>>(hInfo, flags);
}
if (hInfo->bits == 64) {
return test<Blob<64>>(hInfo);
return test<Blob<64>>(hInfo, flags);
}
if (hInfo->bits == 128) {
return test<Blob<128>>(hInfo);
return test<Blob<128>>(hInfo, flags);
}
if (hInfo->bits == 160) {
return test<Blob<160>>(hInfo);
return test<Blob<160>>(hInfo, flags);
}
if (hInfo->bits == 224) {
return test<Blob<224>>(hInfo);
return test<Blob<224>>(hInfo, flags);
}
if (hInfo->bits == 256) {
return test<Blob<256>>(hInfo);
return test<Blob<256>>(hInfo, flags);
}

printf("Invalid hash bit width %d for hash '%s'", hInfo->bits, hInfo->name);
Expand Down Expand Up @@ -673,6 +673,7 @@ int main( int argc, const char ** argv ) {
usage();
}

flags_t flags = FLAG_REPORT_PROGRESS;
for (int argnb = 1; argnb < argc; argnb++) {
const char * const arg = argv[argnb];
if (strncmp(arg, "--", 2) == 0) {
Expand Down Expand Up @@ -701,7 +702,8 @@ int main( int argc, const char ** argv ) {
exit(0);
}
if (strcmp(arg, "--verbose") == 0) {
g_drawDiagram = true;
flags |= FLAG_REPORT_VERBOSE;
flags |= FLAG_REPORT_DIAGRAMS;
continue;
}
if (strcmp(arg, "--force-summary") == 0) {
Expand Down Expand Up @@ -797,13 +799,13 @@ int main( int argc, const char ** argv ) {
size_t timeBegin = monotonic_clock();

if (g_testVerifyAll) {
HashSelfTestAll(g_drawDiagram);
HashSelfTestAll(flags);
} else if (g_testSanityAll) {
HashSanityTestAll(g_drawDiagram);
HashSanityTestAll(flags);
} else if (g_testSpeedAll) {
HashSpeedTestAll(g_drawDiagram);
HashSpeedTestAll(flags);
} else {
testHash(hashToTest);
testHash(hashToTest, flags);
}

size_t timeEnd = monotonic_clock();
Expand Down
17 changes: 8 additions & 9 deletions tests/AvalancheTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ typedef unsigned a_uint;

template <typename hashtype>
static void calcBiasRange( const HashFn hash, const seed_t seed, std::vector<uint32_t> & bins, const unsigned keybytes,
const uint8_t * keys, a_uint & irepp, const unsigned reps, const bool verbose ) {
const uint8_t * keys, a_uint & irepp, const unsigned reps, const flags_t flags ) {
const unsigned keybits = keybytes * 8;

VLA_ALLOC(uint8_t, buf, keybytes);
hashtype A, B;
unsigned irep;

while ((irep = irepp++) < reps) {
if (verbose) {
if (REPORT(PROGRESS, flags)) {
progressdots(irep, 0, reps - 1, 18);
}

Expand All @@ -110,7 +110,7 @@ static void calcBiasRange( const HashFn hash, const seed_t seed, std::vector<uin

template <typename hashtype>
static bool AvalancheImpl( HashFn hash, const seed_t seed, const unsigned keybits,
const unsigned reps, bool drawDiagram, bool drawdots ) {
const unsigned reps, flags_t flags ) {
assert((keybits & 7) == 0);

const unsigned keybytes = keybits / 8;
Expand All @@ -135,14 +135,14 @@ static bool AvalancheImpl( HashFn hash, const seed_t seed, const unsigned keybit
}

if (g_NCPU == 1) {
calcBiasRange<hashtype>(hash, seed, bins[0], keybytes, &keys[0], irep, reps, drawdots);
calcBiasRange<hashtype>(hash, seed, bins[0], keybytes, &keys[0], irep, reps, flags);
} else {
#if defined(HAVE_THREADS)
std::vector<std::thread> t(g_NCPU);
for (unsigned i = 0; i < g_NCPU; i++) {
t[i] = std::thread {
calcBiasRange<hashtype>, hash, seed, std::ref(bins[i]),
keybytes, &keys[0], std::ref(irep), reps, drawdots
keybytes, &keys[0], std::ref(irep), reps, flags
};
}
for (unsigned i = 0; i < g_NCPU; i++) {
Expand All @@ -160,7 +160,7 @@ static bool AvalancheImpl( HashFn hash, const seed_t seed, const unsigned keybit

bool result = true;

result &= ReportBias(&bins[0][0], reps, arraysize, hashbits, drawDiagram);
result &= ReportBias(&bins[0][0], reps, arraysize, hashbits, flags);

recordTestResult(result, "Avalanche", keybytes);

Expand All @@ -170,10 +170,9 @@ static bool AvalancheImpl( HashFn hash, const seed_t seed, const unsigned keybit
//-----------------------------------------------------------------------------

template <typename hashtype>
bool AvalancheTest( const HashInfo * hinfo, const bool verbose, const bool extra ) {
bool AvalancheTest( const HashInfo * hinfo, bool extra, flags_t flags ) {
const HashFn hash = hinfo->hashFn(g_hashEndian);
bool result = true;
bool drawdots = true; // .......... progress dots

printf("[[[ Avalanche Tests ]]]\n\n");

Expand All @@ -188,7 +187,7 @@ bool AvalancheTest( const HashInfo * hinfo, const bool verbose, const bool extra
}

for (unsigned testBits: testBitsvec) {
result &= AvalancheImpl<hashtype>(hash, seed, testBits, 300000, verbose, drawdots);
result &= AvalancheImpl<hashtype>(hash, seed, testBits, 300000, flags);
}

printf("\n%s\n", result ? "" : g_failstr);
Expand Down
2 changes: 1 addition & 1 deletion tests/AvalancheTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
*/

template <typename hashtype>
bool AvalancheTest( const HashInfo * info, const bool verbose, const bool extra );
bool AvalancheTest( const HashInfo * info, bool extra, flags_t flags );
Loading

0 comments on commit ed9d133

Please sign in to comment.