From: hama Date: Mon, 9 Feb 2015 23:42:53 +0000 (+0100) Subject: ReMD5 works, CUnit clean X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=c867ecdb6cacab15284e3e1aac19f09129cbc211;p=crepublib ReMD5 works, CUnit clean --- diff --git a/cunit/cuReMD5.cpp b/cunit/cuReMD5.cpp index 22559c5..9b32c4a 100644 --- a/cunit/cuReMD5.cpp +++ b/cunit/cuReMD5.cpp @@ -6,15 +6,6 @@ * No warranties and disclaimer of any damages. * The latest sources: https://github.com/republib */ -/* - * cuReMatcher.hpp - * - * License: Public domain - * Do what you want. - * No warranties and disclaimer of any damages. - * The latest sources: https://github.com/republib - */ - #include "base/rebase.hpp" #include "math/remath.hpp" @@ -22,12 +13,15 @@ class TestReMd5 : public ReTestUnit { public: - TestReMd5() : ReTestUnit("ReMatcher", __FILE__){ + TestReMd5() : ReTestUnit("ReMD5", __FILE__){ run(); } private: void run(){ - testOld(); + testSpeed(); + testLong(); + testBig(); + //testOld(); testBase(); } void testOld(){ @@ -60,6 +54,73 @@ private: checkEqu("d41d8cd98f00b204e9800998ecf8427e", md5.hexDigest().str()); } + int testOneLong(ReByteBuffer& text, int seed2 = 0){ + ReMD5 md5; + int rc = 0; + ReShiftRandom random; + random.setSeed(text.length() + seed2); + + md5.update((uint8_t*) text.str(), text.length()); + ReByteBuffer digest(md5.hexDigest()); + md5.reset(); + int max2 = text.length() / 20; + if (max2 < 1) + max2 = random.nextInt(20); + while(text.length() > 0){ + int part = random.nextInt(32, 32 + 1 + max2); + if (part > text.length()) + part = text.length(); + md5.update((uint8_t*) text.str(), part); + text.remove(0, part); + rc++; + } + checkEqu(digest.str(), md5.hexDigest().str()); + return rc; + } + void testLong(){ + ReByteBuffer text("01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVEXYZ0123456789001234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVEXYZ01234567890\n"); + for (int ii = 178 - 69 + 1; ii > 0; ii--){ + text.setLength(ii); + testOneLong(text, 200 * ii + 0x47111); + } + } + void testBig(){ + ReByteBuffer text; + ReShiftRandom random; + int64_t start = timer(); + int count = 1000; + int parts = 0; + int sumLength = 0; + for (int ii = 0; ii < count; ii++){ + random.nextString(10, 20000, text); + sumLength += text.length(); + parts += testOneLong(text, 333 * ii + 0x55221); + } + printf("count: %d updates: %d avg length: %d duration: %s\n", + count, parts, sumLength / count, + ReByteBuffer("").appendMilliSec(milliSecSince(start)).str()); + } + void testSpeed(){ + int max = 1024*1024*50; + ReByteBuffer buffer; + buffer.ensureSize(max); + buffer.setLength(max - 1); + buffer.fill('x', max - 1); + ReMD5 md5; + int passes = 2; + int64_t start = timer(); + for (int ii = 0; ii < passes; ii++){ + md5.reset(); + md5.update((uint8_t*) buffer.str(), max - 1); + } + int duration = milliSecSince(start); + if (duration == 0) + duration = 1; + printf("size: %.3f MiByte count: %d rate: %.3f MiB/sec duration: %s\n", + max / 1024.0 / 1024, passes, + max / 1024.0 / 1024 * 1000 * passes / duration, + ReByteBuffer("").appendMilliSec(duration).str()); + } }; extern void testReMD5(void); diff --git a/cunit/cuReRandomizer.cpp b/cunit/cuReRandomizer.cpp index 6615e7b..666c46e 100644 --- a/cunit/cuReRandomizer.cpp +++ b/cunit/cuReRandomizer.cpp @@ -26,25 +26,28 @@ private: ReCongruentialGenerator rand; const int MAX = 16; time_t start = time(NULL); - ReRandomizer::seed_t field[MAX]; + int64_t field[MAX]; for (int ix = 0; ix < MAX; ix++) field[ix] = rand.nextInt64(); rand.reset(); for (int ix = 0; ix < MAX; ix++){ - checkEqu(field[ix], rand.nextInt64(ix + 10)); + int64_t current = rand.nextInt64(); + if (field[ix] != current) + checkEqu(field[ix], current); } } void testNextInt64(){ ReCongruentialGenerator rand; const int MAX = 16; time_t start = time(NULL); - ReRandomizer::seed_t field[MAX]; + int64_t field[MAX]; for (int ix = 0; ix < MAX; ix++) field[ix] = rand.nextInt64(ix + 10); rand.reset(); for (int ix = 0; ix < MAX; ix++){ checkEqu(field[ix], rand.nextInt64(ix + 10)); - checkT(field[ix] >= 0 && field[ix] < ix + 10); + if (! (field[ix] >= 0 && field[ix] <= ix + 10)) + checkT(false); } time_t diff = time(NULL) - start; } diff --git a/math/ReMD5.cpp b/math/ReMD5.cpp index 2f89478..375a91c 100644 --- a/math/ReMD5.cpp +++ b/math/ReMD5.cpp @@ -174,7 +174,6 @@ void ReMD5::processChunk(const uint8_t block[64]){ #else # error "missing __LITTLE_ENDIAN__ or __BIG_ENDIAN__" #endif - //Initialize hash value for this chunk: uint32_t A = m_a0; uint32_t B = m_b0; @@ -183,12 +182,13 @@ void ReMD5::processChunk(const uint8_t block[64]){ //Main loop: int F, g; -#define TRACE_MD5 +//#define TRACE_MD5 #if defined TRACE_MD5 printf("neu: (%s)\n", block); #endif for (int i = 0; i < 64; i++){ #if defined TRACE_MD5 + if (i > 60) printf("%2d: A: %08x B: %08x C: %08x D%08x\n", i, A, B, C, D); #endif if (i < 16){ @@ -212,6 +212,7 @@ void ReMD5::processChunk(const uint8_t block[64]){ g = (7*i) % 16; } #if defined TRACE_MD5 + if (i > 60) printf(" K[%2d]: %08x M[%2d]: %08x shift: %02d\n", i, m_K[i], g, M[g], m_s[i]); #endif @@ -272,19 +273,15 @@ void ReMD5::update(const uint8_t* block, int blockLength){ } } // process full 512 bit chunks (64 byte blocks): - if (blockLength > 0){ - if (blockLength > 64){ - for (int ix = blockLength / 64; ix >= 0; ix--){ - processChunk(block); - block += 64; - } - } - blockLength %= 64; - if (blockLength != 0){ - assert(m_lengthWaiting == 0); - memcpy(m_waiting, block, blockLength); - m_lengthWaiting = blockLength; - } + for (int ix = blockLength / 64; ix > 0; ix--){ + processChunk(block); + block += 64; + } + blockLength %= 64; + if (blockLength != 0){ + assert(m_lengthWaiting == 0); + memcpy(m_waiting, block, blockLength); + m_lengthWaiting = blockLength; } } diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index d865cf9..10553eb 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -447,13 +447,20 @@ void ReDirOptions::help(const char* errorMessage, const char* message2) const{ */ void ReDirOptions::checkStandardFilterOptions(){ ReByteBuffer buffer; - checkDate(m_programArgs.getString("older", buffer)); - checkDate(m_programArgs.getString("younger", buffer)); - checkType(m_programArgs.getString("types", buffer)); - checkSize(m_programArgs.getString("maxsize", buffer)); - checkSize(m_programArgs.getString("minsize", buffer)); - checkPatternList(m_programArgs.getString("nodepattern", buffer)); - checkPatternList(m_programArgs.getString("pathpattern", buffer)); + if (m_programArgs.getString("older", buffer)[0] != '\0') + checkDate(buffer.str()); + if (m_programArgs.getString("younger", buffer)[0] != '\0') + checkDate(buffer.str()); + if (m_programArgs.getString("type", buffer)[0] != '\0') + checkType(buffer.str()); + if (m_programArgs.getString("maxsize", buffer)[0] != '\0') + checkSize(buffer.str()); + if (m_programArgs.getString("minsize", buffer)[0] != '\0') + checkSize(buffer.str()); + if (m_programArgs.getString("nodepattern", buffer)[0] != '\0') + checkPatternList(buffer.str()); + if (m_programArgs.getString("pathpattern", buffer)[0] != '\0') + checkPatternList(buffer.str()); if (m_programArgs.getString("verbose", buffer)[0] != '\0'){ unsigned level = V_NORMAL; if (ReStringUtils::lengthOfUnsigned(buffer.str(), -1, &level)