source: trunk/Tests.pas

Last change on this file was 343, checked in by chronos, 2 hours ago
  • Modified: Improved tests.
File size: 2.3 KB
Line 
1unit Tests;
2
3interface
4
5uses
6 Classes, SysUtils, TestCase, Game;
7
8type
9
10 { TTestCaseGame }
11
12 TTestCaseGame = class(TTestCase)
13 Game: TGame;
14 procedure Run; override;
15 constructor Create; override;
16 destructor Destroy; override;
17 end;
18
19 { TTestCaseMap }
20
21 TTestCaseMap = class(TTestCase)
22 Game: TGame;
23 procedure Run; override;
24 constructor Create; override;
25 destructor Destroy; override;
26 end;
27
28function GetTestCases: TTestCases;
29
30
31implementation
32
33uses
34 Geometry, MapType, Map;
35
36function GetTestCases: TTestCases;
37begin
38 Result := TTestCases.Create;
39 with Result do begin
40 with TTestCaseGame(AddNew('Load and save', TTestCaseGame)) do begin
41 end;
42 with TTestCaseMap(AddNew('Map cells connection', TTestCaseMap)) do begin
43 end;
44 end;
45end;
46
47{ TTestCaseGame }
48
49procedure TTestCaseGame.Run;
50const
51 FileName = 'Test.xtg';
52 Size = 10;
53var
54 Game2: TGame;
55begin
56 Game.Map.Size := TPoint.Create(Size, Size);
57 Game.MapType := mtSquare;
58 Game.New;
59 Game.SaveToFile(FileName);
60 Game2 := TGame.Create;
61 Game2.LoadFromFile(FileName);
62 Evaluate(Game2.Compare(Game));
63 Log := 'Game1: ' + LineEnding + Game.ToString + LineEnding +
64 'Game2: ' + LineEnding + Game2.ToString + LineEnding;
65 Game2.Free;
66end;
67
68constructor TTestCaseGame.Create;
69begin
70 inherited;
71 Game := TGame.Create;
72end;
73
74destructor TTestCaseGame.Destroy;
75begin
76 FreeAndNil(Game);
77 inherited;
78end;
79
80{ TTestCaseMap }
81
82procedure TTestCaseMap.Run;
83var
84 X, Y: Integer;
85 Connected: Boolean;
86 Cell: TCell;
87 Expected: Integer;
88const
89 Size = 10;
90begin
91 Connected := True;
92 Game.Map.Size := TPoint.Create(Size, Size);
93 Game.MapType := mtSquare;
94 Game.New;
95 for Y := 0 to Size - 1 do
96 for X := 0 to Size - 1 do begin
97 Cell := Game.Map.Cells[X + Y * Size];
98 Expected := 4;
99 if (X = 0) or (X = Size - 1) then Dec(Expected);
100 if (Y = 0) or (Y = Size - 1) then Dec(Expected);
101 if Cell.Neighbors.Count <> Expected then begin
102 Log := Log + 'Cell ' + IntToStr(X + Y * Size) + ' expected neighbors count ' +
103 IntToStr(Expected) + ' but is ' + IntToStr(Cell.Neighbors.Count) + LineEnding;
104 if not Connected then Break;
105 end;
106 end;
107 Evaluate(Connected);
108end;
109
110constructor TTestCaseMap.Create;
111begin
112 inherited;
113 Game := TGame.Create;
114end;
115
116destructor TTestCaseMap.Destroy;
117begin
118 FreeAndNil(Game);
119 inherited;
120end;
121
122end.
123
Note: See TracBrowser for help on using the repository browser.