delphi 單元測試

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.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末男图,一起剝皮案震驚了整個濱河市讨韭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖褂痰,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異剖膳,居然都是意外死亡碘举,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進(jìn)店門格仲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來押袍,“玉大人,你說我怎么就攤上這事抓狭〔。” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵否过,是天一觀的道長午笛。 經(jīng)常有香客問我,道長苗桂,這世上最難降的妖魔是什么药磺? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮煤伟,結(jié)果婚禮上癌佩,老公的妹妹穿的比我還像新娘。我一直安慰自己便锨,他們只是感情好围辙,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪淑趾。 梳的紋絲不亂的頭發(fā)上慷蠕,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天掸冤,我揣著相機(jī)與錄音厘托,去河邊找鬼。 笑死稿湿,一個胖子當(dāng)著我的面吹牛铅匹,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播饺藤,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼包斑,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了策精?” 一聲冷哼從身側(cè)響起舰始,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎咽袜,沒想到半個月后丸卷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡询刹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年谜嫉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片凹联。...
    茶點(diǎn)故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡沐兰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蔽挠,到底是詐尸還是另有隱情住闯,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布澳淑,位于F島的核電站比原,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏杠巡。R本人自食惡果不足惜量窘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望氢拥。 院中可真熱鬧蚌铜,春花似錦、人聲如沸嫩海。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叁怪。三九已至造壮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間骂束,已是汗流浹背耳璧。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留展箱,地道東北人旨枯。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像混驰,于是被迫代替她去往敵國和親攀隔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容