DEADSOFTWARE

changed license to GPLv3 only; sorry, no trust to FSF anymore
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE a_modes.inc}
16 {$DEFINE IDPOOL_CHECKS}
17 uses
18 SysUtils,
19 idpool in '../idpool.pas';
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);
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 while true do
57 begin
58 n := Random(ip.maxId+1);
59 if map[n] then
60 begin
61 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(0)');
62 if ip.hasFree[n] then raise Exception.Create('invalid pool(1)');
63 continue;
64 end;
65 break;
66 end;
67 if (ip.alloc(n) <> n) then raise Exception.Create('wutafuuuuu?!');
68 map[n] := true;
69 Inc(usedIds);
70 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(3)');
71 if ip.hasFree[n] then raise Exception.Create('invalid pool(4)');
72 //ip.dump();
73 {
74 if ip.hasAlloced[f] then raise Exception.Create('invalid pool(0)');
75 if not ip.hasFree[f] then raise Exception.Create('invalid pool(1)');
76 if (ip.alloc <> f) then raise Exception.Create('invalid alloc(2)');
77 map[f] := true;
78 Inc(usedIds);
79 if not ip.hasAlloced[f] then raise Exception.Create('invalid pool(3)');
80 if ip.hasFree[f] then raise Exception.Create('invalid pool(4)');
81 }
82 end;
83 for f := 0 to 10000000 do
84 begin
85 //if (usedIds = 0) then break;
86 n := Random(ip.maxId+1);
87 if map[n] then
88 begin
89 // allocated, remove
90 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(5)');
91 if ip.hasFree[n] then raise Exception.Create('invalid pool(6)');
92 //ip.dump();
93 ip.release(n);
94 //ip.dump();
95 if ip.hasAlloced[n] then raise Exception.Create(Format('invalid pool(7): %d', [n]));
96 if not ip.hasFree[n] then raise Exception.Create('invalid pool(8)');
97 map[n] := false;
98 Dec(usedIds);
99 end
100 else
101 begin
102 // free, allocate
103 //ip.dump();
104 n := ip.alloc();
105 //ip.dump();
106 if map[n] then raise Exception.Create('invalid pool(9)');
107 if not ip.hasAlloced[n] then raise Exception.Create('invalid pool(a)');
108 if ip.hasFree[n] then raise Exception.Create('invalid pool(b)');
109 map[n] := true;
110 Inc(usedIds);
111 end;
112 end;
113 writeln(usedIds, ' used ids; id has ', ip.usedRanges, ' used ranges out of ', ip.capacity);
114 if (usedIds <> ip.usedIds) then raise Exception.Create('used ids count mismatch');
115 ip.check();
116 for f := 0 to High(map) do
117 begin
118 if map[f] then
119 begin
120 if not ip.hasAlloced[f] then raise Exception.Create('invalid pool(b)');
121 if ip.hasFree[f] then raise Exception.Create('invalid pool(c)');
122 end
123 else
124 begin
125 if ip.hasAlloced[f] then raise Exception.Create('invalid pool(d)');
126 if not ip.hasFree[f] then raise Exception.Create('invalid pool(e)');
127 end;
128 end;
129 ip.Free();
130 end;
133 begin
134 //simpleTest();
135 Randomize();
136 hardTest();
137 end.