-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest.js
53 lines (42 loc) · 2.13 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
import test from 'node:test';
import assert from 'node:assert/strict';
import KDBush from 'kdbush';
import cities from 'all-the-cities';
import * as geokdbush from './index.js';
const index = new KDBush(cities.length);
for (const {loc: {coordinates: [lon, lat]}} of cities) index.add(lon, lat);
index.finish();
test('performs search according to maxResults', () => {
const points = geokdbush.around(index, -119.7051, 34.4363, 5);
assert.equal(points.map(id => cities[id].name).join(', '), 'Mission Canyon, Santa Barbara, Montecito, Summerland, Goleta');
});
test('performs search within maxDistance', () => {
const points = geokdbush.around(index, 30.5, 50.5, Infinity, 20);
assert.equal(points.map(id => cities[id].name).join(', '),
'Kyiv, Vyshhorod, Pohreby, Kotsyubyns’ke, Horenka, Sofiyivska Borschagivka, Novi Petrivtsi, Vyshneve, Kriukivschina, Irpin, Hostomel, Chabany, Khotiv, Pukhivka');
});
test('performs search using filter function', () => {
const points = geokdbush.around(index, 30.5, 50.5, 10, Infinity, id => cities[id].population > 200000 && cities[id].country === 'UA');
assert.equal(points.map(id => cities[id].name).join(', '),
'Kyiv, Chernihiv, Zhytomyr, Cherkasy, Vinnytsia, Kropyvnytskyi, Kremenchuk, Khmelnytskyi, Rivne, Poltava');
});
test('performs exhaustive search in correct order', () => {
const points = geokdbush.around(index, 30.5, 50.5);
const lon = 30.5;
const lat = 50.5;
const sorted = cities
.map(({loc: {coordinates: [plon, plat]}}, id) => ({id, dist: geokdbush.distance(lon, lat, plon, plat)}))
.sort((a, b) => a.dist - b.dist);
for (let i = 0; i < sorted.length; i++) {
const [plon, plat] = cities[points[i]].loc.coordinates;
const dist = geokdbush.distance(plon, plat, lon, lat);
if (dist !== sorted[i].dist) {
assert.fail(`${cities[points[i]].name } vs ${ cities[sorted[i].id].name}`);
break;
}
}
// all points in correct order
});
test('calculates great circle distance', () => {
assert.equal(10131.7396, Math.round(1e4 * geokdbush.distance(30.5, 50.5, -119.7, 34.4)) / 1e4);
});