-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathencode.test.ts
More file actions
72 lines (65 loc) · 2.24 KB
/
Copy pathencode.test.ts
File metadata and controls
72 lines (65 loc) · 2.24 KB
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
import { describe, expect, it } from 'vitest';
import { testdata } from './fixtures/encodes';
import { encodeWithPrecision } from '../src/encode';
import { decode } from '../src/decode';
import { convertToAlphabet } from '../src/alphabet/alphabet';
import { distanceInMeters, maxErrorInMeters } from '../src/geo/distance';
interface EncodeCase {
str: string;
y: number;
x: number;
local: number;
global: number;
}
function buildCases(): EncodeCase[] {
const cases: EncodeCase[] = [];
for (let i = 0; testdata[i] !== -1 && i < testdata.length; i += 5) {
cases.push({
str: String(testdata[i]),
y: Number(testdata[i + 1]),
x: Number(testdata[i + 2]),
local: Number(testdata[i + 3]),
global: Number(testdata[i + 4]),
});
}
return cases;
}
const cases = buildCases();
describe('encode corpus', () => {
it.each(cases)('encodes/decodes $str ($y, $x)', ({ str, y, x, local, global }) => {
const trimmed = str.trim();
const sp = trimmed.indexOf(' ');
const territory = sp > 0 ? trimmed.substring(0, sp) : 'AAA';
const lat = Math.max(-90, Math.min(90, y));
if (local || trimmed !== '') {
const r = encodeWithPrecision(lat, x, 2, territory);
if (local) {
expect(r.length, `local solution count for ${trimmed}`).toBe(local);
}
if (trimmed !== '') {
expect(
r.some((m) => m.fullmapcode.indexOf(trimmed) === 0),
`expected mapcode ${trimmed} to be produced`,
).toBe(true);
}
}
const g = encodeWithPrecision(lat, x, 2);
if (global) {
expect(g.length, `global solution count for ${trimmed}`).toBe(global);
}
for (let precision = 0; precision <= 8; precision++) {
const rs = encodeWithPrecision(lat, x, precision);
for (const m of rs) {
const full = m.fullmapcode;
expect(convertToAlphabet(convertToAlphabet(full, 14), 0)).toBe(full);
const p = decode(full);
expect(p, `decode(${full}) should succeed`).not.toBe(false);
if (p === false) continue;
const dm = distanceInMeters(lat, x, p.y, p.x);
expect(dm, `decode(${full}) is ${(dm * 100).toFixed(2)}cm from origin`).toBeLessThanOrEqual(
maxErrorInMeters(precision),
);
}
}
});
});