DEADSOFTWARE

393c43cf1c34fae1d27b008cc32eadb419d13de8
[d2df-sdl.git] / src / shared / ztest_idpool.dpr
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE a_modes.inc}
17 {$DEFINE IDPOOL_CHECKS}
18 uses
19 SysUtils, idpool;
22 // ////////////////////////////////////////////////////////////////////////// //
23 procedure simpleTest ();
24 var
25 ip: TIdPool;
26 begin
27 ip := TIdPool.Create();
28 writeln(ip.alloc); ip.dump();
29 writeln(ip.alloc); ip.dump();
30 writeln(ip.alloc); ip.dump();
31 writeln(ip.alloc); ip.dump();
32 writeln(ip.alloc); ip.dump();
33 writeln(ip.alloc); ip.dump();
34 ip.release(2); ip.dump();
35 ip.release(4); ip.dump();
36 ip.release(0); ip.dump();
37 ip.release(1); ip.dump();
38 ip.release(3); ip.dump();
39 ip.release(5); ip.dump();
40 end;
43 // ////////////////////////////////////////////////////////////////////////// //
44 procedure hardTest ();
45 var
46 ip: TIdPool;
47 map: array of Boolean = nil;
48 f, n: Integer;
49 usedIds: Integer = 0;
50 begin
51 ip := TIdPool.Create(65535*1024);
52 SetLength(map, ip.maxId+1);
53 for f := 0 to High(map) do map[f] := false;
54 for f := 0 to High(map) div 2 do
55 begin
56 if ip.hasAlloced[f] then raise Exception.Create('invalid pool(0)');
57 if not ip.hasFree[f] then raise Exception.Create('invalid pool(1)');
58 if (ip.alloc <> f) then raise Exception.Create('invalid alloc(2)');
59 map[f] := true;
60 Inc(usedIds);
61 if not ip.hasAlloced[f] then raise Exception.Create('invalid pool(3)');
62 if ip.hasFree[f] then raise Exception.Create('invalid pool(4)');
63 end;
64 for f := 0 to 10000000 do
65 begin
66 //if (usedIds = 0) then break;
67 n := Random(ip.maxId+1);
68 if map[n] then
69 begin
70 // allocated, remove
71 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(5)');
72 if ip.hasFree[n] then raise Exception.Create('invalid pool(6)');
73 //ip.dump();
74 ip.release(n);
75 //ip.dump();
76 if ip.hasAlloced[n] then raise Exception.Create(Format('invalid pool(7): %d', [n]));
77 if not ip.hasFree[n] then raise Exception.Create('invalid pool(8)');
78 map[n] := false;
79 Dec(usedIds);
80 end
81 else
82 begin
83 // free, allocate
84 //ip.dump();
85 n := ip.alloc();
86 //ip.dump();
87 if map[n] then raise Exception.Create('invalid pool(9)');
88 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(a)');
89 if ip.hasFree[n] then raise Exception.Create('invalid pool(b)');
90 map[n] := true;
91 Inc(usedIds);
92 end;
93 end;
94 writeln(usedIds, ' used ids; id has ', ip.usedRanges, ' used ranges out of ', ip.capacity);
95 if (usedIds <> ip.usedIds) then raise Exception.Create('used ids count mismatch');
96 ip.check();
97 for f := 0 to High(map) do
98 begin
99 if map[f] then
100 begin
101 if not ip.hasAlloced[f] then raise Exception.Create('invalid pool(b)');
102 if ip.hasFree[f] then raise Exception.Create('invalid pool(c)');
103 end
104 else
105 begin
106 if ip.hasAlloced[f] then raise Exception.Create('invalid pool(d)');
107 if not ip.hasFree[f] then raise Exception.Create('invalid pool(e)');
108 end;
109 end;
110 end;
113 begin
114 //simpleTest();
115 Randomize();
116 hardTest();
117 end.