1 | unit Tests;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, TestCase, Game;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
28 | function GetTestCases: TTestCases;
|
---|
29 |
|
---|
30 |
|
---|
31 | implementation
|
---|
32 |
|
---|
33 | uses
|
---|
34 | Geometry, MapType, Map;
|
---|
35 |
|
---|
36 | function GetTestCases: TTestCases;
|
---|
37 | begin
|
---|
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;
|
---|
45 | end;
|
---|
46 |
|
---|
47 | { TTestCaseGame }
|
---|
48 |
|
---|
49 | procedure TTestCaseGame.Run;
|
---|
50 | const
|
---|
51 | FileName = 'Test.xtg';
|
---|
52 | Size = 10;
|
---|
53 | var
|
---|
54 | Game2: TGame;
|
---|
55 | begin
|
---|
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;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | constructor TTestCaseGame.Create;
|
---|
69 | begin
|
---|
70 | inherited;
|
---|
71 | Game := TGame.Create;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | destructor TTestCaseGame.Destroy;
|
---|
75 | begin
|
---|
76 | FreeAndNil(Game);
|
---|
77 | inherited;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | { TTestCaseMap }
|
---|
81 |
|
---|
82 | procedure TTestCaseMap.Run;
|
---|
83 | var
|
---|
84 | X, Y: Integer;
|
---|
85 | Connected: Boolean;
|
---|
86 | Cell: TCell;
|
---|
87 | Expected: Integer;
|
---|
88 | const
|
---|
89 | Size = 10;
|
---|
90 | begin
|
---|
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);
|
---|
108 | end;
|
---|
109 |
|
---|
110 | constructor TTestCaseMap.Create;
|
---|
111 | begin
|
---|
112 | inherited;
|
---|
113 | Game := TGame.Create;
|
---|
114 | end;
|
---|
115 |
|
---|
116 | destructor TTestCaseMap.Destroy;
|
---|
117 | begin
|
---|
118 | FreeAndNil(Game);
|
---|
119 | inherited;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | end.
|
---|
123 |
|
---|