-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterFinder.test.js
331 lines (277 loc) · 7.54 KB
/
clusterFinder.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
var clusterFinder = require('./clusterFinder')
var convertMetersToGeocode = require('./convertMetersToGeocode')
describe('clusterFinder', () => {
describe('simple test cases', () => {
let testData = [{
x: 1, y: 1
}, {
x: 2, y: 1
}, {
x: 1, y: 2
}, {
x: 3, y: 4
}]
test('positive result', () => {
let results = clusterFinder(2, 3, testData)
expect(results.length).toBe(1)
expect(results[0].points.length).toBe(3)
})
test('negative result because of distance', () => {
let results = clusterFinder(0.5, 3, testData)
expect(results.length).toBe(0)
})
test('negative result because of point count', () => {
let results = clusterFinder(1, 4, testData)
expect(results.length).toBe(0)
})
})
describe('complex cases', () => {
describe('negative numbers', () => {
describe('all negative', () => {
let testData = [{
x: -1, y: -1
}, {
x: -2, y: -2
}, {
x: -2, y: -3
}]
test('positive result', () => {
let results = clusterFinder(2, 2, testData)
expect(results.length).toBe(2)
expect(results[0].points.length).toBe(2)
})
test('negative result because of distance', () => {
let results = clusterFinder(0.5, 2, testData)
expect(results.length).toBe(0)
})
test('negative result because of point count', () => {
let results = clusterFinder(1, 3, testData)
expect(results.length).toBe(0)
})
})
describe('some negative', () => {
let testData = [{
x: -2, y: 2
}, {
x: -1, y: 1
}, {
x: 1, y: 2
}, {
x: 1, y: 1
}, {
x: -1, y: -1
}, {
x: -2, y: -2
}, {
x: 1, y: -1
}, {
x: 1, y: -2
}, {
x: 3, y: -6
}]
test('positive result', () => {
let results = clusterFinder(5, 8, testData)
expect(results.length).toBeGreaterThan(0)
})
test('negative result because of distance', () => {
let results = clusterFinder(0.5, 2, testData)
expect(results.length).toBe(0)
})
test('negative result because of point count', () => {
let results = clusterFinder(10, 10, testData)
expect(results.length).toBe(0)
})
test('positive result because of large distance count', () => {
let results = clusterFinder(15, 9, testData)
expect(results.length).toBe(1)
})
})
})
describe('small numbers', () => {
let testData = [{
x: 0.000001, y: 0.000001
}, {
x: 0.000002, y: 0.000001
}, {
x: 0.000001, y: 0.000002
}]
test('positive result', () => {
let results = clusterFinder(1, 3, testData)
expect(results.length).toBe(1)
})
test('negative result because of distance', () => {
let results = clusterFinder(0.0000005, 3, testData)
expect(results.length).toBe(0)
})
test('negative result because of point count', () => {
let results = clusterFinder(1, 4, testData)
expect(results.length).toBe(0)
})
})
})
describe('real data', () => {
// from real data:
// smallest lat 52.266958
// smallest lng -2.3038512
// biggest lat 52.6680175
// biggest lng -1.4391685
// x = longitude, y = lat
let testData = [{
x: -2.3038512, y: 52.266958 // bottom left
}, {
x: -1.4391685, y: 52.6680175 // top right
}, {
x: -1.892702, y: 52.435290 // next two points are schools in the middle of the area and close enough to be flagged when requesting two schools
}, {
x: -1.890728, y: 52.436480
}, {
x: -2.016273, y: 52.575979 // this school is too far away, so nothing comes up when requesting three schools
}]
test('positive result', () => {
let maxDistance = 200 // meters
let gridSize = 10 // meters
let results = clusterFinder(convertMetersToGeocode(maxDistance), 2, testData)
expect(results.length).toBeGreaterThan(0)
})
// function convertMetersToGeocodeOld (meters) {
// // dinstances taken from gmaps in birmingham :D
// // 52.434524, -1.892822
// // 52.433682, -1.892924
// // 94.65m
// let xDist = -1.892822 - -1.892924
// let yDist = 52.434524 - 52.433682
// let dist = Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2))
// return meters * (dist / 94.65)
// }
})
describe('groupings', () => {
test('finds the destinct groups', () => {
let testData = [{
x: 1, y: 2
}, {
x: 2, y: 3
}, {
x: 3, y: 2
}, {
x: 5, y: 10
}, {
x: 5, y: 12
}, {
x: 7, y: 12
}, {
x: 7, y: 10
}, {
x: 12, y: 7
}, {
x: 15, y: 7
}, {
x: 13, y: 3
}, {
x: 14, y: 1
}, {
x: 13, y: 2
}, {
x: 15, y: 2
}, {
x: 15, y: 3
}]
let results = clusterFinder(3, 3, testData)
expect(results.length).toBe(3)
})
test('node in multiple groups', () => {
let testData = [{
x: 2, y: 2
}, {
x: 2, y: 3
}, {
x: 3, y: 2
}, {
x: 3, y: 3
}, {
x: 3, y: 4
}, {
x: 4, y: 3
}, {
x: 4, y: 4
}, {
x: 4, y: 5
}, {
x: 5, y: 4
}, {
x: 5, y: 5
}]
let results = clusterFinder(Math.sqrt(2), 4, testData)
expect(results.length).toBe(3)
expect(results[0].points.length).toBe(4)
expect(results[0].points).toContainEqual({
x: 2, y: 2
})
expect(results[0].points).toContainEqual({
x: 2, y: 3
})
expect(results[0].points).toContainEqual({
x: 3, y: 2
})
expect(results[0].points).toContainEqual({
x: 3, y: 3
})
expect(results[1].points.length).toBe(4)
expect(results[1].points).toContainEqual({
x: 3, y: 3
})
expect(results[1].points).toContainEqual({
x: 3, y: 4
})
expect(results[1].points).toContainEqual({
x: 4, y: 3
})
expect(results[1].points).toContainEqual({
x: 4, y: 4
})
expect(results[2].points.length).toBe(4)
expect(results[2].points).toContainEqual({
x: 4, y: 4
})
expect(results[2].points).toContainEqual({
x: 4, y: 5
})
expect(results[2].points).toContainEqual({
x: 5, y: 4
})
expect(results[2].points).toContainEqual({
x: 5, y: 5
})
})
})
describe('central points', function () {
// JS floating point math has limitations. could use decimal.js here if more accuracy is required
describe('basic case', () => {
let testData = [{
x: 2, y: 3
}, {
x: 2, y: 4
}, {
x: 3, y: 3
}, {
x: 3, y: 4
}]
let results = clusterFinder(Math.sqrt(2), 4, testData)
expect(results[0].center.x).toBeCloseTo(2.5)
expect(results[0].center.y).toBeCloseTo(3.5)
})
describe('negative case', () => {
let testData = [{
x: -2, y: -3
}, {
x: -2, y: -4
}, {
x: -3, y: -3
}, {
x: -3, y: -4
}]
let results = clusterFinder(Math.sqrt(2), 4, testData)
expect(results[0].center.x).toBeCloseTo(-2.5)
expect(results[0].center.y).toBeCloseTo(-3.5)
})
})
})