Skip to content

Commit 6bd1691

Browse files
test: port full encode corpus to Vitest
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ed18b77 commit 6bd1691

2 files changed

Lines changed: 15976 additions & 0 deletions

File tree

test/encode.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { testdata } from './fixtures/encodes';
3+
import { encodeWithPrecision } from '../src/encode';
4+
import { decode } from '../src/decode';
5+
import { convertToAlphabet } from '../src/alphabet/alphabet';
6+
import { distanceInMeters, maxErrorInMeters } from '../src/geo/distance';
7+
8+
interface EncodeCase {
9+
str: string;
10+
y: number;
11+
x: number;
12+
local: number;
13+
global: number;
14+
}
15+
16+
function buildCases(): EncodeCase[] {
17+
const cases: EncodeCase[] = [];
18+
for (let i = 0; testdata[i] !== -1 && i < testdata.length; i += 5) {
19+
cases.push({
20+
str: String(testdata[i]),
21+
y: Number(testdata[i + 1]),
22+
x: Number(testdata[i + 2]),
23+
local: Number(testdata[i + 3]),
24+
global: Number(testdata[i + 4]),
25+
});
26+
}
27+
return cases;
28+
}
29+
30+
const cases = buildCases();
31+
32+
describe('encode corpus', () => {
33+
it.each(cases)('encodes/decodes $str ($y, $x)', ({ str, y, x, local, global }) => {
34+
const trimmed = str.trim();
35+
const sp = trimmed.indexOf(' ');
36+
const territory = sp > 0 ? trimmed.substring(0, sp) : 'AAA';
37+
const lat = Math.max(-90, Math.min(90, y));
38+
39+
if (local || trimmed !== '') {
40+
const r = encodeWithPrecision(lat, x, 2, territory);
41+
if (local) {
42+
expect(r.length, `local solution count for ${trimmed}`).toBe(local);
43+
}
44+
if (trimmed !== '') {
45+
expect(
46+
r.some((m) => m.fullmapcode.indexOf(trimmed) === 0),
47+
`expected mapcode ${trimmed} to be produced`,
48+
).toBe(true);
49+
}
50+
}
51+
52+
const g = encodeWithPrecision(lat, x, 2);
53+
if (global) {
54+
expect(g.length, `global solution count for ${trimmed}`).toBe(global);
55+
}
56+
57+
for (let precision = 0; precision <= 8; precision++) {
58+
const rs = encodeWithPrecision(lat, x, precision);
59+
for (const m of rs) {
60+
const full = m.fullmapcode;
61+
expect(convertToAlphabet(convertToAlphabet(full, 14), 0)).toBe(full);
62+
const p = decode(full);
63+
expect(p, `decode(${full}) should succeed`).not.toBe(false);
64+
if (p === false) continue;
65+
const dm = distanceInMeters(lat, x, p.y, p.x);
66+
expect(
67+
dm,
68+
`decode(${full}) is ${(dm * 100).toFixed(2)}cm from origin`,
69+
).toBeLessThanOrEqual(maxErrorInMeters(precision));
70+
}
71+
}
72+
});
73+
});

0 commit comments

Comments
 (0)