]>
Lady’s Gitweb - Pisces/blob - binary.test.js
1a0920f38a9a43025d64dc2f9173648d0ece4ee2
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
,
38 // These tests assume a LITTLE‐ENDIAN environment.
39 const data
= new Map([
47 base16
: "006200610073006500360034",
48 base32
: "ABRAAYIAOMAGKABWAA2A====",
49 base64
: "AGIAYQBzAGUANgA0",
50 wrmg
: "01H00R80EC06A01P00T0",
52 [new Uint16Array([62535]), {
58 [new Uint8ClampedArray([75, 73, 66, 73]).buffer
, {
64 [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer
), {
65 base16
: "626173653634",
66 base32
: "MJQXGZJWGQ======",
71 // The following three examples are from RFC 3548.
72 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
73 base16
: "14FB9C03D97E",
74 base32
: "CT5ZYA6ZPY======",
78 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
84 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
91 // The following examples are from the Ruby base32 gem.
92 [new Uint8Array([0x28]), {
98 [new Uint8Array([0xD6]), {
104 [new Uint16Array([0xF8D6]), {
110 [new Uint8Array([0xD6, 0xF8, 0x00]), {
116 [new Uint8Array([0xD6, 0xF8, 0x10]), {
122 [new Uint32Array([0x0C11F8D6]), {
128 [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
129 base16
: "D6F8110C80",
134 [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
135 base16
: "D6F8110C8530",
136 base32
: "234BCDEFGA======",
142 describe("base16Binary", () => {
143 it("[[Call]] returns the correct data", () => {
145 new Uint8Array(base16Binary("")),
150 new Uint8Array(base16Binary("006200610073006500360034")),
152 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
153 ($) => $.charCodeAt(0),
155 "006200610073006500360034",
158 new Uint16Array(base16Binary("47F4")),
159 new Uint16Array([62535]),
163 new Uint8ClampedArray(base16Binary("4B494249")),
164 new Uint8ClampedArray([75, 73, 66, 73]),
168 new Uint8Array(base16Binary("626173653634")),
169 new Uint8Array([98, 97, 115, 101, 54, 52]),
173 new Uint8Array(base16Binary("14FB9C03D97E")),
174 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
178 new Uint8Array(base16Binary("14FB9C03D9")),
179 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
183 new Uint8Array(base16Binary("14FB9C03")),
184 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
188 new Uint8Array(base16Binary("28")),
189 new Uint8Array([0x28]),
193 new Uint8Array(base16Binary("D6")),
194 new Uint8Array([0xD6]),
198 new Uint16Array(base16Binary("D6F8")),
199 new Uint16Array([0xF8D6]),
203 new Uint8Array(base16Binary("D6F800")),
204 new Uint8Array([0xD6, 0xF8, 0x00]),
208 new Uint8Array(base16Binary("D6F810")),
209 new Uint8Array([0xD6, 0xF8, 0x10]),
213 new Uint32Array(base16Binary("D6F8110C")),
214 new Uint32Array([0x0C11F8D6]),
218 new Uint8Array(base16Binary("D6F8110C80")),
219 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
223 new Uint16Array(base16Binary("D6F8110C8530")),
224 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
229 it("[[Call]] is case‐insensitive", () => {
231 new Uint8Array(base16Binary("d6f8110C80")),
232 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
237 it("[[Call]] throws when provided with an invalid character", () => {
238 assertThrows(() => base16Binary("ABCG"));
241 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 2", () => {
242 assertThrows(() => base16Binary("A"));
243 assertThrows(() => base16Binary("ABC"));
247 describe("base16String", () => {
248 it("[[Call]] returns the correct string", () => {
249 for (const [source
, { base16
}] of data
) {
250 assertStrictEquals(base16String(source
), base16
);
255 describe("base32Binary", () => {
256 it("[[Call]] returns the correct data", () => {
258 new Uint8Array(base32Binary("")),
263 new Uint8Array(base32Binary("ABRAAYIAOMAGKABWAA2A====")),
265 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
266 ($) => $.charCodeAt(0),
268 "ABRAAYIAOMAGKABWAA2A====",
271 new Uint16Array(base32Binary("I72A====")),
272 new Uint16Array([62535]),
276 new Uint8ClampedArray(base32Binary("JNEUESI=")),
277 new Uint8ClampedArray([75, 73, 66, 73]),
281 new Uint8Array(base32Binary("MJQXGZJWGQ======")),
282 new Uint8Array([98, 97, 115, 101, 54, 52]),
286 new Uint8Array(base32Binary("CT5ZYA6ZPY======")),
287 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
291 new Uint8Array(base32Binary("CT5ZYA6Z")),
292 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
296 new Uint8Array(base32Binary("CT5ZYAY=")),
297 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
301 new Uint8Array(base32Binary("FA======")),
302 new Uint8Array([0x28]),
306 new Uint8Array(base32Binary("2Y======")),
307 new Uint8Array([0xD6]),
311 new Uint16Array(base32Binary("234A====")),
312 new Uint16Array([0xF8D6]),
316 new Uint8Array(base32Binary("234AA===")),
317 new Uint8Array([0xD6, 0xF8, 0x00]),
321 new Uint8Array(base32Binary("234BA===")),
322 new Uint8Array([0xD6, 0xF8, 0x10]),
326 new Uint32Array(base32Binary("234BCDA=")),
327 new Uint32Array([0x0C11F8D6]),
331 new Uint8Array(base32Binary("234BCDEA")),
332 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
336 new Uint16Array(base32Binary("234BCDEFGA======")),
337 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
342 it("[[Call]] is case‐insensitive", () => {
344 new Uint8Array(base32Binary("234bcdEA")),
345 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
350 it("[[Call]] throws when provided with an invalid character", () => {
351 assertThrows(() => base32Binary("ABC0"));
352 assertThrows(() => base32Binary("ABC1"));
353 assertThrows(() => base32Binary("ABC8"));
354 assertThrows(() => base32Binary("ABC9"));
357 it("[[Call]] throws when provided with an invalid equals", () => {
358 assertThrows(() => base32Binary("CT3ZYAY=="));
361 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
362 assertThrows(() => base32Binary("234BCDEAA"));
363 assertThrows(() => base32Binary("234BCDEAA======="));
364 assertThrows(() => base32Binary("UHI"));
365 assertThrows(() => base32Binary("UHIUJD"));
369 describe("base32String", () => {
370 it("[[Call]] returns the correct string", () => {
371 for (const [source
, { base32
}] of data
) {
372 assertStrictEquals(base32String(source
), base32
);
377 describe("base64Binary", () => {
378 it("[[Call]] returns the correct data", () => {
380 new Uint8Array(base64Binary("")),
385 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
387 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
388 ($) => $.charCodeAt(0),
393 new Uint16Array(base64Binary("R/Q=")),
394 new Uint16Array([62535]),
398 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
399 new Uint8ClampedArray([75, 73, 66, 73]),
403 new Uint8Array(base64Binary("YmFzZTY0")),
404 new Uint8Array([98, 97, 115, 101, 54, 52]),
408 new Uint8Array(base64Binary("FPucA9l+")),
409 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
413 new Uint8Array(base64Binary("FPucA9k=")),
414 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
418 new Uint8Array(base64Binary("FPucAw==")),
419 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
423 new Uint8Array(base64Binary("KA==")),
424 new Uint8Array([0x28]),
428 new Uint8Array(base64Binary("1g==")),
429 new Uint8Array([0xD6]),
433 new Uint16Array(base64Binary("1vg")),
434 new Uint16Array([0xF8D6]),
438 new Uint8Array(base64Binary("1vgA")),
439 new Uint8Array([0xD6, 0xF8, 0x00]),
443 new Uint8Array(base64Binary("1vgQ")),
444 new Uint8Array([0xD6, 0xF8, 0x10]),
448 new Uint32Array(base64Binary("1vgRDA==")),
449 new Uint32Array([0x0C11F8D6]),
453 new Uint8Array(base64Binary("1vgRDIA=")),
454 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
458 new Uint16Array(base64Binary("1vgRDIUw")),
459 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
464 it("[[Call]] throws when provided with an invalid character", () => {
465 assertThrows(() => base64Binary("abc_"));
468 it("[[Call]] throws when provided with an invalid equals", () => {
469 assertThrows(() => base64Binary("abc=="));
472 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
473 assertThrows(() => base64Binary("abcde"));
474 assertThrows(() => base64Binary("abcde==="));
478 describe("base64String", () => {
479 it("[[Call]] returns the correct string", () => {
480 for (const [source
, { base64
}] of data
) {
481 assertStrictEquals(base64String(source
), base64
);
486 describe("filenameSafeBase64Binary", () => {
487 it("[[Call]] returns the correct data", () => {
489 new Uint8Array(filenameSafeBase64Binary("")),
494 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
496 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
497 ($) => $.charCodeAt(0),
502 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
503 new Uint16Array([62535]),
507 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
508 new Uint8ClampedArray([75, 73, 66, 73]),
512 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
513 new Uint8Array([98, 97, 115, 101, 54, 52]),
517 new Uint8Array(filenameSafeBase64Binary("KA==")),
518 new Uint8Array([0x28]),
522 new Uint8Array(filenameSafeBase64Binary("1g==")),
523 new Uint8Array([0xD6]),
527 new Uint16Array(filenameSafeBase64Binary("1vg")),
528 new Uint16Array([0xF8D6]),
532 new Uint8Array(filenameSafeBase64Binary("1vgA")),
533 new Uint8Array([0xD6, 0xF8, 0x00]),
537 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
538 new Uint8Array([0xD6, 0xF8, 0x10]),
542 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
543 new Uint32Array([0x0C10F8D6]),
547 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
548 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
552 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
553 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
558 it("[[Call]] throws when provided with an invalid character", () => {
559 assertThrows(() => filenameSafeBase64Binary("abc/"));
562 it("[[Call]] throws when provided with an invalid equals", () => {
563 assertThrows(() => filenameSafeBase64Binary("abc=="));
566 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
567 assertThrows(() => filenameSafeBase64Binary("abcde"));
568 assertThrows(() => filenameSafeBase64Binary("abcde==="));
572 describe("filenameSafeBase64String", () => {
573 it("[[Call]] returns the correct string", () => {
574 for (const [source
, { base64
}] of data
) {
576 filenameSafeBase64String(source
),
577 base64
.replace("+", "-").replace("/", "_"),
583 describe("isArrayBufferView", () => {
584 it("[[Call]] returns true for data views", () => {
586 isArrayBufferView(new DataView(new ArrayBuffer())),
591 it("[[Call]] returns true for typed arrays", () => {
593 isArrayBufferView(new Uint8ClampedArray()),
596 assertStrictEquals(isArrayBufferView(new BigInt64Array()), true);
599 it("[[Call]] returns false for others", () => {
613 new SharedArrayBuffer(),
615 assertStrictEquals(isArrayBufferView(value
), false)
620 describe("isBase16", () => {
621 it("[[Call]] returns true for base64 strings", () => {
622 for (const { base16
} of data
.values()) {
623 assertStrictEquals(isBase16(base16
), true);
624 assertStrictEquals(isBase16(base16
.toLowerCase()), true);
628 it("[[Call]] returns false for others", () => {
644 ].forEach((value
) => assertStrictEquals(isBase16(value
), false));
648 describe("isBase32", () => {
649 it("[[Call]] returns true for base32 strings", () => {
650 for (const { base32
} of data
.values()) {
651 assertStrictEquals(isBase32(base32
), true);
652 assertStrictEquals(isBase32(base32
.toLowerCase()), true);
656 it("[[Call]] returns false for others", () => {
671 ].forEach((value
) => assertStrictEquals(isBase32(value
), false));
675 describe("isBase64", () => {
676 it("[[Call]] returns true for base64 strings", () => {
677 for (const { base64
} of data
.values()) {
678 assertStrictEquals(isBase64(base64
), true);
682 it("[[Call]] returns false for others", () => {
697 ].forEach((value
) => assertStrictEquals(isBase64(value
), false));
701 describe("isFilenameSafeBase64", () => {
702 it("[[Call]] returns true for filename‐safe base64 strings", () => {
703 for (const { base64
} of data
.values()) {
705 isFilenameSafeBase64(
706 base64
.replace("+", "-").replace("/", "_"),
713 it("[[Call]] returns false for others", () => {
729 assertStrictEquals(isFilenameSafeBase64(value
), false)
734 describe("isWRMGBase32", () => {
735 it("[[Call]] returns true for W·R·M·G base32 strings", () => {
736 for (const { wrmg
} of data
.values()) {
737 assertStrictEquals(isWRMGBase32(wrmg
), true);
738 assertStrictEquals(isWRMGBase32(wrmg
.toLowerCase()), true);
739 assertStrictEquals(isWRMGBase32(`--${wrmg}--`), true);
741 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "I")),
745 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "L")),
749 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "O")),
753 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "i")),
757 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "l")),
761 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "o")),
765 isWRMGBase32(wrmg
.replaceAll(/./gu
, ($) => {
766 const rand
= Math
.random();
780 it("[[Call]] returns false for others", () => {
796 assertStrictEquals(isWRMGBase32(value
), false)
801 describe("toArrayBuffer", () => {
802 it("[[Call]] returns the argument for array buffers", () => {
803 const buffer
= new ArrayBuffer();
804 assertStrictEquals(toArrayBuffer(buffer
), buffer
);
807 it("[[Call]] returns the buffer for data views", () => {
808 const src
= Uint8Array
.from([2, 3, 1]);
809 const buffer
= toArrayBuffer(new DataView(src
.buffer
));
810 assert(buffer
instanceof ArrayBuffer
);
811 assertEquals(new Uint8Array(buffer
), src
);
814 it("[[Call]] returns the buffer for typed arrays", () => {
815 const src
= Uint8Array
.from([2, 3, 1]);
816 const buffer
= toArrayBuffer(src
);
817 assert(buffer
instanceof ArrayBuffer
);
818 assertEquals(new Uint8Array(buffer
), src
);
821 it("[[Call]] throws for others", () => {
836 toArrayBuffer(value
);
842 describe("wrmgBase32Binary", () => {
843 it("[[Call]] returns the correct data", () => {
845 new Uint8Array(wrmgBase32Binary("")),
850 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
852 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
853 ($) => $.charCodeAt(0),
855 "01H00R80EC06A01P00T0",
858 new Uint16Array(wrmgBase32Binary("8ZT0")),
859 new Uint16Array([62535]),
863 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
864 new Uint8ClampedArray([75, 73, 66, 73]),
868 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
869 new Uint8Array([98, 97, 115, 101, 54, 52]),
873 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
874 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
878 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
879 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
883 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
884 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
888 new Uint8Array(wrmgBase32Binary("50")),
889 new Uint8Array([0x28]),
893 new Uint8Array(wrmgBase32Binary("TR")),
894 new Uint8Array([0xD6]),
898 new Uint16Array(wrmgBase32Binary("TVW0")),
899 new Uint16Array([0xF8D6]),
903 new Uint8Array(wrmgBase32Binary("TVW00")),
904 new Uint8Array([0xD6, 0xF8, 0x00]),
908 new Uint8Array(wrmgBase32Binary("TVW10")),
909 new Uint8Array([0xD6, 0xF8, 0x10]),
913 new Uint32Array(wrmgBase32Binary("TVW1230")),
914 new Uint32Array([0x0C11F8D6]),
918 new Uint8Array(wrmgBase32Binary("TVW12340")),
919 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
923 new Uint16Array(wrmgBase32Binary("TVW1234560")),
924 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
929 it("[[Call]] is case‐insensitive", () => {
931 new Uint8Array(wrmgBase32Binary("tVw12340")),
932 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
937 it("[[Call]] casts I, L & O", () => {
939 new Uint8Array(wrmgBase32Binary("TVWI234O")),
940 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
944 new Uint8Array(wrmgBase32Binary("TVWi234o")),
945 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
949 new Uint8Array(wrmgBase32Binary("TVWL234O")),
950 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
954 new Uint8Array(wrmgBase32Binary("TVWl234o")),
955 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
960 it("[[Call]] ignores hyphens", () => {
962 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
963 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
964 "--TVW---123-40-----",
968 it("[[Call]] throws when provided with an invalid character", () => {
969 assertThrows(() => wrmgBase32Binary("ABCu"));
970 assertThrows(() => wrmgBase32Binary("ABCU"));
971 assertThrows(() => wrmgBase32Binary("ABC="));
974 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
975 assertThrows(() => base32Binary("TVW123400"));
976 assertThrows(() => base32Binary("TVW123400======="));
977 assertThrows(() => base32Binary("M78"));
978 assertThrows(() => base32Binary("M78M93"));
981 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
982 assertThrows(() => wrmgBase32Binary("234BCDEAA"));
983 assertThrows(() => wrmgBase32Binary("2-34-B--CD-EA-A-"));
987 describe("wrmgBase32String", () => {
988 it("[[Call]] returns the correct string", () => {
989 for (const [source
, { wrmg
}] of data
) {
990 assertStrictEquals(wrmgBase32String(source
), wrmg
);
This page took 0.236577 seconds and 3 git commands to generate.