Skip to content

Commit d434b94

Browse files
author
Markus Humm
committed
Further improved hash unit tests. Removed further unnecessary calculations by saving intermediate results.
1 parent b5ad1b4 commit d434b94

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

Unit Tests/Tests/TestDECHash.pas

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5454,25 +5454,23 @@ procedure THash_TestBase.DoTestCalcBuffer(HashClass: TDECHash);
54545454
begin
54555455
ResultBuf := TFormat_HEXL.Encode(HashClass.CalcBuffer(Buf[0], Length(Buf)));
54565456
RawByteStrResult := BytesToRawString(ResultBuf);
5457-
// Configure again, as for password hashes DoDone would clear the salt
5458-
ConfigHashClass(HashClass, i);
5457+
54595458
CheckEquals(FTestData[i].ExpectedOutput,
54605459
RawByteStrResult,
54615460
'Index: ' + IntToStr(i) + ' - expected: <' +
54625461
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5463-
string(BytesToRawString(ResultBuf)) + '>');
5462+
string(RawByteStrResult) + '>');
54645463
end
54655464
else
54665465
begin
54675466
ResultBuf := TFormat_HEXL.Encode(HashClass.CalcBuffer(Buf, Length(Buf)));
54685467
RawByteStrResult := BytesToRawString(ResultBuf);
5469-
// Configure again, as for password hashes DoDone would clear the salt
5470-
ConfigHashClass(HashClass, i);
5468+
54715469
CheckEquals(FTestData[i].ExpectedOutput,
54725470
RawByteStrResult,
54735471
'Index: ' + IntToStr(i) + ' - expected: <' +
54745472
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5475-
string(BytesToRawString(ResultBuf)) + '>');
5473+
string(RawByteStrResult) + '>');
54765474
end;
54775475
end;
54785476
end;
@@ -5490,14 +5488,11 @@ procedure THash_TestBase.DoTestCalcBytes(HashClass: TDECHash);
54905488
ResultBuf := HashClass.CalcBytes(BytesOf(RawByteString(FTestData[i].InputData)));
54915489
RawByteStrResult := BytesToRawString(TFormat_HEXL.Encode(ResultBuf));
54925490

5493-
// Configure again, as for password hashes DoDone would clear the salt
5494-
ConfigHashClass(HashClass, i);
5495-
54965491
CheckEquals(FTestData[i].ExpectedOutput,
54975492
RawByteStrResult,
54985493
'Index: ' + IntToStr(i) + ' - expected: <' +
54995494
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5500-
string(BytesToRawString(TFormat_HEXL.Encode(ResultBuf))) + '>');
5495+
string(RawByteStrResult) + '>');
55015496
end;
55025497
end;
55035498

@@ -5520,9 +5515,6 @@ procedure THash_TestBase.DoTestCalcUnicodeString(HashClass: TDECHash);
55205515
TFormat_HEXL.Encode(
55215516
System.SysUtils.BytesOf(StrResult)));
55225517

5523-
// Configure again, as for password hashes DoDone would clear the salt
5524-
ConfigHashClass(HashClass, i);
5525-
55265518
CheckEquals(FTestData[i].ExpectedOutputUTFStrTest,
55275519
RawByteStrResult,
55285520
'Index: ' + IntToStr(i) + ' - expected: <' +
@@ -5636,9 +5628,6 @@ procedure THash_TestBase.DoTestCalcRawByteString(HashClass: TDECHash);
56365628

56375629
RawByteStrResult := HashClass.CalcString(FTestData[i].InputData, TFormat_HEXL);
56385630

5639-
// Configure again, as for password hashes DoDone would clear the salt
5640-
ConfigHashClass(HashClass, i);
5641-
56425631
CheckEquals(FTestData[i].ExpectedOutput,
56435632
RawByteStrResult,
56445633
'Index: ' + IntToStr(i) + ' - expected: <' +
@@ -5651,13 +5640,13 @@ procedure THash_TestBase.DoTestCalcRawByteString(HashClass: TDECHash);
56515640

56525641
procedure THash_TestBaseExtended.DoTestCalcStream(HashClass: TDECHashExtended);
56535642
var
5654-
Stream : TMemoryStream;
5655-
i : Integer;
5656-
Buf : TBytes;
5657-
Hash : TBytes;
5658-
ProgressCalled : Boolean;
5659-
5660-
BufSize: Integer;
5643+
Stream : TMemoryStream;
5644+
i : Integer;
5645+
Buf : TBytes;
5646+
Hash : TBytes;
5647+
ProgressCalled : Boolean;
5648+
RawByteStrResult : RawByteString;
5649+
BufSize : Integer;
56615650
begin
56625651
Stream := TMemoryStream.Create;
56635652
BufSize := 0;
@@ -5696,11 +5685,13 @@ procedure THash_TestBaseExtended.DoTestCalcStream(HashClass: TDECHashExtended);
56965685
if (i = FTestData.Count-1) then
56975686
StreamBufferSize := BufSize;
56985687

5688+
RawByteStrResult := BytesToRawString(TFormat_HEXL.Encode(Hash));
5689+
56995690
CheckEquals(FTestData[i].ExpectedOutput,
5700-
BytesToRawString(TFormat_HEXL.Encode(Hash)),
5691+
RawByteStrResult,
57015692
'Index: ' + IntToStr(i) + ' - expected: <' +
57025693
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5703-
string(BytesToRawString(TFormat_HEXL.Encode(Hash))) + '>');
5694+
string(RawByteStrResult) + '>');
57045695

57055696
CheckEquals(true, ProgressCalled, 'Progress event not called');
57065697

@@ -5715,11 +5706,12 @@ procedure THash_TestBaseExtended.DoTestCalcStream(HashClass: TDECHashExtended);
57155706
if (i = FTestData.Count-1) then
57165707
StreamBufferSize := BufSize;
57175708

5709+
RawByteStrResult := BytesToRawString(TFormat_HEXL.Encode(Hash));
57185710
CheckEquals(FTestData[i].ExpectedOutput,
5719-
BytesToRawString(TFormat_HEXL.Encode(Hash)),
5711+
RawByteStrResult,
57205712
'Index: ' + IntToStr(i) + ' - expected: <' +
57215713
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5722-
string(BytesToRawString(TFormat_HEXL.Encode(Hash))) + '>');
5714+
string(RawByteStrResult) + '>');
57235715

57245716
CheckEquals(true, ProgressCalled, 'Progress event not called');
57255717
end;
@@ -5730,12 +5722,13 @@ procedure THash_TestBaseExtended.DoTestCalcStream(HashClass: TDECHashExtended);
57305722

57315723
procedure THash_TestBaseExtended.DoTestCalcStreamNoDone(HashClass: TDECHashExtended);
57325724
var
5733-
Stream : TMemoryStream;
5734-
i : Integer;
5735-
Buf : TBytes;
5736-
Hash : TBytes;
5737-
ProgressCalled : Boolean;
5738-
BufSize : Integer;
5725+
Stream : TMemoryStream;
5726+
i : Integer;
5727+
Buf : TBytes;
5728+
Hash : TBytes;
5729+
ProgressCalled : Boolean;
5730+
RawByteStrResult : RawByteString;
5731+
BufSize : Integer;
57395732
begin
57405733
Stream := TMemoryStream.Create;
57415734
BufSize := 0;
@@ -5776,11 +5769,12 @@ procedure THash_TestBaseExtended.DoTestCalcStreamNoDone(HashClass: TDECHashExten
57765769
if (i = FTestData.Count-1) then
57775770
StreamBufferSize := BufSize;
57785771

5772+
RawByteStrResult := BytesToRawString(TFormat_HEXL.Encode(Hash));
57795773
CheckEquals(FTestData[i].ExpectedOutput,
5780-
BytesToRawString(TFormat_HEXL.Encode(Hash)),
5774+
RawByteStrResult,
57815775
'Index: ' + IntToStr(i) + ' - expected: <' +
57825776
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5783-
string(BytesToRawString(TFormat_HEXL.Encode(Hash))) + '>');
5777+
string(RawByteStrResult) + '>');
57845778

57855779
CheckEquals(true, ProgressCalled, 'Progress event not called');
57865780

@@ -5798,11 +5792,12 @@ procedure THash_TestBaseExtended.DoTestCalcStreamNoDone(HashClass: TDECHashExten
57985792
if (i = FTestData.Count-1) then
57995793
StreamBufferSize := BufSize;
58005794

5795+
RawByteStrResult := BytesToRawString(TFormat_HEXL.Encode(Hash));
58015796
CheckEquals(FTestData[i].ExpectedOutput,
5802-
BytesToRawString(TFormat_HEXL.Encode(Hash)),
5797+
RawByteStrResult,
58035798
'Index: ' + IntToStr(i) + ' - expected: <' +
58045799
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5805-
string(BytesToRawString(TFormat_HEXL.Encode(Hash))) + '>');
5800+
string(RawByteStrResult) + '>');
58065801

58075802
CheckEquals(true, ProgressCalled, 'Progress event not called');
58085803
end;
@@ -5813,15 +5808,16 @@ procedure THash_TestBaseExtended.DoTestCalcStreamNoDone(HashClass: TDECHashExten
58135808

58145809
procedure THash_TestBaseExtended.DoTestCalcStreamNoDoneMulti(HashClass: TDECHashExtended);
58155810
var
5816-
Stream : TMemoryStream;
5817-
i, n : Integer;
5818-
Buf : TBytes;
5819-
Hash : TBytes;
5820-
ProgressCalled : Boolean;
5821-
BufSize : Integer;
5822-
Count : Integer;
5823-
IsLastByte : Boolean;
5824-
Idx, CopyCount : Integer;
5811+
Stream : TMemoryStream;
5812+
i, n : Integer;
5813+
Buf : TBytes;
5814+
Hash : TBytes;
5815+
ProgressCalled : Boolean;
5816+
BufSize : Integer;
5817+
Count : Integer;
5818+
IsLastByte : Boolean;
5819+
Idx, CopyCount : Integer;
5820+
RawByteStrResult : RawByteString;
58255821
begin
58265822
Stream := TMemoryStream.Create;
58275823
BufSize := 0;
@@ -5888,11 +5884,12 @@ procedure THash_TestBaseExtended.DoTestCalcStreamNoDoneMulti(HashClass: TDECHash
58885884
if (i = FTestData.Count-1) then
58895885
StreamBufferSize := BufSize;
58905886

5887+
RawByteStrResult := BytesToRawString(TFormat_HEXL.Encode(Hash));
58915888
CheckEquals(FTestData[i].ExpectedOutput,
5892-
BytesToRawString(TFormat_HEXL.Encode(Hash)),
5889+
RawByteStrResult,
58935890
'Index: ' + IntToStr(i) + ' - expected: <' +
58945891
string(FTestData[i].ExpectedOutput) + '> but was: <' +
5895-
string(BytesToRawString(TFormat_HEXL.Encode(Hash))) + '>');
5892+
string(RawByteStrResult) + '>');
58965893

58975894
CheckEquals(true, ProgressCalled, 'Progress event not called');
58985895
end;

0 commit comments

Comments
 (0)