File: testavglvltree.pas

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (321 lines) | stat: -rw-r--r-- 8,002 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{
 Test all with:
     ./runtests --format=plain --suite=TTest_AvgLvlTree
     ./runtests --format=plain --suite=TTest_AVLTree

 Test specific with:
     ./runtests --format=plain --suite=TestAvgLvlTreeAddsDeletes
     ./runtests --format=plain --suite=TestIndexedAVLTreeAddsDeletes
     ./runtests --format=plain --suite=TestAVLTreeAddsDeletes
}
unit TestAvgLvlTree;

{$mode objfpc}{$H+}

{ $DEFINE VerboseTestSequence}

interface

uses
  Classes, SysUtils, fpcunit, testglobals, LazLogger,
  AVL_Tree, // the unit from FPC
  // Laz_AVL_Tree, the unit copied from FPC when compiling Lazarus for older compilers
  AvgLvlTree // unit from LazUtils, an extended version
  ;

type
  { TTest_AvgLvlTree - the LazUtils extensions }

  TTest_AvgLvlTree = class(TTestCase)
  private
    fTreeClass: AvgLvlTree.TAvgLvlTreeClass;
    function CreateTree(Args: array of const): AvgLvlTree.TAvgLvlTree;
    procedure TestSequence(Args: array of const);
    procedure TestAscendingSequence(InitArgs: array of const; AscSeq: array of const);
    procedure TestAvgLvlTree;
  published
    procedure TestAvgLvlTreeAddsDeletes;
    procedure TestIndexedAVLTreeAddsDeletes;
  end;

  {$IF FPC_FULLVERSION<30101}
  TAVLTreeClass = class of TAVLTree;
  {$ENDIF}

  { TTest_AVLTree - the FPC unit}

  TTest_AVLTree = class(TTestCase)
  private
    fTreeClass: TAVLTreeClass;
    function CreateTree(Args: array of const): TAVLTree;
    procedure TestSequence(Args: array of const);
    {$IF FPC_FULLVERSION>=30101}
    procedure TestAscendingSequence(InitArgs: array of const; AscSeq: array of const);
    {$ENDIF}
    procedure TestAVLTree;
  published
    procedure TestAVLTreeAddsDeletes;
  end;

implementation

{ TTest_AVLTree }

function TTest_AVLTree.CreateTree(Args: array of const): TAVLTree;
var
  i: Integer;
  Value: LongInt;
begin
  Result:=fTreeClass.Create;
  //DebugLn(Result.ReportAsString);
  Result.ConsistencyCheck;

  for i:=Low(Args) to high(Args) do begin
    if Args[i].VType<>vtInteger then continue;
    Value:=Args[i].vinteger;
    if Value>0 then begin
      {$IFDEF VerboseTestSequence}
      DebugLn(['  add value ',Value]);
      {$ENDIF}
      Result.Add({%H-}Pointer(Value));
    end else begin
      Value:=-Value;
      {$IFDEF VerboseTestSequence}
      debugln(['  remove value ',Value]);
      {$ENDIF}
      Result.Remove({%H-}Pointer(Value));
    end;
    {$IFDEF VerboseTestSequence}
    DebugLn(Result.ReportAsString);
    {$ENDIF}
    Result.ConsistencyCheck;
  end;
end;

procedure TTest_AVLTree.TestSequence(Args: array of const);
var
  Tree: AVL_Tree.TAVLTree;
begin
  Tree:=CreateTree(Args);
  Tree.Clear;
  //DebugLn(Tree.ReportAsString);
  Tree.ConsistencyCheck;
  Tree.Free;
end;

{$IF FPC_FULLVERSION>=30101}
procedure TTest_AVLTree.TestAscendingSequence(InitArgs: array of const;
  AscSeq: array of const);
var
  Tree: AVL_Tree.TAVLTree;
  LastAdded, Succesor: AVL_Tree.TAVLTreeNode;
  i: Integer;
  Value: LongInt;
begin
  Tree:=CreateTree(InitArgs);

  LastAdded:=nil;
  Succesor:=nil;
  for i:=Low(AscSeq) to high(AscSeq) do begin
    if AscSeq[i].VType<>vtInteger then continue;
    Value:=AscSeq[i].vinteger;
    {$IFDEF VerboseTestSequence}
    DebugLn(['  add ascending value ',Value]);
    {$ENDIF}
    LastAdded:=Tree.AddAscendingSequence({%H-}Pointer(Value),LastAdded,Succesor);
    {$IFDEF VerboseTestSequence}
    DebugLn(Tree.ReportAsString);
    {$ENDIF}
    Tree.ConsistencyCheck;
  end;

  Tree.Clear;
  //DebugLn(Tree.ReportAsString);
  Tree.ConsistencyCheck;
  Tree.Free;
end;
{$ENDIF}

procedure TTest_AVLTree.TestAVLTree;
begin
  // rotate left
  TestSequence([]);
  TestSequence([1]);
  TestSequence([1,2]);
  TestSequence([1,2,3]);
  TestSequence([1,2,3,4]);
  TestSequence([1,2,3,4,5]);
  TestSequence([1,2,3,4,5,6]);
  TestSequence([1,2,3,4,5,6,7,8,9,10]);

  // rotate right
  TestSequence([10,9,8,7,6,5,4,3,2,1]);

  // double rotate right, left
  TestSequence([5,7,6]);

  // double rotate left, right
  TestSequence([5,3,4]);

  // test deletes
  TestSequence([1,2,3,-1,-2,-3]);
  TestSequence([1,2,3,-1,-3,-2]);
  TestSequence([1,2,3,-2,-1,-3]);
  TestSequence([1,2,3,-2,-3,-1]);
  TestSequence([1,2,3,-3,-1,-2]);
  TestSequence([1,2,3,-3,-2,-1]);

  {$IF FPC_FULLVERSION>=30101}
  // test AddAscendingSequence
  TestAscendingSequence([],[1]);
  TestAscendingSequence([],[1,2]);
  TestAscendingSequence([],[1,2,3]);
  TestAscendingSequence([],[2,1]);
  TestAscendingSequence([],[3,2,1]);
  TestAscendingSequence([1],[2,3,4,5]);
  TestAscendingSequence([2],[1,3,4,5]);
  TestAscendingSequence([3],[1,2,4,5,6]);
  TestAscendingSequence([3,4],[1,2,5,6,7]);
  {$ENDIF}
end;

procedure TTest_AVLTree.TestAVLTreeAddsDeletes;
begin
  fTreeClass:=AVL_Tree.TAVLTree;
  TestAVLTree;
end;

{ TTest_AvgLvlTree }

function TTest_AvgLvlTree.CreateTree(Args: array of const): AvgLvlTree.TAvgLvlTree;
var
  i: Integer;
  Value: LongInt;
begin
  Result:=fTreeClass.Create;
  //DebugLn(Result.ReportAsString);
  Result.ConsistencyCheck;

  for i:=Low(Args) to high(Args) do begin
    if Args[i].VType<>vtInteger then continue;
    Value:=Args[i].vinteger;
    if Value>0 then begin
      {$IFDEF VerboseTestSequence}
      DebugLn(['  add value ',Value]);
      {$ENDIF}
      Result.Add({%H-}Pointer(Value));
    end else begin
      Value:=-Value;
      {$IFDEF VerboseTestSequence}
      debugln(['  remove value ',Value]);
      {$ENDIF}
      Result.Remove({%H-}Pointer(Value));
    end;
    {$IFDEF VerboseTestSequence}
    DebugLn(Result.ReportAsString);
    {$ENDIF}
    Result.ConsistencyCheck;
  end;
end;

procedure TTest_AvgLvlTree.TestSequence(Args: array of const);
var
  Tree: AvgLvlTree.TAvgLvlTree;
begin
  Tree:=CreateTree(Args);
  Tree.Clear;
  //DebugLn(Tree.ReportAsString);
  Tree.ConsistencyCheck;
  Tree.Free;
end;

procedure TTest_AvgLvlTree.TestAscendingSequence(InitArgs: array of const;
  AscSeq: array of const);
var
  Tree: AvgLvlTree.TAvgLvlTree;
  LastAdded, Succesor: AvgLvlTree.TAvgLvlTreeNode;
  i: Integer;
  Value: LongInt;
begin
  Tree:=CreateTree(InitArgs);

  LastAdded:=nil;
  Succesor:=nil;
  for i:=Low(AscSeq) to high(AscSeq) do begin
    if AscSeq[i].VType<>vtInteger then continue;
    Value:=AscSeq[i].vinteger;
    {$IFDEF VerboseTestSequence}
    DebugLn(['  add ascending value ',Value]);
    {$ENDIF}
    LastAdded:=Tree.AddAscendingSequence({%H-}Pointer(Value),LastAdded,Succesor);
    {$IFDEF VerboseTestSequence}
    DebugLn(Tree.ReportAsString);
    {$ENDIF}
    Tree.ConsistencyCheck;
  end;

  Tree.Clear;
  //DebugLn(Tree.ReportAsString);
  Tree.ConsistencyCheck;
  Tree.Free;
end;

procedure TTest_AvgLvlTree.TestAvgLvlTree;
begin
  // rotate left
  TestSequence([]);
  TestSequence([1]);
  TestSequence([1,2]);
  TestSequence([1,2,3]);
  TestSequence([1,2,3,4]);
  TestSequence([1,2,3,4,5]);
  TestSequence([1,2,3,4,5,6]);
  TestSequence([1,2,3,4,5,6,7,8,9,10]);

  // rotate right
  TestSequence([10,9,8,7,6,5,4,3,2,1]);

  // double rotate right, left
  TestSequence([5,7,6]);

  // double rotate left, right
  TestSequence([5,3,4]);

  // test deletes
  TestSequence([1,2,3,-1,-2,-3]);
  TestSequence([1,2,3,-1,-3,-2]);
  TestSequence([1,2,3,-2,-1,-3]);
  TestSequence([1,2,3,-2,-3,-1]);
  TestSequence([1,2,3,-3,-1,-2]);
  TestSequence([1,2,3,-3,-2,-1]);

  // test AddAscendingSequence
  TestAscendingSequence([],[1]);
  TestAscendingSequence([],[1,2]);
  TestAscendingSequence([],[1,2,3]);
  TestAscendingSequence([],[2,1]);
  TestAscendingSequence([],[3,2,1]);
  TestAscendingSequence([1],[2,3,4,5]);
  TestAscendingSequence([2],[1,3,4,5]);
  TestAscendingSequence([3],[1,2,4,5,6]);
  TestAscendingSequence([3,4],[1,2,5,6,7]);
end;

procedure TTest_AvgLvlTree.TestAvgLvlTreeAddsDeletes;
begin
  fTreeClass:=AvgLvlTree.TAvgLvlTree;
  TestAvgLvlTree;
end;

procedure TTest_AvgLvlTree.TestIndexedAVLTreeAddsDeletes;
begin
  fTreeClass:=AvgLvlTree.TIndexedAVLTree;
  TestAvgLvlTree;
end;

initialization
  AddToLazUtilsTestSuite(TTest_AvgLvlTree);
  AddToLazUtilsTestSuite(TTest_AVLTree);

end.