-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
154 lines (117 loc) · 3.31 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
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
'use strict'
var t = require('tape')
var snap = require('./snap')
var approxEqual = require('almost-equal')
t('snap-points-2d', t => {
function verifySnap(srcPoints) {
var numPoints = srcPoints.length>>>1
var bounds = []
var {levels, ids, weights, points} = snap(srcPoints, bounds)
var npoints = points
var sx = bounds[0]
var sy = bounds[1]
var sw = bounds[2] - bounds[0]
var sh = bounds[3] - bounds[1]
for(var i=0; i < numPoints; ++i) {
var id = ids[i]
t.ok(approxEqual(sx + sw*npoints[2*i], srcPoints[2*id], approxEqual.FLT_EPSILON),
'id perm ok: ' + id + ' ' + srcPoints[2*id] + ' = ' + (sx + sw*npoints[2*i]))
t.ok(approxEqual(sy + sh*npoints[2*i+1], srcPoints[2*id+1], approxEqual.FLT_EPSILON), 'id perm ok: ' + id + ' ' + srcPoints[2*id+1] + ' = ' + (sy + sh*npoints[2*i+1]))
}
t.equals(levels[levels.length-1].offset, 0, 'last item')
t.equals(levels[0].offset+levels[0].count, numPoints, 'first item')
for(var i=0; i < levels.length; ++i) {
var s = levels[i]
var r = s.pixelSize
var offs = s.offset
var count = s.count
console.log('level=', i, r, offs, count)
if(i > 0) {
t.equals(offs+count, levels[i-1].offset, 'offset for ' + i)
t.ok(r < levels[i-1].pixelSize, 'test scales ok')
}
k_loop:
for(var k=offs-1; k>=0; --k) {
var ax = npoints[2*k]
var ay = npoints[2*k+1]
var mind = Infinity
for(var j=offs; j < offs+count; ++j) {
var x = npoints[2*j]
var y = npoints[2*j+1]
mind = Math.min(mind, Math.max(Math.abs(ax-x), Math.abs(ay-y)))
}
t.ok(mind <= 2.0 * r, k + ':' + ax + ',' + ay + ' is not covered - closest pt = ' + mind)
}
}
}
verifySnap([
1, 1,
2, 2,
3, 3,
4, 4,
5, 5
])
verifySnap([
0,0,
0,0,
0,0,
0,0
])
verifySnap([
1, 2,
2, 5,
3, 6,
4, -1
])
var pts = new Array(100)
for(var i=0; i < 100; ++i) {
pts[i] = Math.random()
}
verifySnap(pts)
t.end()
})
t('basics', t => {
let {levels, points, ids, weights} = snap([1,1,2,2,3,3,4,4,5,5])
t.deepEqual(ids, [2, 4, 1, 3, 0])
t.deepEqual(points, [ 0.5, 0.5, 1, 1, 0.25, 0.25, 0.75, 0.75, 0, 0 ])
t.deepEqual(weights, [1, 1, 2, 2, 5])
t.deepEqual(levels, [
{ count: 1, offset: 4, pixelSize: 2 },
{ count: 2, offset: 2, pixelSize: 1 },
{ count: 2, offset: 0, pixelSize: 0.5 }
])
t.end()
})
t('no arguments', t => {
var levels = snap([0,0, 1,1, 2,2])
t.end()
})
t('larger bounds', t => {
var pos = [0,0, 1,1, 2,2, 3,3, 4,4]
var {levels} = snap(pos.slice(), [0,0,4,4])
t.deepEqual(levels, [
{pixelSize: 2, offset: 4, count: 1},
{pixelSize: 1, offset: 2, count: 2},
{pixelSize: 0.5, offset: 0, count: 2}
])
var {levels} = snap(pos.slice(), [0,0,40,40])
t.deepEqual(levels, [
{pixelSize: 20, offset: 4, count: 1},
{pixelSize: 10, offset: 3, count: 1},
{pixelSize: 5, offset: 2, count: 1},
{pixelSize: 2.5, offset: 1, count: 1},
{pixelSize: 1.25, offset: 0, count: 1}
])
t.end()
})
t('performance', t => {
let N = 1e6
let points = new Float64Array(N)
for (let i = 0; i < N; i++) {
points[i] = Math.random()
}
console.time(1)
snap(points)
console.timeEnd(1)
t.end()
})