-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavltree-test.html
288 lines (276 loc) · 8.08 KB
/
avltree-test.html
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
<html>
<head>
<script src="ejui/avltree.js"></script>
</head>
<body>
<div id="vis"></div>
<script>
var the_set = new AVLMap();
function doGet()
{
try
{
alert(JSON.stringify(the_set.get(+document.getElementById('key').value)));
}
catch(e)
{
alert(e+'\n'+e.stack);
}
}
function doSet()
{
try
{
the_set.set(+document.getElementById('key').value, +document.getElementById('value').value);
alert('Value set!');
}
catch(e)
{
alert(e+'\n'+e.stack);
}
}
function doDel()
{
try
{
the_set.del(+document.getElementById('key').value);
alert('Key removed!');
}
catch(e)
{
alert(e+'\n'+e.stack);
}
}
function doIdx()
{
try
{
alert(JSON.stringify(the_set.index(+document.getElementById('key').value)));
}
catch(e)
{
alert(e+'\n'+e.stack);
}
}
function htmlescape(x)
{
return (x+'').split('&').join('&').split('<').join('<').split('>').join('>').split('\n').join('<br/>');
}
function validateNode(x, p)
{
if(x === null)
return true;
if(x.parent !== p)
return false;
var h = 0;
if(x.left && x.left.height > h)
h = x.left.height;
if(x.right && x.right.height > h)
h = x.right.height;
h++;
if(h !== x.height)
return false;
return validateNode(x.left, x) && validateNode(x.right, x);
}
function visualiseNode(x, p)
{
var notes = '';
if(x === null)
return 'null';
if(x.parent !== p)
notes += 'Parent doesn\'t match!\n';
var h = 0;
if(x.left && x.left.height > h)
h = x.left.height;
if(x.right && x.right.height > h)
h = x.right.height;
h++;
if(h !== x.height)
notes += 'Height doesn\'t match!\n';
if((typeof x) === 'object' && (x instanceof AVLTreeNode))
{
try
{
return '<div style="margin: 5px; border: 1px solid black;"><p align="center">'+htmlescape(
'key='+JSON.stringify(x.key)+
', value='+JSON.stringify(x.value)+
', height='+JSON.stringify(x.height)+
', count='+JSON.stringify(x.count)
)+'</p><pre>'+notes+'</pre><table width="100%"><tr><th>left branch:</th><th>right branch:</th></tr><tr><td>'+visualiseNode(x.left, x)+'</td><td>'+visualiseNode(x.right, x)+'</td></tr></table></div>';
}
catch(e)
{
return '<div style="border: 1px solid red;">'+htmlescape('Failed to serialize:\n'+e+'\n'+e.stack);
}
}
else
return 'invalid node: '+x;
}
function doVis()
{
document.getElementById('vis').innerHTML = visualiseNode(the_set.root);
}
function fuzz()
{
var log = '';
var st = new AVLMap();
var lst = [];
try
{
main_loop:
for(;;)
{
var action = Math.floor(6*Math.random());
switch(action)
{
case 0: //insert
var key = Math.floor(11*Math.random())-5;
var value = Math.floor(11*Math.random())-5;
log += 'set('+key+', '+value+');\n';
st.set(key, value);
for(var i = 0; i < lst.length; i++)
if(lst[i].key === key)
{
lst[i].value = value;
continue main_loop;
}
lst.push({key: key, value: value});
break;
case 1: //remove
case 6: //remove by iterator
var key = Math.floor(11*Math.random())-5;
if(action == 1)
{
log += 'del('+key+');\n';
st.del(key);
}
else
{
log += 'get_iter('+key+').pop();\n';
st.get_iter(key).pop();
}
for(var i = 0; i < lst.length; i++)
if(lst[i].key === key)
{
lst.splice(i, 1);
break;
}
break;
case 2: //get
var key = Math.floor(11*Math.random())-5;
log += 'get('+key+');\n';
var val1 = st.get(key);
var val2 = null;
for(var i = 0; i < lst.length; i++)
if(lst[i].key === key)
val2 = lst[i].value;
if(val1 !== val2)
throw "values do not match! ("+val1+" != "+val+")";
break;
case 3: //lower_bound
var key = Math.floor(11*Math.random())-5;
log += 'lower_bound('+key+');\n';
var val1 = st.lower_bound(key);
var val2 = {key: +'Infinity', value: 0};
for(var i = 0; i < lst.length; i++)
if(lst[i].key >= key && lst[i].key < val2.key)
val2 = lst[i];
if(val2.key == 'Infinity')
val2 = null;
val1 = JSON.stringify(val1);
val2 = JSON.stringify(val2);
if(val1 !== val2)
throw "values do not match! ("+val1+" != "+val2+")";
break;
case 4: //index
var key = Math.floor(11*Math.random())-5;
log += 'index('+key+');\n';
var idx1 = st.index(key);
var idx2 = 0;
var found = false;
for(var i = 0; i < lst.length; i++)
if(lst[i].key < key)
idx2++;
else if(lst[i].key == key)
found = true;
if(!found)
idx2 = -1;
if(idx1 !== idx2)
throw "indices do not match! ("+idx1+" != "+val2+")";
break;
case 5: //get_by_index
var idx = Math.floor(15*Math.random());
log += 'get_by_index('+idx+');';
var val1 = st.get_by_index(idx);
var val2 = null;
for(var i = 0; i < lst.length; i++)
{
var idx2 = 0;
for(var j = 0; j < lst.length; j++)
if(lst[j].key < lst[i].key)
idx2++;
if(idx2 == idx)
val2 = lst[i];
}
val1 = JSON.stringify(val1);
val2 = JSON.stringify(val2);
if(val1 !== val2)
throw "values do not match! ("+val1+" != "+val2+")";
break;
default:
throw "unknown action!";
}
if(!validateNode(st.root, null))
throw "node validation failed!";
}
}
catch(e)
{
return '<font color=red>'+htmlescape(e+'\n'+e.stack+'\n'+log)+'</font>';
}
}
function doFuzz()
{
document.getElementById('vis').innerHTML = 'fuzzing...';
var min_len = 1 / 0;
setInterval(function()
{
for(var i = 0; i < 100; i++)
{
var s = fuzz();
if(s.length < min_len)
{
document.getElementById('vis').innerHTML = s;
min_len = s.length;
}
}
}, 1);
}
var lineno = 0;
function doExec()
{
var l = document.getElementById('textarea').value.split('\n')[lineno++];
try
{
document.getElementById('execResult').innerHTML = htmlescape(JSON.stringify(eval("the_set."+l)));
}
catch(e)
{
alert(e+'\n'+e.stack);
}
doVis();
}
</script>
<p><label for="key">Key: </label><input id="key" /></p>
<p><label for="value">Value: </label><input id="value" /></p>
<button onclick="doGet()">Get</button>
<button onclick="doSet()">Set</button>
<button onclick="doDel()">Delete</button>
<button onclick="doIdx()">Index</button>
<button onclick="doVis()">Visualise</button>
<button onclick="doFuzz()">Fuzz</button>
<p><textarea id="textarea"></textarea></p>
<button onclick="doExec()">Step</button>
<div id="execResult"></div>
</body>
</html>