DEADSOFTWARE

4938eb5252cbf61deeaf0a616727aeb0e20de745
[d2df-sdl.git] / src / shared / tests / test_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,
20 idpool in '../idpool.pas';
23 // ////////////////////////////////////////////////////////////////////////// //
24 procedure simpleTest ();
25 var
26 ip: TIdPool;
27 begin
28 ip := TIdPool.Create();
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 writeln(ip.alloc); ip.dump();
35 ip.release(2); ip.dump();
36 ip.release(4); ip.dump();
37 ip.release(0); ip.dump();
38 ip.release(1); ip.dump();
39 ip.release(3); ip.dump();
40 ip.release(5); ip.dump();
41 end;
44 // ////////////////////////////////////////////////////////////////////////// //
45 procedure hardTest ();
46 var
47 ip: TIdPool;
48 map: array of Boolean = nil;
49 f, n: Integer;
50 usedIds: Integer = 0;
51 begin
52 ip := TIdPool.Create(65535);
53 SetLength(map, ip.maxId+1);
54 for f := 0 to High(map) do map[f] := false;
55 for f := 0 to High(map) div 2 do
56 begin
57 while true do
58 begin
59 n := Random(ip.maxId+1);
60 if map[n] then
61 begin
62 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(0)');
63 if ip.hasFree[n] then raise Exception.Create('invalid pool(1)');
64 continue;
65 end;
66 break;
67 end;
68 if (ip.alloc(n) <> n) then raise Exception.Create('wutafuuuuu?!');
69 map[n] := true;
70 Inc(usedIds);
71 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(3)');
72 if ip.hasFree[n] then raise Exception.Create('invalid pool(4)');
73 //ip.dump();
74 {
75 if ip.hasAlloced[f] then raise Exception.Create('invalid pool(0)');
76 if not ip.hasFree[f] then raise Exception.Create('invalid pool(1)');
77 if (ip.alloc <> f) then raise Exception.Create('invalid alloc(2)');
78 map[f] := true;
79 Inc(usedIds);
80 if not ip.hasAlloced[f] then raise Exception.Create('invalid pool(3)');
81 if ip.hasFree[f] then raise Exception.Create('invalid pool(4)');
82 }
83 end;
84 for f := 0 to 10000000 do
85 begin
86 //if (usedIds = 0) then break;
87 n := Random(ip.maxId+1);
88 if map[n] then
89 begin
90 // allocated, remove
91 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(5)');
92 if ip.hasFree[n] then raise Exception.Create('invalid pool(6)');
93 //ip.dump();
94 ip.release(n);
95 //ip.dump();
96 if ip.hasAlloced[n] then raise Exception.Create(Format('invalid pool(7): %d', [n]));
97 if not ip.hasFree[n] then raise Exception.Create('invalid pool(8)');
98 map[n] := false;
99 Dec(usedIds);
100 end
101 else
102 begin
103 // free, allocate
104 //ip.dump();
105 n := ip.alloc();
106 //ip.dump();
107 if map[n] then raise Exception.Create('invalid pool(9)');
108 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(a)');
109 if ip.hasFree[n] then raise Exception.Create('invalid pool(b)');
110 map[n] := true;
111 Inc(usedIds);
112 end;
113 end;
114 writeln(usedIds, ' used ids; id has ', ip.usedRanges, ' used ranges out of ', ip.capacity);
115 if (usedIds <> ip.usedIds) then raise Exception.Create('used ids count mismatch');
116 ip.check();
117 for f := 0 to High(map) do
118 begin
119 if map[f] then
120 begin
121 if not ip.hasAlloced[f] then raise Exception.Create('invalid pool(b)');
122 if ip.hasFree[f] then raise Exception.Create('invalid pool(c)');
123 end
124 else
125 begin
126 if ip.hasAlloced[f] then raise Exception.Create('invalid pool(d)');
127 if not ip.hasFree[f] then raise Exception.Create('invalid pool(e)');
128 end;
129 end;
130 ip.Free();
131 end;
134 begin
135 //simpleTest();
136 Randomize();
137 hardTest();
138 end.