]>
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 when divided by 4", () => {
360 assertThrows(() => base32Binary("234BCDEAA"));
361 assertThrows(() => base32Binary("234BCDEAA======="));
365 describe("base32String", () => {
366 it("[[Call]] returns the correct string", () => {
367 for (const [source
, { base32
}] of data
) {
368 assertStrictEquals(base32String(source
), base32
);
373 describe("base64Binary", () => {
374 it("[[Call]] returns the correct data", () => {
376 new Uint8Array(base64Binary("")),
381 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
383 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
384 ($) => $.charCodeAt(0),
389 new Uint16Array(base64Binary("R/Q=")),
390 new Uint16Array([62535]),
394 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
395 new Uint8ClampedArray([75, 73, 66, 73]),
399 new Uint8Array(base64Binary("YmFzZTY0")),
400 new Uint8Array([98, 97, 115, 101, 54, 52]),
404 new Uint8Array(base64Binary("FPucA9l+")),
405 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
409 new Uint8Array(base64Binary("FPucA9k=")),
410 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
414 new Uint8Array(base64Binary("FPucAw==")),
415 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
419 new Uint8Array(base64Binary("KA==")),
420 new Uint8Array([0x28]),
424 new Uint8Array(base64Binary("1g==")),
425 new Uint8Array([0xD6]),
429 new Uint16Array(base64Binary("1vg")),
430 new Uint16Array([0xF8D6]),
434 new Uint8Array(base64Binary("1vgA")),
435 new Uint8Array([0xD6, 0xF8, 0x00]),
439 new Uint8Array(base64Binary("1vgQ")),
440 new Uint8Array([0xD6, 0xF8, 0x10]),
444 new Uint32Array(base64Binary("1vgRDA==")),
445 new Uint32Array([0x0C11F8D6]),
449 new Uint8Array(base64Binary("1vgRDIA=")),
450 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
454 new Uint16Array(base64Binary("1vgRDIUw")),
455 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
460 it("[[Call]] throws when provided with an invalid character", () => {
461 assertThrows(() => base64Binary("abc_"));
464 it("[[Call]] throws when provided with an invalid equals", () => {
465 assertThrows(() => base64Binary("abc=="));
468 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
469 assertThrows(() => base64Binary("abcde"));
470 assertThrows(() => base64Binary("abcde==="));
474 describe("base64String", () => {
475 it("[[Call]] returns the correct string", () => {
476 for (const [source
, { base64
}] of data
) {
477 assertStrictEquals(base64String(source
), base64
);
482 describe("filenameSafeBase64Binary", () => {
483 it("[[Call]] returns the correct data", () => {
485 new Uint8Array(filenameSafeBase64Binary("")),
490 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
492 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
493 ($) => $.charCodeAt(0),
498 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
499 new Uint16Array([62535]),
503 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
504 new Uint8ClampedArray([75, 73, 66, 73]),
508 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
509 new Uint8Array([98, 97, 115, 101, 54, 52]),
513 new Uint8Array(filenameSafeBase64Binary("KA==")),
514 new Uint8Array([0x28]),
518 new Uint8Array(filenameSafeBase64Binary("1g==")),
519 new Uint8Array([0xD6]),
523 new Uint16Array(filenameSafeBase64Binary("1vg")),
524 new Uint16Array([0xF8D6]),
528 new Uint8Array(filenameSafeBase64Binary("1vgA")),
529 new Uint8Array([0xD6, 0xF8, 0x00]),
533 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
534 new Uint8Array([0xD6, 0xF8, 0x10]),
538 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
539 new Uint32Array([0x0C10F8D6]),
543 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
544 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
548 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
549 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
554 it("[[Call]] throws when provided with an invalid character", () => {
555 assertThrows(() => filenameSafeBase64Binary("abc/"));
558 it("[[Call]] throws when provided with an invalid equals", () => {
559 assertThrows(() => filenameSafeBase64Binary("abc=="));
562 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
563 assertThrows(() => filenameSafeBase64Binary("abcde"));
564 assertThrows(() => filenameSafeBase64Binary("abcde==="));
568 describe("filenameSafeBase64String", () => {
569 it("[[Call]] returns the correct string", () => {
570 for (const [source
, { base64
}] of data
) {
572 filenameSafeBase64String(source
),
573 base64
.replace("+", "-").replace("/", "_"),
579 describe("isBase16", () => {
580 it("[[Call]] returns true for base64 strings", () => {
581 for (const { base16
} of data
.values()) {
582 assert(isBase16(base16
));
583 assert(isBase16(base16
.toLowerCase()));
587 it("[[Call]] returns false for others", () => {
603 ].forEach((value
) => assert(!isBase16(value
)));
607 describe("isBase32", () => {
608 it("[[Call]] returns true for base32 strings", () => {
609 for (const { base32
} of data
.values()) {
610 assert(isBase32(base32
));
611 assert(isBase32(base32
.toLowerCase()));
615 it("[[Call]] returns false for others", () => {
630 ].forEach((value
) => assert(!isBase32(value
)));
634 describe("isBase64", () => {
635 it("[[Call]] returns true for base64 strings", () => {
636 for (const { base64
} of data
.values()) {
637 assert(isBase64(base64
));
641 it("[[Call]] returns false for others", () => {
656 ].forEach((value
) => assert(!isBase64(value
)));
660 describe("isFilenameSafeBase64", () => {
661 it("[[Call]] returns true for filename‐safe base64 strings", () => {
662 for (const { base64
} of data
.values()) {
664 isFilenameSafeBase64(
665 base64
.replace("+", "-").replace("/", "_"),
671 it("[[Call]] returns false for others", () => {
686 ].forEach((value
) => assert(!isFilenameSafeBase64(value
)));
690 describe("isWRMGBase32", () => {
691 it("[[Call]] returns true for W·R·M·G base32 strings", () => {
692 for (const { wrmg
} of data
.values()) {
693 assert(isWRMGBase32(wrmg
));
694 assert(isWRMGBase32(wrmg
.toLowerCase()));
695 assert(isWRMGBase32(`--${wrmg}--`));
696 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "I")));
697 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "L")));
698 assert(isWRMGBase32(wrmg
.replaceAll(/0/gu
, "O")));
699 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "i")));
700 assert(isWRMGBase32(wrmg
.replaceAll(/1/gu
, "l")));
701 assert(isWRMGBase32(wrmg
.replaceAll(/0/gu
, "o")));
702 assert(isWRMGBase32(wrmg
.replaceAll(/./gu
, ($) => {
703 const rand
= Math
.random();
715 it("[[Call]] returns false for others", () => {
730 ].forEach((value
) => assert(!isWRMGBase32(value
)));
734 describe("wrmgBase32Binary", () => {
735 it("[[Call]] returns the correct data", () => {
737 new Uint8Array(wrmgBase32Binary("")),
742 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
744 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
745 ($) => $.charCodeAt(0),
747 "01H00R80EC06A01P00T0",
750 new Uint16Array(wrmgBase32Binary("8ZT0")),
751 new Uint16Array([62535]),
755 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
756 new Uint8ClampedArray([75, 73, 66, 73]),
760 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
761 new Uint8Array([98, 97, 115, 101, 54, 52]),
765 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
766 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
770 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
771 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
775 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
776 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
780 new Uint8Array(wrmgBase32Binary("50")),
781 new Uint8Array([0x28]),
785 new Uint8Array(wrmgBase32Binary("TR")),
786 new Uint8Array([0xD6]),
790 new Uint16Array(wrmgBase32Binary("TVW0")),
791 new Uint16Array([0xF8D6]),
795 new Uint8Array(wrmgBase32Binary("TVW00")),
796 new Uint8Array([0xD6, 0xF8, 0x00]),
800 new Uint8Array(wrmgBase32Binary("TVW10")),
801 new Uint8Array([0xD6, 0xF8, 0x10]),
805 new Uint32Array(wrmgBase32Binary("TVW1230")),
806 new Uint32Array([0x0C11F8D6]),
810 new Uint8Array(wrmgBase32Binary("TVW12340")),
811 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
815 new Uint16Array(wrmgBase32Binary("TVW1234560")),
816 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
821 it("[[Call]] is case‐insensitive", () => {
823 new Uint8Array(wrmgBase32Binary("tVw12340")),
824 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
829 it("[[Call]] casts I, L & O", () => {
831 new Uint8Array(wrmgBase32Binary("TVWI234O")),
832 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
836 new Uint8Array(wrmgBase32Binary("TVWi234o")),
837 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
841 new Uint8Array(wrmgBase32Binary("TVWL234O")),
842 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
846 new Uint8Array(wrmgBase32Binary("TVWl234o")),
847 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
852 it("[[Call]] ignores hyphens", () => {
854 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
855 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
856 "--TVW---123-40-----",
860 it("[[Call]] throws when provided with an invalid character", () => {
861 assertThrows(() => wrmgBase32Binary("ABCu"));
862 assertThrows(() => wrmgBase32Binary("ABCU"));
863 assertThrows(() => wrmgBase32Binary("ABC="));
866 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
867 assertThrows(() => wrmgBase32Binary("234BCDEAA"));
868 assertThrows(() => wrmgBase32Binary("2-34-B--CD-EA-A-"));
872 describe("wrmgBase32String", () => {
873 it("[[Call]] returns the correct string", () => {
874 for (const [source
, { wrmg
}] of data
) {
875 assertStrictEquals(wrmgBase32String(source
), wrmg
);
This page took 0.126473 seconds and 5 git commands to generate.