⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testalgorithms.pas

📁 在Delphi上实现的数据结构
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit TestAlgorithms;

interface
uses
  Classes, SysUtils,
  TestFramework,
  TestExtensions,
  DGL_Pointer,DGL_Integer,DGL_String,DGL_Interface,
  _DGL_Object,_DGL_Point;

type

  TTest_Algorithms = class(TTestCase)
  private
    FSumCount : integer;
    FFindIfValue : integer;
    procedure SumCount(const x:integer);
    function  FindIf1b(const x:integer):boolean;
    function  FindIf2b(const x0,x1:integer):boolean;
    function  Search2b(const x0,x1:integer):boolean;
    function  Less(const x0,x1:integer):boolean;
    function  great(const x0,x1:integer):boolean;
    function  TansfromAdd1(const x:integer):integer;
    function TansfromAdd(const x0, x1: integer): integer;
    function Get10(): integer;
    function RandomGenerate(const Range: Integer): integer;
  public
    procedure Setup; override;
    procedure TearDown; override;
  published
    procedure testAlgorithms0();
    procedure testAlgorithms1();
    procedure testAlgorithms2();
    procedure testAlgorithms3();
    procedure testAlgorithms4();
    procedure testAlgorithms5();
  end;
implementation


{ TTest_Algorithms }

function TTest_Algorithms.FindIf1b(const x: integer): boolean;
begin
  result:=(x=FFindIfValue);
end;

function TTest_Algorithms.FindIf2b(const x0, x1: integer): boolean;
begin
  result:=(FFindIfValue=x0*x1);
end;

function TTest_Algorithms.Less(const x0, x1: integer): boolean;
begin
  result:=(x0<x1);
end;

function TTest_Algorithms.Search2b(const x0, x1: integer): boolean;
begin
  result:=(x0=x1);
end;

procedure TTest_Algorithms.Setup;
begin
  inherited;

end;

procedure TTest_Algorithms.SumCount(const x: integer);
begin
  inc(FSumCount,x);
end;

function TTest_Algorithms.TansfromAdd1(const x: integer): integer;
begin
  result:=x+1;
end;

function gpTansfromAdd1(const x: integer): integer;
begin
  result:=x+1;
end;

function TTest_Algorithms.TansfromAdd(const x0,x1: integer): integer;
begin
  result:=x0+x1;
end;

function gpTansfromAdd(const x0,x1: integer): integer;
begin
  result:=x0+x1;
end;

function gpRandomGenerate(const Range: Integer):integer;
 //0<=result<Range
begin
  result:=abs(Random(Range*1242341)*123567 mod Range);
end;

function TTest_Algorithms.RandomGenerate(const Range: Integer):integer;
 //0<=result<Range
begin
  result:=abs(Random(Range*1242341)*123567 mod Range);
end;


procedure TTest_Algorithms.TearDown;
begin
  inherited;

end;

var
  gSumCount : integer;
  procedure gpSumCount(const x:integer);
  begin
    inc(gSumCount,x);
  end;

var
  gFindIfValue:integer;
  function gpFindIf1b(const x:integer):boolean;
  begin
    result:=(gFindIfValue=x);
  end;
  function gpFindIf2b(const x0,x1:integer):boolean;
  begin
    result:=(gFindIfValue=x0*x1);
  end;


procedure TTest_Algorithms.testAlgorithms0;
const
  num = 50;
var
  IntVector : IIntVector;
  IntVector1 : IIntVector;
  it,it1 :IIntIterator;
  i: integer;
begin
  //test Distance\IsEquals\ForEach\Find

  IntVector :=TIntVector.Create();
  for i:=0 to num-1 do
    IntVector.PushBack(i);


  //Distance
  CheckEquals(IntVector.Size(),
    TIntAlgorithms.Distance(IntVector.ItBegin,IntVector.ItEnd));

  //IsEquals
  IntVector1:=IIntVector(IntVector.Clone());
  CheckEquals(true,TIntAlgorithms.IsEquals(IntVector.ItBegin,IntVector.ItEnd,IntVector1.ItBegin,IntVector1.ItEnd));
  IntVector1.Items[2]:=7;
  CheckEquals(false,TIntAlgorithms.IsEquals(IntVector.ItBegin,IntVector.ItEnd,IntVector1.ItBegin,IntVector1.ItEnd));
  IntVector1:=nil;

  //ForEach
  FSumCount:=0;
  TIntAlgorithms.ForEach(IntVector.ItBegin,IntVector.ItEnd,self.SumCount);
  CheckEquals(FSumCount,(0+(num-1))*num div 2);
  gSumCount:=0;
  TIntAlgorithms.ForEach(IntVector.ItBegin,IntVector.ItEnd,gpSumCount);
  CheckEquals(gSumCount,(0+(num-1))*num div 2);

  //Find
  it:=TIntAlgorithms.Find(IntVector.ItBegin,IntVector.ItEnd,3);
  CheckEquals(it.Value,3);
  it:=TIntAlgorithms.Find(IntVector.ItBegin,IntVector.ItEnd,-1);
  CheckEquals(true,it.IsEqual(IntVector.ItEnd));

  //FindIf 1b
  self.FFindIfValue:=7;
  it:=TIntAlgorithms.FindIf(IntVector.ItBegin,IntVector.ItEnd,self.FindIf1b);
  CheckEquals(it.Value,FFindIfValue);
  gFindIfValue:=7;
  it:=TIntAlgorithms.FindIf(IntVector.ItBegin,IntVector.ItEnd,gpFindIf1b);
  CheckEquals(it.Value,gFindIfValue);

  //FindIf 2b
  self.FFindIfValue:=30;
  it:=TIntAlgorithms.FindIf(IntVector.ItBegin,IntVector.ItEnd,6,self.FindIf2b);
  CheckEquals(it.Value*6,FFindIfValue);
  gFindIfValue:=28;
  it:=TIntAlgorithms.FindIf(IntVector.ItBegin,IntVector.ItEnd,7,gpFindIf2b);
  CheckEquals(it.Value*7,gFindIfValue);
end;

    function  gpSearch2b(const x0,x1:integer):boolean;
    begin
      result:=(x0=x1);
    end;

    function gpLess(const x0, x1: integer): boolean;
    begin
      result:=(x0<x1);
    end;


procedure TTest_Algorithms.testAlgorithms1;
const
  num = 100;
var
  IntVector : IIntVector;
  IntVector1 : IIntVector;
  it,it1 :IIntIterator;
  i,j: integer;
begin
  //test Count\Search\MinElement\MaxElement

  IntVector :=TIntVector.Create();
  for i:=0 to num-1 do
    for j:=0 to i-1 do
      IntVector.PushBack(i);
  TIntAlgorithms.RandomShuffle(IntVector.ItBegin,IntVector.ItEnd);

  //Count
  CheckEquals(7,TIntAlgorithms.Count(IntVector.ItBegin,IntVector.ItEnd,7));
  CheckEquals(2,TIntAlgorithms.Count(IntVector.ItBegin,IntVector.ItEnd,2));

  //CountIf
  FFindIfValue:=5;
  CheckEquals(5,TIntAlgorithms.CountIf(IntVector.ItBegin,IntVector.ItEnd,self.FindIf1b));
  gFindIfValue:=9;
  CheckEquals(9,TIntAlgorithms.CountIf(IntVector.ItBegin,IntVector.ItEnd,gpFindIf1b));

  //Search
  IntVector.Clear();
  for i:=0 to num-1 do
      IntVector.PushBack(i);
  IntVector1 :=TIntVector.Create(IntVector.ItBegin.Clone(5),IntVector.ItBegin.Clone(10));
  it:=TIntAlgorithms.Search(IntVector.ItBegin,IntVector.ItEnd,IntVector1.ItBegin,IntVector1.ItEnd);
  CheckEquals(5,it.Value);
  CheckEquals(true,TIntAlgorithms.IsEquals(IntVector1.ItBegin,IntVector1.ItEnd,it,it.Clone(IntVector1.Size)));
  IntVector1.Items[2]:=-1;
  it:=TIntAlgorithms.Search(IntVector.ItBegin,IntVector.ItEnd,IntVector1.ItBegin,IntVector1.ItEnd);
  CheckEquals(true,it.IsEqual(IntVector.ItEnd));

  //Search if
  IntVector.Clear();
  for i:=0 to num-1 do
      IntVector.PushBack(i);
  IntVector1 :=TIntVector.Create(IntVector.ItBegin.Clone(7),IntVector.ItBegin.Clone(9));
  it:=TIntAlgorithms.Search(IntVector.ItBegin,IntVector.ItEnd,IntVector1.ItBegin,IntVector1.ItEnd,self.Search2b);
  CheckEquals(7,it.Value);
  IntVector1 :=TIntVector.Create(IntVector.ItBegin.Clone(2),IntVector.ItBegin.Clone(6));
  it:=TIntAlgorithms.Search(IntVector.ItBegin,IntVector.ItEnd,IntVector1.ItBegin,IntVector1.ItEnd,gpSearch2b);
  CheckEquals(2,it.Value);

  //MinElement
  TIntAlgorithms.RandomShuffle(IntVector.ItBegin,IntVector.ItEnd);
  it:=TIntAlgorithms.MinElement(IntVector.ItBegin,IntVector.ItEnd);
  CheckEquals(0,it.Value);
  it:=TIntAlgorithms.MaxElement(IntVector.ItBegin,IntVector.ItEnd);
  CheckEquals(num-1,it.Value);
  it:=TIntAlgorithms.MinElement(IntVector.ItBegin,IntVector.ItEnd,self.Less);
  CheckEquals(0,it.Value);
  it:=TIntAlgorithms.MaxElement(IntVector.ItBegin,IntVector.ItEnd,self.Less);
  CheckEquals(num-1,it.Value);
  it:=TIntAlgorithms.MinElement(IntVector.ItBegin,IntVector.ItEnd,gpLess);
  CheckEquals(0,it.Value);
  it:=TIntAlgorithms.MaxElement(IntVector.ItBegin,IntVector.ItEnd,gpLess);
  CheckEquals(num-1,it.Value);


end;

procedure TTest_Algorithms.testAlgorithms2;
const
  num = 100;
var
  IntVector : IIntVector;
  IntVector1,IntVector2 : IIntVector;
  it,it1 :IIntIterator;
  i,j: integer;
begin
  //test SwapValue\Copy\Tansfrom\SwapRanges

  IntVector :=TIntVector.Create();
  for i:=0 to num-1 do
      IntVector.PushBack(i);

  //SwapValue
  TIntAlgorithms.SwapValue(IntVector.ItBegin.Clone(3),IntVector.ItBegin.Clone(7));
  CheckEquals(7,IntVector.Items[3]);
  CheckEquals(3,IntVector.Items[7]);
  TIntAlgorithms.SwapValue(IntVector.ItBegin,3,IntVector.ItBegin,7);
  CheckEquals(3,IntVector.Items[3]);
  CheckEquals(7,IntVector.Items[7]);
  
  //Copy

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -