Skip to content
File

Blob: test/ofs-delta-encode.test.ts

typescript30 lines
1import { assert, test } from "vitest";
2import { encodeOfsDeltaDistance } from "@/worker/git";
3 
4function decodeOfsDeltaDistance(bytes: Uint8Array): number {
5 let p = 0;
6 let b = bytes[p++];
7 let x = b & 0x7f;
8 while (b & 0x80) {
9 b = bytes[p++];
10 x = ((x + 1) << 7) | (b & 0x7f);
11 }
12 return x >>> 0;
13}
14 
15const cases = [1, 0x7f, 0x80, 0x1234, 0x1ffff, 0x20000, 0x3ffffff, 0x4000000];
16 
17test("encodeOfsDeltaDistance round-trips typical values", () => {
18 for (const n of cases) {
19 const enc = encodeOfsDeltaDistance(n);
20 const dec = decodeOfsDeltaDistance(enc);
21 assert.strictEqual(
22 dec,
23 n,
24 `round-trip failed for ${n} (enc: ${Array.from(enc)
25 .map((b) => b.toString(16))
26 .join(",")})`
27 );
28 }
29});