2004-12-28 14:43:27 +00:00
|
|
|
unit UTrees;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
TestFramework,
|
|
|
|
X2UtTrees;
|
|
|
|
|
|
|
|
type
|
|
|
|
TBinaryTreeTest = class(TTestCase)
|
|
|
|
private
|
|
|
|
FMemory: Integer;
|
|
|
|
FTree: TX2BinaryTree;
|
|
|
|
protected
|
2005-01-12 11:08:24 +00:00
|
|
|
// If we test the memory usage in SetUp and TearDown, the values are off.
|
|
|
|
// Instead, we manually call these functions to ensure our code is the only
|
|
|
|
// one that gets screened...
|
|
|
|
procedure CustomSetUp();
|
|
|
|
procedure CustomTearDown();
|
2004-12-28 14:43:27 +00:00
|
|
|
|
|
|
|
procedure CheckTree(const AValue: String);
|
|
|
|
published
|
|
|
|
procedure Insert();
|
|
|
|
procedure Delete();
|
|
|
|
procedure Clear();
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
SysUtils;
|
|
|
|
|
|
|
|
|
|
|
|
{ TBinaryTreeTest }
|
2005-01-12 11:08:24 +00:00
|
|
|
procedure TBinaryTreeTest.CustomSetUp;
|
2004-12-28 14:43:27 +00:00
|
|
|
begin
|
2005-01-12 11:08:24 +00:00
|
|
|
FMemory := AllocMemSize;
|
2004-12-28 14:43:27 +00:00
|
|
|
FTree := TX2BinaryTree.Create();
|
|
|
|
FTree.Insert(10);
|
|
|
|
FTree.Insert(25);
|
|
|
|
FTree.Insert(5);
|
|
|
|
FTree.Insert(8);
|
|
|
|
FTree.Insert(16);
|
|
|
|
FTree.Insert(1);
|
|
|
|
end;
|
|
|
|
|
2005-01-12 11:08:24 +00:00
|
|
|
procedure TBinaryTreeTest.CustomTearDown;
|
2004-12-28 14:43:27 +00:00
|
|
|
begin
|
|
|
|
FreeAndNil(FTree);
|
|
|
|
|
2005-01-12 11:08:24 +00:00
|
|
|
CheckEquals(0, AllocMemSize - FMemory, 'Memory leak');
|
2004-12-28 14:43:27 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TBinaryTreeTest.CheckTree;
|
|
|
|
var
|
|
|
|
sTree: String;
|
|
|
|
|
|
|
|
begin
|
|
|
|
sTree := '';
|
|
|
|
|
|
|
|
FTree.First();
|
|
|
|
while not FTree.Eof do
|
|
|
|
begin
|
|
|
|
sTree := sTree + Format('-%d', [FTree.CurrentKey]);
|
|
|
|
FTree.Next();
|
|
|
|
end;
|
|
|
|
|
|
|
|
if Length(sTree) = 0 then
|
|
|
|
Check(Length(AValue) = 0, 'Tree is empty')
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
System.Delete(sTree, 1, 1);
|
|
|
|
CheckEquals(AValue, sTree, 'Tree content is invalid')
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TBinaryTreeTest.Insert;
|
|
|
|
begin
|
2005-01-12 11:08:24 +00:00
|
|
|
CustomSetUp();
|
|
|
|
|
2004-12-28 14:43:27 +00:00
|
|
|
// In these tests we also assume that iterating through the tree is done
|
|
|
|
// from top to bottom, left to right:
|
|
|
|
//
|
|
|
|
// 10
|
|
|
|
// 5 25
|
|
|
|
// 1 8 16
|
|
|
|
CheckTree('10-5-1-8-25-16');
|
2005-01-12 11:08:24 +00:00
|
|
|
|
|
|
|
CustomTearDown();
|
2004-12-28 14:43:27 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TBinaryTreeTest.Delete;
|
|
|
|
begin
|
2005-01-12 11:08:24 +00:00
|
|
|
CustomSetUp();
|
|
|
|
|
|
|
|
// 10
|
|
|
|
// 5 25
|
|
|
|
// 1 16
|
2004-12-28 14:43:27 +00:00
|
|
|
FTree.Delete(8);
|
2005-01-12 11:08:24 +00:00
|
|
|
CheckTree('10-5-1-25-16');
|
2004-12-28 14:43:27 +00:00
|
|
|
|
|
|
|
// 16
|
|
|
|
// 5 25
|
|
|
|
// 1
|
2005-01-12 13:06:05 +00:00
|
|
|
FTree.Delete(10);
|
|
|
|
CheckTree('16-5-1-25');
|
|
|
|
|
|
|
|
// 16
|
|
|
|
// 1 25
|
|
|
|
FTree.Delete(5);
|
|
|
|
CheckTree('16-1-25');
|
|
|
|
|
|
|
|
// 16
|
|
|
|
// 1
|
|
|
|
FTree.Delete(25);
|
|
|
|
CheckTree('16-1');
|
|
|
|
|
|
|
|
// 1
|
|
|
|
FTree.Delete(16);
|
|
|
|
CheckTree('1');
|
|
|
|
|
|
|
|
FTree.Delete(1);
|
|
|
|
CheckTree('');
|
2005-01-12 11:08:24 +00:00
|
|
|
|
|
|
|
CustomTearDown();
|
2004-12-28 14:43:27 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TBinaryTreeTest.Clear;
|
|
|
|
begin
|
2005-01-12 11:08:24 +00:00
|
|
|
CustomSetUp();
|
|
|
|
|
2004-12-28 14:43:27 +00:00
|
|
|
FTree.Clear();
|
|
|
|
CheckTree('');
|
2005-01-12 11:08:24 +00:00
|
|
|
|
|
|
|
CustomTearDown();
|
2004-12-28 14:43:27 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
initialization
|
|
|
|
RegisterTest('Trees.BinaryTree', TBinaryTreeTest.Suite);
|
|
|
|
|
|
|
|
end.
|