This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtest.js
293 lines (269 loc) · 8.87 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
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
289
290
291
292
293
var assert = require("chai").assert,
binary = require("./lib/binary"),
spherekd = require("./lib/spherekd")
describe("sphere-knn", function() {
describe("binary", function() {
describe("search", function() {
var array = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61
]
it("should return the correct index of the first item", function() {
assert.equal(binary.search(2, array), 0)
})
it("should return the correct index of the last item", function() {
assert.equal(binary.search(61, array), 17)
})
it("should return the correct index of an arbitrary item", function() {
assert.equal(binary.search(23, array), 8)
})
it("should return the correct index of a missing item before the first", function() {
assert.equal(binary.search(1, array), -1)
})
it("should return the correct index of a missing item after the last", function() {
assert.equal(binary.search(1000, array), -19)
})
it("should return the correct index of a missing item after the last", function() {
assert.equal(binary.search(10, array), -5)
})
})
describe("insert", function() {
it("should correctly insert into an empty array", function() {
var a = []
binary.insert(50, a)
assert.deepEqual(a, [50])
})
it("should correctly insert before the beginning of an array", function() {
var a = [50]
binary.insert(25, a)
assert.deepEqual(a, [25, 50])
})
it("should correctly insert after the end of an array", function() {
var a = [25, 50]
binary.insert(75, a)
assert.deepEqual(a, [25, 50, 75])
})
it("should correctly insert in the middle of an array", function() {
var a = [25, 50, 75]
binary.insert(62.5, a)
assert.deepEqual(a, [25, 50, 62.5, 75])
})
it("should correctly insert a duplicate into the middle of an array", function() {
var a = [25, 50, 62.5, 75]
binary.insert(50, a)
assert.deepEqual(a, [25, 50, 50, 62.5, 75])
})
})
})
describe("spherekd", function() {
function City(name, lat, lon) {
this.name = name;
this.lat = lat;
this.lon = lon;
}
var boston = new City("Boston", 42.358, -71.064),
troy = new City("Troy", 42.732, -73.693),
newYork = new City("New York", 40.664, -73.939),
miami = new City("Miami", 25.788, -80.224),
london = new City("London", 51.507, -0.128),
paris = new City("Paris", 48.857, 2.351),
vienna = new City("Vienna", 48.208, 16.373),
rome = new City("Rome", 41.900, 12.500),
beijing = new City("Beijing", 39.914, 116.392),
hongKong = new City("Hong Kong", 22.278, 114.159),
seoul = new City("Seoul", 37.567, 126.978),
tokyo = new City("Tokyo", 35.690, 139.692),
cities = [
boston, troy, newYork, miami, london, paris, vienna, rome, beijing,
hongKong, seoul, tokyo
];
describe("build", function() {
it("should return null given an empty array", function() {
assert.isNull(spherekd.build([]))
})
it("should construct a KD Tree from the raw data", function() {
var root = spherekd.build(cities)
assert.deepEqual(root, {
axis: 0,
split: 0.20985921950633998,
left: {
axis: 1,
split: 0.6096887366493714,
left: {
axis: 2,
split: 0.5253996743519346,
left: {
object: miami,
position: [
0.1528866465300001,
0.43504252750547817,
-0.8873351523378616
]
},
right: {
axis: 0,
split: -0.37872039680406316,
left: {
object: tokyo,
position: [
-0.6193546995975016,
0.5833994671555415,
0.5253996743519346
]
},
right: {
object: hongKong,
position: [
-0.37872039680406316,
0.37910087653634733,
0.844306452926866
]
}
}
},
right: {
axis: 2,
split: 0.6332143107601375,
left: {
object: troy,
position: [
0.20624585628442826,
0.6785700178191618,
-0.7049860833253415
]
},
right: {
axis: 0,
split: -0.3409429851536599,
left: {
object: seoul,
position: [
-0.4767801181377611,
0.6096887366493714,
0.6332143107601375
]
},
right: {
object: beijing,
position: [
-0.3409429851536599,
0.6416370662276932,
0.6870660493120221
]
}
}
}
},
right: {
axis: 1,
split: 0.745569057905259,
left: {
axis: 2,
split: -0.6989587067659011,
left: {
object: newYork,
position: [
0.20985921950633998,
0.6516219252904669,
-0.7289361936884015
]
},
right: {
axis: 0,
split: 0.7266683906387393,
left: {
object: boston,
position: [
0.23979780809048454,
0.6737608904285988,
-0.6989587067659011
]
},
right: {
object: rome,
position: [
0.7266683906387393,
0.6678325554710466,
0.1610985037159434
]
}
}
},
right: {
axis: 2,
split: 0.026989498186927637,
left: {
object: london,
position: [
0.6224174651684267,
0.782684205521879,
-0.001390496276659977
]
},
right: {
axis: 0,
split: 0.6573868000210528,
left: {
object: vienna,
position: [
0.6394026512991717,
0.745569057905259,
0.18785906793619112
]
},
right: {
object: paris,
position: [
0.6573868000210528,
0.7530698255445496,
0.026989498186927637
]
}
}
}
}
})
})
it("should allow building from geojson features", function() {
var features = cities.map(function(c) {
return {
'type': 'Feature',
'properties': {'name': c.name},
'geometry': {'type': 'Point', 'coordinates': [c.lon, c.lat]},
}
});
var tree = spherekd.build(features)
assert.deepEqual(
spherekd.lookup(39.95, -75.17, tree, 4).map(function(c) { return c.properties.name }),
['New York', 'Troy', 'Boston', 'Miami']
)
})
})
describe("lookup", function() {
var tree = spherekd.build(cities)
it("should return New York, Troy, Boston, and Miami as the four closest cities to Philadelphia", function() {
assert.deepEqual(
spherekd.lookup(39.95, -75.17, tree, 4),
[newYork, troy, boston, miami]
)
})
it("should return Vienna, Paris, London, and Rome as the four closest cities to Berlin", function() {
assert.deepEqual(
spherekd.lookup(52.50, 13.40, tree, 4),
[vienna, paris, london, rome]
)
})
it("should return Tokyo and Seoul as the two closest cities to Hawaii", function() {
assert.deepEqual(
spherekd.lookup(21.31, -157.80, tree, 2),
[tokyo, seoul]
)
})
it("should return Troy, Boston, and New York as the only cities within 200km of Hartford", function() {
assert.deepEqual(
spherekd.lookup(41.76, -72.67, tree, 20, 200000),
[troy, boston, newYork]
)
})
})
})
})