|
| 1 | +import { describe, test } from "node:test"; |
| 2 | +import { expect } from "expect"; |
| 3 | + |
| 4 | +import { decode, encode } from "./base62.ts"; |
| 5 | + |
| 6 | +describe("base62 roundtrip", () => { |
| 7 | + function encodeText(value: string): Uint8Array { |
| 8 | + return new TextEncoder().encode(value); |
| 9 | + } |
| 10 | + |
| 11 | + describe("roundtrip", () => { |
| 12 | + test("empty string", () => { |
| 13 | + const s = encode(new Uint8Array()); |
| 14 | + expect(s).toEqual(""); |
| 15 | + expect(decode(s)).toEqual(new Uint8Array()); |
| 16 | + }); |
| 17 | + |
| 18 | + test("bytes 0..255", () => { |
| 19 | + const data = new Uint8Array(256); |
| 20 | + for (let i = 0; i < 256; ++i) { |
| 21 | + data[i] = i; |
| 22 | + } |
| 23 | + |
| 24 | + const s = encode(data); |
| 25 | + |
| 26 | + expect(s).toEqual( |
| 27 | + "035wZm1edb9RUsMkV3aMFxHKByy8J2aM5Kr1OxrO0hmOBbl8MqLURltuvCSdUydttr0kDH5LJyur4aKPuwfsxqZ0ALf45zQuWteH7IF5ycqRE5mYv5s3iwmE6MyKUHdhAqHAteHCSgmXkPbhxydluUJPsZdhmc8Jfpx2AkFATFLIY11c84EiU86slyiE7aaeBzQEW1rGH2yjb3RyCOFrD2Ol1CAAmSSHZ9VfBmJbqeWev8Awd6QrqF8nDpJQ7IKKxLq81bRPQzxDdy5sebVIHOCAVxlF0dnpB8oNC2rq2h7Z02P679dGKSlV8bWd13mxeGjbVg7L5nxLrZKqRzdn27z", |
| 28 | + ); |
| 29 | + expect(decode(s)).toEqual(data); |
| 30 | + }); |
| 31 | + |
| 32 | + test("encode simple text", () => { |
| 33 | + const data = encodeText("abc"); |
| 34 | + const s = encode(data); |
| 35 | + |
| 36 | + expect(s).toEqual("QmIN"); |
| 37 | + expect(decode(s)).toEqual(data); |
| 38 | + }); |
| 39 | + |
| 40 | + test("encode zeros prefix", () => { |
| 41 | + const data = new Uint8Array([0, 0, 1, 2, 3]); |
| 42 | + const s = encode(data); |
| 43 | + |
| 44 | + expect(s).toEqual("00HBL"); |
| 45 | + expect(decode(s)).toEqual(data); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
0 commit comments