1 {$INCLUDE ../a_modes.inc}
2 // tests for hash table
3 {$DEFINE EXCESSIVE_CHECKS}
6 hashtbl in '../hashtable.pas';
13 function hequ (constref a, b: Integer): Boolean; begin result := (a = b); end;
15 function hhash (constref k: Integer): LongWord;
17 //result := fnvHash(k, sizeof(k));
18 //result := u32Hash(LongWord(k));
19 result := joaatHash(k, sizeof(k));
25 its: array [0..MaxItems-1] of Integer;
26 marks: array [0..MaxItems-1] of Boolean;
31 procedure checkHash (dump: Boolean=false);
37 if dump then writeln('====== CHECK ======');
38 for i := 0 to High(its) do
43 flag := hash.get(i, v);
44 writeln(' check #', i, '; v=', v, '; flag=', flag);
49 if not hash.has(i) then raise Exception.Create('(0.0) fuuuuuuuuuuuu');
50 if not hash.get(i, v) then raise Exception.Create('(0.1) fuuuuuuuuuuuu');
51 if (v <> its[i]) then raise Exception.Create('(0.2) fuuuuuuuuuuuu');
55 if hash.has(i) then raise Exception.Create('(0.3) fuuuuuuuuuuuu');
58 if (count <> hash.count) then raise Exception.Create('(0.4) fuuuuuuuuuuuu');
59 if dump then writeln('------');
63 procedure testIterator ();
67 function iter (constref k: Integer; constref v: Integer): Boolean;
69 result := false; // don't stop
70 //writeln('key=', k, '; value=', v);
71 if marks[k] then raise Exception.Create('duplicate entry in iterator');
72 if (its[k] <> v) then raise Exception.Create('invalid entry in iterator');
79 for i := 0 to High(marks) do marks[i] := false;
81 if (count <> hash.count) then
83 writeln('0: count=', count, '; hash.count=', hash.count);
84 //raise Exception.Create('lost entries in iterator');
87 for i := 0 to High(marks) do if marks[i] then Inc(count);
88 if (count <> hash.count) then
90 writeln('1: count=', count, '; hash.count=', hash.count);
91 raise Exception.Create('lost entries in iterator');
99 for i := 0 to High(its) do its[i] := -1;
101 //hash := THashInt.Create(hhash, hequ);
102 hash := hashNewIntInt();
106 writeln('testing: insertion');
108 for i := 0 to MaxItems-1 do
110 v := Random(MaxItems);
111 //writeln('i=', i, '; v=', v, '; its[v]=', its[v]);
112 if (its[v] >= 0) then
114 if not hash.has(v) then raise Exception.Create('(1.0) fuuuuuuuuuuuu');
115 if not hash.get(v, hv) then raise Exception.Create('(1.1) fuuuuuuuuuuuu');
116 if (hv <> its[v]) then raise Exception.Create('(1.2) fuuuuuuuuuuuu');
121 if hash.put(v, i) then raise Exception.Create('(1.3) fuuuuuuuuuuuu');
123 if (xcount <> hash.count) then raise Exception.Create('(1.4) fuuuuuuuuuuuu');
125 {$IFDEF EXCESSIVE_CHECKS}checkHash();{$ENDIF}
126 {$IFDEF EXCESSIVE_CHECKS}testIterator();{$ENDIF}
128 if (xcount <> hash.count) then raise Exception.Create('(1.4) fuuuuuuuuuuuu');
132 writeln('testing: deletion');
133 for i := 0 to MaxItems*8 do
135 v := Random(MaxItems);
136 //writeln('trying to delete ', v, '; its[v]=', its[v]);
138 //writeln(' del=', del);
141 if (its[v] < 0) then raise Exception.Create('(2.0) fuuuuuuuuuuuu');
146 if (its[v] >= 0) then raise Exception.Create('(2.1) fuuuuuuuuuuuu');
149 if (xcount <> hash.count) then raise Exception.Create('(1.4) fuuuuuuuuuuuu');
151 if (xcount <> hash.count) then raise Exception.Create('(1.4) fuuuuuuuuuuuu');
152 {$IFDEF EXCESSIVE_CHECKS}checkHash();{$ENDIF}
153 {$IFDEF EXCESSIVE_CHECKS}testIterator();{$ENDIF}
154 if (hash.count = 0) then break;
157 writeln('testing: complete');