]>
Lady’s Gitweb - Pisces/blob - binary.test.js
1 // ♓🌟 Piscēs ∷ binary.test.js
2 // ====================================================================
4 // Copyright © 2020–2023 Lady [@ Lady’s Computer].
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
17 } from "./dev-deps.js";
25 filenameSafeBase64Binary
,
26 filenameSafeBase64String
,
36 // These tests assume a LITTLE‐ENDIAN environment.
37 const data
= new Map([
45 base16
: "006200610073006500360034",
46 base32
: "ABRAAYIAOMAGKABWAA2A====",
47 base64
: "AGIAYQBzAGUANgA0",
48 wrmg
: "01H00R80EC06A01P00T0",
50 [new Uint16Array([62535]), {
56 [new Uint8ClampedArray([75, 73, 66, 73]).buffer
, {
62 [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer
), {
63 base16
: "626173653634",
64 base32
: "MJQXGZJWGQ======",
69 // The following three examples are from RFC 3548.
70 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
71 base16
: "14FB9C03D97E",
72 base32
: "CT5ZYA6ZPY======",
76 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
82 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
89 // The following examples are from the Ruby base32 gem.
90 [new Uint8Array([0x28]), {
96 [new Uint8Array([0xD6]), {
102 [new Uint16Array([0xF8D6]), {
108 [new Uint8Array([0xD6, 0xF8, 0x00]), {
114 [new Uint8Array([0xD6, 0xF8, 0x10]), {
120 [new Uint32Array([0x0C11F8D6]), {
126 [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
127 base16
: "D6F8110C80",
132 [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
133 base16
: "D6F8110C8530",
134 base32
: "234BCDEFGA======",
140 describe("base16Binary", () => {
141 it("[[Call]] returns the correct data", () => {
143 new Uint8Array(base16Binary("")),
148 new Uint8Array(base16Binary("006200610073006500360034")),
150 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
151 ($) => $.charCodeAt(0),
153 "006200610073006500360034",
156 new Uint16Array(base16Binary("47F4")),
157 new Uint16Array([62535]),
161 new Uint8ClampedArray(base16Binary("4B494249")),
162 new Uint8ClampedArray([75, 73, 66, 73]),
166 new Uint8Array(base16Binary("626173653634")),
167 new Uint8Array([98, 97, 115, 101, 54, 52]),
171 new Uint8Array(base16Binary("14FB9C03D97E")),
172 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
176 new Uint8Array(base16Binary("14FB9C03D9")),
177 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
181 new Uint8Array(base16Binary("14FB9C03")),
182 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
186 new Uint8Array(base16Binary("28")),
187 new Uint8Array([0x28]),
191 new Uint8Array(base16Binary("D6")),
192 new Uint8Array([0xD6]),
196 new Uint16Array(base16Binary("D6F8")),
197 new Uint16Array([0xF8D6]),
201 new Uint8Array(base16Binary("D6F800")),
202 new Uint8Array([0xD6, 0xF8, 0x00]),
206 new Uint8Array(base16Binary("D6F810")),
207 new Uint8Array([0xD6, 0xF8, 0x10]),
211 new Uint32Array(base16Binary("D6F8110C")),
212 new Uint32Array([0x0C11F8D6]),
216 new Uint8Array(base16Binary("D6F8110C80")),
217 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
221 new Uint16Array(base16Binary("D6F8110C8530")),
222 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
227 it("[[Call]] is case‐insensitive", () => {
229 new Uint8Array(base16Binary("d6f8110C80")),
230 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
235 it("[[Call]] throws when provided with an invalid character", () => {
236 assertThrows(() => base16Binary("ABCG"));
239 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 2", () => {
240 assertThrows(() => base16Binary("A"));
241 assertThrows(() => base16Binary("ABC"));
245 describe("base16String", () => {
246 it("[[Call]] returns the correct string", () => {
247 for (const [source
, { base16
}] of data
) {
248 assertStrictEquals(base16String(source
), base16
);
253 describe("base32Binary", () => {
254 it("[[Call]] returns the correct data", () => {
256 new Uint8Array(base32Binary("")),
261 new Uint8Array(base32Binary("ABRAAYIAOMAGKABWAA2A====")),
263 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
264 ($) => $.charCodeAt(0),
266 "ABRAAYIAOMAGKABWAA2A====",
269 new Uint16Array(base32Binary("I72A====")),
270 new Uint16Array([62535]),
274 new Uint8ClampedArray(base32Binary("JNEUESI=")),
275 new Uint8ClampedArray([75, 73, 66, 73]),
279 new Uint8Array(base32Binary("MJQXGZJWGQ======")),
280 new Uint8Array([98, 97, 115, 101, 54, 52]),
284 new Uint8Array(base32Binary("CT5ZYA6ZPY======")),
285 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
289 new Uint8Array(base32Binary("CT5ZYA6Z")),
290 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
294 new Uint8Array(base32Binary("CT5ZYAY=")),
295 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
299 new Uint8Array(base32Binary("FA======")),
300 new Uint8Array([0x28]),
304 new Uint8Array(base32Binary("2Y======")),
305 new Uint8Array([0xD6]),
309 new Uint16Array(base32Binary("234A====")),
310 new Uint16Array([0xF8D6]),
314 new Uint8Array(base32Binary("234AA===")),
315 new Uint8Array([0xD6, 0xF8, 0x00]),
319 new Uint8Array(base32Binary("234BA===")),
320 new Uint8Array([0xD6, 0xF8, 0x10]),
324 new Uint32Array(base32Binary("234BCDA=")),
325 new Uint32Array([0x0C11F8D6]),
329 new Uint8Array(base32Binary("234BCDEA")),
330 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
334 new Uint16Array(base32Binary("234BCDEFGA======")),
335 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
340 it("[[Call]] is case‐insensitive", () => {
342 new Uint8Array(base32Binary("234bcdEA")),
343 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
348 it("[[Call]] throws when provided with an invalid character", () => {
349 assertThrows(() => base32Binary("ABC0"));
350 assertThrows(() => base32Binary("ABC1"));
351 assertThrows(() => base32Binary("ABC8"));
352 assertThrows(() => base32Binary("ABC9"));
355 it("[[Call]] throws when provided with an invalid equals", () => {
356 assertThrows(() => base32Binary("CT3ZYAY=="));
359 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
360 assertThrows(() => base32Binary("234BCDEAA"));
361 assertThrows(() => base32Binary("234BCDEAA======="));
362 assertThrows(() => base32Binary("UHI"));
363 assertThrows(() => base32Binary("UHIUJD"));
367 describe("base32String", () => {
368 it("[[Call]] returns the correct string", () => {
369 for (const [source
, { base32
}] of data
) {
370 assertStrictEquals(base32String(source
), base32
);
375 describe("base64Binary", () => {
376 it("[[Call]] returns the correct data", () => {
378 new Uint8Array(base64Binary("")),
383 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
385 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
386 ($) => $.charCodeAt(0),
391 new Uint16Array(base64Binary("R/Q=")),
392 new Uint16Array([62535]),
396 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
397 new Uint8ClampedArray([75, 73, 66, 73]),
401 new Uint8Array(base64Binary("YmFzZTY0")),
402 new Uint8Array([98, 97, 115, 101, 54, 52]),
406 new Uint8Array(base64Binary("FPucA9l+")),
407 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
411 new Uint8Array(base64Binary("FPucA9k=")),
412 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
416 new Uint8Array(base64Binary("FPucAw==")),
417 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
421 new Uint8Array(base64Binary("KA==")),
422 new Uint8Array([0x28]),
426 new Uint8Array(base64Binary("1g==")),
427 new Uint8Array([0xD6]),
431 new Uint16Array(base64Binary("1vg")),
432 new Uint16Array([0xF8D6]),
436 new Uint8Array(base64Binary("1vgA")),
437 new Uint8Array([0xD6, 0xF8, 0x00]),
441 new Uint8Array(base64Binary("1vgQ")),
442 new Uint8Array([0xD6, 0xF8, 0x10]),
446 new Uint32Array(base64Binary("1vgRDA==")),
447 new Uint32Array([0x0C11F8D6]),
451 new Uint8Array(base64Binary("1vgRDIA=")),
452 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
456 new Uint16Array(base64Binary("1vgRDIUw")),
457 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
462 it("[[Call]] throws when provided with an invalid character", () => {
463 assertThrows(() => base64Binary("abc_"));
466 it("[[Call]] throws when provided with an invalid equals", () => {
467 assertThrows(() => base64Binary("abc=="));
470 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
471 assertThrows(() => base64Binary("abcde"));
472 assertThrows(() => base64Binary("abcde==="));
476 describe("base64String", () => {
477 it("[[Call]] returns the correct string", () => {
478 for (const [source
, { base64
}] of data
) {
479 assertStrictEquals(base64String(source
), base64
);
484 describe("filenameSafeBase64Binary", () => {
485 it("[[Call]] returns the correct data", () => {
487 new Uint8Array(filenameSafeBase64Binary("")),
492 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
494 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
495 ($) => $.charCodeAt(0),
500 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
501 new Uint16Array([62535]),
505 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
506 new Uint8ClampedArray([75, 73, 66, 73]),
510 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
511 new Uint8Array([98, 97, 115, 101, 54, 52]),
515 new Uint8Array(filenameSafeBase64Binary("KA==")),
516 new Uint8Array([0x28]),
520 new Uint8Array(filenameSafeBase64Binary("1g==")),
521 new Uint8Array([0xD6]),
525 new Uint16Array(filenameSafeBase64Binary("1vg")),
526 new Uint16Array([0xF8D6]),
530 new Uint8Array(filenameSafeBase64Binary("1vgA")),
531 new Uint8Array([0xD6, 0xF8, 0x00]),
535 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
536 new Uint8Array([0xD6, 0xF8, 0x10]),
540 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
541 new Uint32Array([0x0C10F8D6]),
545 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
546 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
550 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
551 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
556 it("[[Call]] throws when provided with an invalid character", () => {
557 assertThrows(() => filenameSafeBase64Binary("abc/"));
560 it("[[Call]] throws when provided with an invalid equals", () => {
561 assertThrows(() => filenameSafeBase64Binary("abc=="));
564 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
565 assertThrows(() => filenameSafeBase64Binary("abcde"));
566 assertThrows(() => filenameSafeBase64Binary("abcde==="));
570 describe("filenameSafeBase64String", () => {
571 it("[[Call]] returns the correct string", () => {
572 for (const [source
, { base64
}] of data
) {
574 filenameSafeBase64String(source
),
575 base64
.replace("+", "-").replace("/", "_"),
581 describe("isBase16", () => {
582 it("[[Call]] returns true for base64 strings", () => {
583 for (const { base16
} of data
.values()) {
584 assert(isBase16(base16
));
585 assert(isBase16(base16
.toLowerCase()));
589 it("[[Call]] returns false for others", () => {
605 ].forEach((value
) => assert(!isBase16(value
)));
609 describe("isBase32", () => {
610 it("[[Call]] returns true for base32 strings", () => {
611 for (const { base32
} of data
.values()) {
612 assert(isBase32(base32
));
613 assert(isBase32(base32
.toLowerCase()));
617 it("[[Call]] returns false for others", () => {
632 ].forEach((value
) => assert(!isBase32(value
)));
636 describe("isBase64", () => {
637 it("[[Call]] returns true for base64 strings", () => {
638 for (const { base64
} of data
.values()) {
639 assert(isBase64(base64
));
643 it("[[Call]] returns false for others", () => {
658 ].forEach((value
) => assert(!isBase64(value
)));
662 describe("isFilenameSafeBase64", () => {
663 it("[[Call]] returns true for filename‐safe base64 strings", () => {
664 for (const { base64
} of data
.values()) {
666 isFilenameSafeBase64(
667 base64
.replace("+", "-").replace("/", "_"),
673 it("[[Call]] returns false for others", () => {
688 ].forEach((value
) => assert(!isFilenameSafeBase64(value
)));
692 describe("isWRMGBase32", () => {
693 it("[[Call]] returns true for W·R·M·G base32 strings", () => {
694 for (const { wrmg
} of data
.values()) {
695 assert(isWRMGBase32(wrmg
));
696 assert(isWRMGBase32(wrmg
.toLowerCase()));
697 assert(isWRMGBase32(`--${wrmg}--`));
698 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "I")));
699 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "L")));
700 assert(isWRMGBase32(wrmg
.replaceAll(/0/gu
, "O")));
701 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "i")));
702 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "l")));
703 assert(isWRMGBase32(wrmg
.replaceAll(/0/gu
, "o")));
704 assert(isWRMGBase32(wrmg
.replaceAll(/./gu
, ($) => {
705 const rand
= Math
.random();
717 it("[[Call]] returns false for others", () => {
732 ].forEach((value
) => assert(!isWRMGBase32(value
)));
736 describe("wrmgBase32Binary", () => {
737 it("[[Call]] returns the correct data", () => {
739 new Uint8Array(wrmgBase32Binary("")),
744 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
746 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
747 ($) => $.charCodeAt(0),
749 "01H00R80EC06A01P00T0",
752 new Uint16Array(wrmgBase32Binary("8ZT0")),
753 new Uint16Array([62535]),
757 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
758 new Uint8ClampedArray([75, 73, 66, 73]),
762 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
763 new Uint8Array([98, 97, 115, 101, 54, 52]),
767 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
768 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
772 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
773 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
777 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
778 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
782 new Uint8Array(wrmgBase32Binary("50")),
783 new Uint8Array([0x28]),
787 new Uint8Array(wrmgBase32Binary("TR")),
788 new Uint8Array([0xD6]),
792 new Uint16Array(wrmgBase32Binary("TVW0")),
793 new Uint16Array([0xF8D6]),
797 new Uint8Array(wrmgBase32Binary("TVW00")),
798 new Uint8Array([0xD6, 0xF8, 0x00]),
802 new Uint8Array(wrmgBase32Binary("TVW10")),
803 new Uint8Array([0xD6, 0xF8, 0x10]),
807 new Uint32Array(wrmgBase32Binary("TVW1230")),
808 new Uint32Array([0x0C11F8D6]),
812 new Uint8Array(wrmgBase32Binary("TVW12340")),
813 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
817 new Uint16Array(wrmgBase32Binary("TVW1234560")),
818 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
823 it("[[Call]] is case‐insensitive", () => {
825 new Uint8Array(wrmgBase32Binary("tVw12340")),
826 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
831 it("[[Call]] casts I, L & O", () => {
833 new Uint8Array(wrmgBase32Binary("TVWI234O")),
834 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
838 new Uint8Array(wrmgBase32Binary("TVWi234o")),
839 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
843 new Uint8Array(wrmgBase32Binary("TVWL234O")),
844 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
848 new Uint8Array(wrmgBase32Binary("TVWl234o")),
849 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
854 it("[[Call]] ignores hyphens", () => {
856 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
857 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
858 "--TVW---123-40-----",
862 it("[[Call]] throws when provided with an invalid character", () => {
863 assertThrows(() => wrmgBase32Binary("ABCu"));
864 assertThrows(() => wrmgBase32Binary("ABCU"));
865 assertThrows(() => wrmgBase32Binary("ABC="));
868 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
869 assertThrows(() => base32Binary("TVW123400"));
870 assertThrows(() => base32Binary("TVW123400======="));
871 assertThrows(() => base32Binary("M78"));
872 assertThrows(() => base32Binary("M78M93"));
875 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
876 assertThrows(() => wrmgBase32Binary("234BCDEAA"));
877 assertThrows(() => wrmgBase32Binary("2-34-B--CD-EA-A-"));
881 describe("wrmgBase32String", () => {
882 it("[[Call]] returns the correct string", () => {
883 for (const [source
, { wrmg
}] of data
) {
884 assertStrictEquals(wrmgBase32String(source
), wrmg
);
This page took 0.133463 seconds and 5 git commands to generate.