unit xxHash32NormalTest;
interface
uses
DUnitX.TestFramework,
System.SysUtils,
System.Classes,
Vcl.Forms,
xxHash32;
type
[TestFixture]
TxxHash32NormalTest = class(TObject)
protected Const
DictionaryPath = 'Res\xxHash32_Dict.txt';
class var FStringList: TStringList;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
(* &&&&&&&&&&&&&&&&&&&&&&& xxHash32 Test &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
[Test]
procedure xxHashTest32NoRefCountedANSI();
[Test]
procedure xxHashTest32RefCountedANSI();
[Test]
procedure xxHash32SimpleANSI();
[Test]
procedure xxHash32SimpleANSIPointerVersion();
end;
implementation
procedure TxxHash32NormalTest.Setup;
var
ExePath, SubbedPath, FixedPath: String;
begin
ExePath := Application.ExeName;
SubbedPath := ExtractFileDir(ExtractFileDir(ExtractFileDir(ExePath)));
SubbedPath := StringReplace(SubbedPath, 'xxHash.Test', '', [rfReplaceAll]);
FixedPath := SubbedPath + DictionaryPath;
if not FileExists(FixedPath) then
raise Exception.Create('Dictionary File not Found in ' + FixedPath);
FStringList := TStringList.Create;
try
FStringList.LoadFromFile(FixedPath, TEncoding.ANSI);
except
raise Exception.Create('Error Loading Dictionary File');
end;
end;
procedure TxxHash32NormalTest.TearDown;
begin
FStringList.Free;
end;
(* &&&&&&&&&&&&&&&&&&&&&&& xxHash32 Test &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
procedure TxxHash32NormalTest.xxHashTest32NoRefCountedANSI();
var
tempByteArray: TArray<Byte>;
tempStr: String;
hash: TxxHash32;
loopCount: Integer;
tempStrArray: TArray<String>;
tempString: String;
begin
for loopCount := 0 to FStringList.Count - 1 do
begin
tempString := FStringList.Strings[loopCount];
tempStrArray := tempString.Split([':'], None);
tempByteArray := TEncoding.ANSI.GetBytes(tempStrArray[0]);
hash := TxxHash32.Create;
try
hash.Init(); // Initialize with Seed else (0) is used by default
hash.Update(tempByteArray[Low(tempByteArray)], Length(tempByteArray));
tempStr := UIntToStr(hash.Digest);
finally
hash.Free;
end;
Assert.AreEqual(tempStr, tempStrArray[1],
Format('Assertion was called with Hash of %s and Plain Value %s',
[tempStrArray[0], tempStrArray[1]]));
end;
end;
procedure TxxHash32NormalTest.xxHashTest32RefCountedANSI();
var
tempByteArray: TArray<Byte>;
tempStr: String;
hash: IIxxHash32;
loopCount: Integer;
tempStrArray: TArray<String>;
tempString: String;
begin
for loopCount := 0 to FStringList.Count - 1 do
begin
tempString := FStringList.Strings[loopCount];
tempStrArray := tempString.Split([':'], None);
tempByteArray := TEncoding.ANSI.GetBytes(tempStrArray[0]);
hash := TxxHash32.Create;
hash.Init(); // Initialize with Seed else (0) is used by default
hash.Update(tempByteArray[Low(tempByteArray)], Length(tempByteArray));
tempStr := UIntToStr(hash.Digest);
Assert.AreEqual(tempStr, tempStrArray[1],
Format('Assertion was called with Hash of %s and Plain Value %s',
[tempStrArray[0], tempStrArray[1]]));
end;
end;
procedure TxxHash32NormalTest.xxHash32SimpleANSI();
var
tempByteArray: TArray<Byte>;
tempStr: String;
loopCount: Integer;
tempStrArray: TArray<String>;
tempString: String;
begin
for loopCount := 0 to FStringList.Count - 1 do
begin
tempString := FStringList.Strings[loopCount];
tempStrArray := tempString.Split([':'], None);
tempByteArray := TEncoding.ANSI.GetBytes(tempStrArray[0]);
tempStr := UIntToStr(TxxHash32.CalculateHash32(tempByteArray
[Low(tempByteArray)], Length(tempByteArray)));
Assert.AreEqual(tempStr, tempStrArray[1],
Format('Assertion was called with Hash of %s and Plain Value %s',
[tempStrArray[0], tempStrArray[1]]));
end;
end;
procedure TxxHash32NormalTest.xxHash32SimpleANSIPointerVersion();
var
tempByteArray: TArray<Byte>;
tempStr: String;
loopCount: Integer;
tempStrArray: TArray<String>;
tempString: String;
begin
for loopCount := 0 to FStringList.Count - 1 do
begin
tempString := FStringList.Strings[loopCount];
tempStrArray := tempString.Split([':'], None);
tempByteArray := TEncoding.ANSI.GetBytes(tempStrArray[0]);
tempStr := UIntToStr(TxxHash32.CalculateHash32(Pointer(tempByteArray)^,
Length(tempByteArray)));
Assert.AreEqual(tempStr, tempStrArray[1],
Format('Assertion was called with Hash of %s and Plain Value %s',
[tempStrArray[0], tempStrArray[1]]));
end;
end;
initialization
TDUnitX.RegisterTestFixture(TxxHash32NormalTest);
end.
delphi 单元测试
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 系列教程 在Android Studio中进行单元测试和UI测试 - 1.概述 在Android Studio中进...
- 系列教程 在Android Studio中进行单元测试和UI测试 - 1.概述 在Android Studio中进...
- 系列教程 在Android Studio中进行单元测试和UI测试 - 1.概述 在Android Studio中进...
- 本节将涵盖Angular常用的组件单元测试方法,例如:Router、Component、Directive、Pip...