+// These tests assume a LITTLE‐ENDIAN environment.
+const data = new Map([
+ ["", {
+ base16: "",
+ base32: "",
+ base64: "",
+ wrmg: "",
+ }],
+ ["base64", {
+ base16: "006200610073006500360034",
+ base32: "ABRAAYIAOMAGKABWAA2A====",
+ base64: "AGIAYQBzAGUANgA0",
+ wrmg: "01H00R80EC06A01P00T0",
+ }],
+ [new Uint16Array([62535]), {
+ base16: "47F4",
+ base32: "I72A====",
+ base64: "R/Q=",
+ wrmg: "8ZT0",
+ }],
+ [new Uint8ClampedArray([75, 73, 66, 73]).buffer, {
+ base16: "4B494249",
+ base32: "JNEUESI=",
+ base64: "S0lCSQ==",
+ wrmg: "9D4M4J8",
+ }],
+ [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer), {
+ base16: "626173653634",
+ base32: "MJQXGZJWGQ======",
+ base64: "YmFzZTY0",
+ wrmg: "C9GQ6S9P6G",
+ }],
+
+ // The following three examples are from RFC 3548.
+ [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
+ base16: "14FB9C03D97E",
+ base32: "CT5ZYA6ZPY======",
+ base64: "FPucA9l+",
+ wrmg: "2KXSR0YSFR",
+ }],
+ [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
+ base16: "14FB9C03D9",
+ base32: "CT5ZYA6Z",
+ base64: "FPucA9k=",
+ wrmg: "2KXSR0YS",
+ }],
+ [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
+ base16: "14FB9C03",
+ base32: "CT5ZYAY=",
+ base64: "FPucAw==",
+ wrmg: "2KXSR0R",
+ }],
+
+ // The following examples are from the Ruby base32 gem.
+ [new Uint8Array([0x28]), {
+ base16: "28",
+ base32: "FA======",
+ base64: "KA==",
+ wrmg: "50",
+ }],
+ [new Uint8Array([0xD6]), {
+ base16: "D6",
+ base32: "2Y======",
+ base64: "1g==",
+ wrmg: "TR",
+ }],
+ [new Uint16Array([0xF8D6]), {
+ base16: "D6F8",
+ base32: "234A====",
+ base64: "1vg=",
+ wrmg: "TVW0",
+ }],
+ [new Uint8Array([0xD6, 0xF8, 0x00]), {
+ base16: "D6F800",
+ base32: "234AA===",
+ base64: "1vgA",
+ wrmg: "TVW00",
+ }],
+ [new Uint8Array([0xD6, 0xF8, 0x10]), {
+ base16: "D6F810",
+ base32: "234BA===",
+ base64: "1vgQ",
+ wrmg: "TVW10",
+ }],
+ [new Uint32Array([0x0C11F8D6]), {
+ base16: "D6F8110C",
+ base32: "234BCDA=",
+ base64: "1vgRDA==",
+ wrmg: "TVW1230",
+ }],
+ [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
+ base16: "D6F8110C80",
+ base32: "234BCDEA",
+ base64: "1vgRDIA=",
+ wrmg: "TVW12340",
+ }],
+ [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
+ base16: "D6F8110C8530",
+ base32: "234BCDEFGA======",
+ base64: "1vgRDIUw",
+ wrmg: "TVW1234560",
+ }],
+]);
+
+describe("base16Binary", () => {
+ it("[[Call]] returns the correct data", () => {
+ assertEquals(
+ new Uint8Array(base16Binary("")),
+ new Uint8Array([]),
+ "<empty>",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("006200610073006500360034")),
+ Uint8Array.from(
+ "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
+ ($) => $.charCodeAt(0),
+ ),
+ "006200610073006500360034",
+ );
+ assertEquals(
+ new Uint16Array(base16Binary("47F4")),
+ new Uint16Array([62535]),
+ "47F4",
+ );
+ assertEquals(
+ new Uint8ClampedArray(base16Binary("4B494249")),
+ new Uint8ClampedArray([75, 73, 66, 73]),
+ "4B494249",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("626173653634")),
+ new Uint8Array([98, 97, 115, 101, 54, 52]),
+ "626173653634",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("14FB9C03D97E")),
+ new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
+ "14FB9C03D97E",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("14FB9C03D9")),
+ new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
+ "14FB9C03D9",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("14FB9C03")),
+ new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
+ "14FB9C03",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("28")),
+ new Uint8Array([0x28]),
+ "28",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("D6")),
+ new Uint8Array([0xD6]),
+ "D6",
+ );
+ assertEquals(
+ new Uint16Array(base16Binary("D6F8")),
+ new Uint16Array([0xF8D6]),
+ "D6F8",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("D6F800")),
+ new Uint8Array([0xD6, 0xF8, 0x00]),
+ "D6F800",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("D6F810")),
+ new Uint8Array([0xD6, 0xF8, 0x10]),
+ "D6F810",
+ );
+ assertEquals(
+ new Uint32Array(base16Binary("D6F8110C")),
+ new Uint32Array([0x0C11F8D6]),
+ "D6F8110C",
+ );
+ assertEquals(
+ new Uint8Array(base16Binary("D6F8110C80")),
+ new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
+ "D6F8110C80",
+ );
+ assertEquals(
+ new Uint16Array(base16Binary("D6F8110C8530")),
+ new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
+ "D6F8110C8530",
+ );
+ });
+
+ it("[[Call]] is case‐insensitive", () => {
+ assertEquals(
+ new Uint8Array(base16Binary("d6f8110C80")),
+ new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
+ "d6f8110C80",
+ );
+ });
+
+ it("[[Call]] throws when provided with an invalid character", () => {
+ assertThrows(() => base16Binary("ABCG"));
+ });
+
+ it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 2", () => {
+ assertThrows(() => base16Binary("A"));
+ assertThrows(() => base16Binary("ABC"));
+ });
+});
+
+describe("base16String", () => {
+ it("[[Call]] returns the correct string", () => {
+ for (const [source, { base16 }] of data) {
+ assertStrictEquals(base16String(source), base16);
+ }
+ });
+});
+
+describe("base32Binary", () => {
+ it("[[Call]] returns the correct data", () => {
+ assertEquals(
+ new Uint8Array(base32Binary("")),
+ new Uint8Array([]),
+ "<empty>",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("ABRAAYIAOMAGKABWAA2A====")),
+ Uint8Array.from(
+ "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
+ ($) => $.charCodeAt(0),
+ ),
+ "ABRAAYIAOMAGKABWAA2A====",
+ );
+ assertEquals(
+ new Uint16Array(base32Binary("I72A====")),
+ new Uint16Array([62535]),
+ "I72A====",
+ );
+ assertEquals(
+ new Uint8ClampedArray(base32Binary("JNEUESI=")),
+ new Uint8ClampedArray([75, 73, 66, 73]),
+ "JNEUESI=",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("MJQXGZJWGQ======")),
+ new Uint8Array([98, 97, 115, 101, 54, 52]),
+ "MJQXGZJWGQ======",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("CT5ZYA6ZPY======")),
+ new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
+ "CT5ZYA6ZPY======",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("CT5ZYA6Z")),
+ new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
+ "CT5ZYA6Z",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("CT5ZYAY=")),
+ new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
+ "CT5ZYAY=",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("FA======")),
+ new Uint8Array([0x28]),
+ "FA======",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("2Y======")),
+ new Uint8Array([0xD6]),
+ "2Y======",
+ );
+ assertEquals(
+ new Uint16Array(base32Binary("234A====")),
+ new Uint16Array([0xF8D6]),
+ "234A====",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("234AA===")),
+ new Uint8Array([0xD6, 0xF8, 0x00]),
+ "234AA===",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("234BA===")),
+ new Uint8Array([0xD6, 0xF8, 0x10]),
+ "234BA===",
+ );
+ assertEquals(
+ new Uint32Array(base32Binary("234BCDA=")),
+ new Uint32Array([0x0C11F8D6]),
+ "234BCDA=",
+ );
+ assertEquals(
+ new Uint8Array(base32Binary("234BCDEA")),
+ new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
+ "234BCDEA",
+ );
+ assertEquals(
+ new Uint16Array(base32Binary("234BCDEFGA======")),
+ new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
+ "234BCDEFGA======",
+ );
+ });
+
+ it("[[Call]] is case‐insensitive", () => {
+ assertEquals(
+ new Uint8Array(base32Binary("234bcdEA")),
+ new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
+ "234bcdEA",
+ );
+ });
+
+ it("[[Call]] throws when provided with an invalid character", () => {
+ assertThrows(() => base32Binary("ABC0"));
+ assertThrows(() => base32Binary("ABC1"));
+ assertThrows(() => base32Binary("ABC8"));
+ assertThrows(() => base32Binary("ABC9"));
+ });
+
+ it("[[Call]] throws when provided with an invalid equals", () => {
+ assertThrows(() => base32Binary("CT3ZYAY=="));
+ });
+
+ it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
+ assertThrows(() => base32Binary("234BCDEAA"));
+ assertThrows(() => base32Binary("234BCDEAA======="));
+ assertThrows(() => base32Binary("UHI"));
+ assertThrows(() => base32Binary("UHIUJD"));
+ });
+});
+
+describe("base32String", () => {
+ it("[[Call]] returns the correct string", () => {
+ for (const [source, { base32 }] of data) {
+ assertStrictEquals(base32String(source), base32);
+ }
+ });
+});