forked from shinout/interval-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
80 lines (66 loc) · 1.48 KB
/
test.js
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
var IntervalTree = require('./IntervalTree');
function test() {
function createRandomInterval(unit, id) {
var p1 = Math.floor(Math.random() * unit);
var p2 = Math.floor(Math.random() * unit);
if (p1 === p2) {
if (p1 > 0 ) {
p1--;
}
else {
p2++;
}
}
return [Math.min(p1,p2), Math.max(p1,p2), id];
}
var iTree = new IntervalTree(500);
var intervals = (function() {
var ret = [];
for (var i=0; i<100; i++) ret.push(createRandomInterval(1000, i));
return ret;
})();
intervals.forEach(function(val) {
iTree.add(val, val[2]);
});
/**
* point search
**/
var results = iTree.search(500);
var resultIds = results.map(function(val) {
return Number(val.id);
});
intervals.forEach(function(v, k) {
if (resultIds.indexOf(k) >= 0) {
if (v[0] <= 500 && v[1] >= 500) {
console.log('true positive', v);
}
else {
throw new Error('false positive');
}
}
else {
if (v[0] <= 500 && v[1] >= 500) {
throw new Error('false negative');
}
else {
console.log('true negative', v);
}
}
});
/**
* range search
**/
results = iTree.search(500, 700);
/**
* range search evaluation
**/
results.forEach(function(result) {
if (result.rate1 == 0 && result.rate2 == 0) {
throw new Error('false positive');
}
});
// TODO : detect false negative
}
if (process.argv[1] == __filename) {
test();
}