]>
Lady’s Gitweb - Pisces/blob - binary.test.js
9d8c16993b8630711d05ebfa006e0e461e4cac40
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
,
41 // These tests assume a LITTLE‐ENDIAN environment.
42 const data
= new Map([
50 base16
: "006200610073006500360034",
51 base32
: "ABRAAYIAOMAGKABWAA2A====",
52 base64
: "AGIAYQBzAGUANgA0",
53 wrmg
: "01H00R80EC06A01P00T0",
55 [new Uint16Array([62535]), {
61 [new Uint8ClampedArray([75, 73, 66, 73]).buffer
, {
67 [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer
), {
68 base16
: "626173653634",
69 base32
: "MJQXGZJWGQ======",
74 // The following three examples are from RFC 3548.
75 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
76 base16
: "14FB9C03D97E",
77 base32
: "CT5ZYA6ZPY======",
81 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
87 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
94 // The following examples are from the Ruby base32 gem.
95 [new Uint8Array([0x28]), {
101 [new Uint8Array([0xD6]), {
107 [new Uint16Array([0xF8D6]), {
113 [new Uint8Array([0xD6, 0xF8, 0x00]), {
119 [new Uint8Array([0xD6, 0xF8, 0x10]), {
125 [new Uint32Array([0x0C11F8D6]), {
131 [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
132 base16
: "D6F8110C80",
137 [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
138 base16
: "D6F8110C8530",
139 base32
: "234BCDEFGA======",
145 describe("base16Binary", () => {
146 it("[[Call]] returns the correct data", () => {
148 new Uint8Array(base16Binary("")),
153 new Uint8Array(base16Binary("006200610073006500360034")),
155 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
156 ($) => $.charCodeAt(0),
158 "006200610073006500360034",
161 new Uint16Array(base16Binary("47F4")),
162 new Uint16Array([62535]),
166 new Uint8ClampedArray(base16Binary("4B494249")),
167 new Uint8ClampedArray([75, 73, 66, 73]),
171 new Uint8Array(base16Binary("626173653634")),
172 new Uint8Array([98, 97, 115, 101, 54, 52]),
176 new Uint8Array(base16Binary("14FB9C03D97E")),
177 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
181 new Uint8Array(base16Binary("14FB9C03D9")),
182 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
186 new Uint8Array(base16Binary("14FB9C03")),
187 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
191 new Uint8Array(base16Binary("28")),
192 new Uint8Array([0x28]),
196 new Uint8Array(base16Binary("D6")),
197 new Uint8Array([0xD6]),
201 new Uint16Array(base16Binary("D6F8")),
202 new Uint16Array([0xF8D6]),
206 new Uint8Array(base16Binary("D6F800")),
207 new Uint8Array([0xD6, 0xF8, 0x00]),
211 new Uint8Array(base16Binary("D6F810")),
212 new Uint8Array([0xD6, 0xF8, 0x10]),
216 new Uint32Array(base16Binary("D6F8110C")),
217 new Uint32Array([0x0C11F8D6]),
221 new Uint8Array(base16Binary("D6F8110C80")),
222 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
226 new Uint16Array(base16Binary("D6F8110C8530")),
227 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
232 it("[[Call]] is case‐insensitive", () => {
234 new Uint8Array(base16Binary("d6f8110C80")),
235 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
240 it("[[Call]] throws when provided with an invalid character", () => {
241 assertThrows(() => base16Binary("ABCG"));
244 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 2", () => {
245 assertThrows(() => base16Binary("A"));
246 assertThrows(() => base16Binary("ABC"));
250 describe("base16String", () => {
251 it("[[Call]] returns the correct string", () => {
252 for (const [source
, { base16
}] of data
) {
253 assertStrictEquals(base16String(source
), base16
);
258 describe("base32Binary", () => {
259 it("[[Call]] returns the correct data", () => {
261 new Uint8Array(base32Binary("")),
266 new Uint8Array(base32Binary("ABRAAYIAOMAGKABWAA2A====")),
268 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
269 ($) => $.charCodeAt(0),
271 "ABRAAYIAOMAGKABWAA2A====",
274 new Uint16Array(base32Binary("I72A====")),
275 new Uint16Array([62535]),
279 new Uint8ClampedArray(base32Binary("JNEUESI=")),
280 new Uint8ClampedArray([75, 73, 66, 73]),
284 new Uint8Array(base32Binary("MJQXGZJWGQ======")),
285 new Uint8Array([98, 97, 115, 101, 54, 52]),
289 new Uint8Array(base32Binary("CT5ZYA6ZPY======")),
290 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
294 new Uint8Array(base32Binary("CT5ZYA6Z")),
295 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
299 new Uint8Array(base32Binary("CT5ZYAY=")),
300 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
304 new Uint8Array(base32Binary("FA======")),
305 new Uint8Array([0x28]),
309 new Uint8Array(base32Binary("2Y======")),
310 new Uint8Array([0xD6]),
314 new Uint16Array(base32Binary("234A====")),
315 new Uint16Array([0xF8D6]),
319 new Uint8Array(base32Binary("234AA===")),
320 new Uint8Array([0xD6, 0xF8, 0x00]),
324 new Uint8Array(base32Binary("234BA===")),
325 new Uint8Array([0xD6, 0xF8, 0x10]),
329 new Uint32Array(base32Binary("234BCDA=")),
330 new Uint32Array([0x0C11F8D6]),
334 new Uint8Array(base32Binary("234BCDEA")),
335 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
339 new Uint16Array(base32Binary("234BCDEFGA======")),
340 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
345 it("[[Call]] is case‐insensitive", () => {
347 new Uint8Array(base32Binary("234bcdEA")),
348 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
353 it("[[Call]] throws when provided with an invalid character", () => {
354 assertThrows(() => base32Binary("ABC0"));
355 assertThrows(() => base32Binary("ABC1"));
356 assertThrows(() => base32Binary("ABC8"));
357 assertThrows(() => base32Binary("ABC9"));
360 it("[[Call]] throws when provided with an invalid equals", () => {
361 assertThrows(() => base32Binary("CT3ZYAY=="));
364 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
365 assertThrows(() => base32Binary("234BCDEAA"));
366 assertThrows(() => base32Binary("234BCDEAA======="));
367 assertThrows(() => base32Binary("UHI"));
368 assertThrows(() => base32Binary("UHIUJD"));
372 describe("base32String", () => {
373 it("[[Call]] returns the correct string", () => {
374 for (const [source
, { base32
}] of data
) {
375 assertStrictEquals(base32String(source
), base32
);
380 describe("base64Binary", () => {
381 it("[[Call]] returns the correct data", () => {
383 new Uint8Array(base64Binary("")),
388 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
390 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
391 ($) => $.charCodeAt(0),
396 new Uint16Array(base64Binary("R/Q=")),
397 new Uint16Array([62535]),
401 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
402 new Uint8ClampedArray([75, 73, 66, 73]),
406 new Uint8Array(base64Binary("YmFzZTY0")),
407 new Uint8Array([98, 97, 115, 101, 54, 52]),
411 new Uint8Array(base64Binary("FPucA9l+")),
412 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
416 new Uint8Array(base64Binary("FPucA9k=")),
417 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
421 new Uint8Array(base64Binary("FPucAw==")),
422 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
426 new Uint8Array(base64Binary("KA==")),
427 new Uint8Array([0x28]),
431 new Uint8Array(base64Binary("1g==")),
432 new Uint8Array([0xD6]),
436 new Uint16Array(base64Binary("1vg")),
437 new Uint16Array([0xF8D6]),
441 new Uint8Array(base64Binary("1vgA")),
442 new Uint8Array([0xD6, 0xF8, 0x00]),
446 new Uint8Array(base64Binary("1vgQ")),
447 new Uint8Array([0xD6, 0xF8, 0x10]),
451 new Uint32Array(base64Binary("1vgRDA==")),
452 new Uint32Array([0x0C11F8D6]),
456 new Uint8Array(base64Binary("1vgRDIA=")),
457 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
461 new Uint16Array(base64Binary("1vgRDIUw")),
462 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
467 it("[[Call]] throws when provided with an invalid character", () => {
468 assertThrows(() => base64Binary("abc_"));
471 it("[[Call]] throws when provided with an invalid equals", () => {
472 assertThrows(() => base64Binary("abc=="));
475 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
476 assertThrows(() => base64Binary("abcde"));
477 assertThrows(() => base64Binary("abcde==="));
481 describe("base64String", () => {
482 it("[[Call]] returns the correct string", () => {
483 for (const [source
, { base64
}] of data
) {
484 assertStrictEquals(base64String(source
), base64
);
489 describe("filenameSafeBase64Binary", () => {
490 it("[[Call]] returns the correct data", () => {
492 new Uint8Array(filenameSafeBase64Binary("")),
497 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
499 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
500 ($) => $.charCodeAt(0),
505 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
506 new Uint16Array([62535]),
510 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
511 new Uint8ClampedArray([75, 73, 66, 73]),
515 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
516 new Uint8Array([98, 97, 115, 101, 54, 52]),
520 new Uint8Array(filenameSafeBase64Binary("KA==")),
521 new Uint8Array([0x28]),
525 new Uint8Array(filenameSafeBase64Binary("1g==")),
526 new Uint8Array([0xD6]),
530 new Uint16Array(filenameSafeBase64Binary("1vg")),
531 new Uint16Array([0xF8D6]),
535 new Uint8Array(filenameSafeBase64Binary("1vgA")),
536 new Uint8Array([0xD6, 0xF8, 0x00]),
540 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
541 new Uint8Array([0xD6, 0xF8, 0x10]),
545 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
546 new Uint32Array([0x0C10F8D6]),
550 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
551 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
555 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
556 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
561 it("[[Call]] throws when provided with an invalid character", () => {
562 assertThrows(() => filenameSafeBase64Binary("abc/"));
565 it("[[Call]] throws when provided with an invalid equals", () => {
566 assertThrows(() => filenameSafeBase64Binary("abc=="));
569 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
570 assertThrows(() => filenameSafeBase64Binary("abcde"));
571 assertThrows(() => filenameSafeBase64Binary("abcde==="));
575 describe("filenameSafeBase64String", () => {
576 it("[[Call]] returns the correct string", () => {
577 for (const [source
, { base64
}] of data
) {
579 filenameSafeBase64String(source
),
580 base64
.replace("+", "-").replace("/", "_"),
586 describe("isArrayBuffer", () => {
587 it("[[Call]] returns true for array buffers", () => {
589 isArrayBuffer(new ArrayBuffer()),
594 it("[[Call]] returns false for others", () => {
607 new SharedArrayBuffer(),
608 new DataView(new ArrayBuffer()),
611 assertStrictEquals(isArrayBuffer(value
), false)
616 describe("isArrayBufferView", () => {
617 it("[[Call]] returns true for data views", () => {
619 isArrayBufferView(new DataView(new ArrayBuffer())),
624 it("[[Call]] returns true for typed arrays", () => {
626 isArrayBufferView(new Uint8ClampedArray()),
629 assertStrictEquals(isArrayBufferView(new BigInt64Array()), true);
632 it("[[Call]] returns false for others", () => {
646 new SharedArrayBuffer(),
648 assertStrictEquals(isArrayBufferView(value
), false)
653 describe("isBase16", () => {
654 it("[[Call]] returns true for base64 strings", () => {
655 for (const { base16
} of data
.values()) {
656 assertStrictEquals(isBase16(base16
), true);
657 assertStrictEquals(isBase16(base16
.toLowerCase()), true);
661 it("[[Call]] returns false for others", () => {
677 ].forEach((value
) => assertStrictEquals(isBase16(value
), false));
681 describe("isBase32", () => {
682 it("[[Call]] returns true for base32 strings", () => {
683 for (const { base32
} of data
.values()) {
684 assertStrictEquals(isBase32(base32
), true);
685 assertStrictEquals(isBase32(base32
.toLowerCase()), true);
689 it("[[Call]] returns false for others", () => {
704 ].forEach((value
) => assertStrictEquals(isBase32(value
), false));
708 describe("isBase64", () => {
709 it("[[Call]] returns true for base64 strings", () => {
710 for (const { base64
} of data
.values()) {
711 assertStrictEquals(isBase64(base64
), true);
715 it("[[Call]] returns false for others", () => {
730 ].forEach((value
) => assertStrictEquals(isBase64(value
), false));
734 describe("isDataView", () => {
735 it("[[Call]] returns true for data views", () => {
737 isDataView(new DataView(new ArrayBuffer())),
742 it("[[Call]] returns false for others", () => {
756 new SharedArrayBuffer(),
758 ].forEach((value
) => assertStrictEquals(isDataView(value
), false));
762 describe("isFilenameSafeBase64", () => {
763 it("[[Call]] returns true for filename‐safe base64 strings", () => {
764 for (const { base64
} of data
.values()) {
766 isFilenameSafeBase64(
767 base64
.replace("+", "-").replace("/", "_"),
774 it("[[Call]] returns false for others", () => {
790 assertStrictEquals(isFilenameSafeBase64(value
), false)
795 describe("isTypedArray", () => {
796 it("[[Call]] returns true for typed arrays", () => {
798 isTypedArray(new Uint8Array()),
802 isTypedArray(new BigInt64Array()),
807 it("[[Call]] returns false for others", () => {
821 new SharedArrayBuffer(),
822 new DataView(new ArrayBuffer()),
824 assertStrictEquals(isTypedArray(value
), false)
829 describe("isWRMGBase32", () => {
830 it("[[Call]] returns true for W·R·M·G base32 strings", () => {
831 for (const { wrmg
} of data
.values()) {
832 assertStrictEquals(isWRMGBase32(wrmg
), true);
833 assertStrictEquals(isWRMGBase32(wrmg
.toLowerCase()), true);
834 assertStrictEquals(isWRMGBase32(`--${wrmg}--`), true);
836 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "I")),
840 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "L")),
844 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "O")),
848 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "i")),
852 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "l")),
856 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "o")),
860 isWRMGBase32(wrmg
.replaceAll(/./gu
, ($) => {
861 const rand
= Math
.random();
875 it("[[Call]] returns false for others", () => {
891 assertStrictEquals(isWRMGBase32(value
), false)
896 describe("toArrayBuffer", () => {
897 it("[[Call]] returns the argument for array buffers", () => {
898 const buffer
= new ArrayBuffer();
899 assertStrictEquals(toArrayBuffer(buffer
), buffer
);
902 it("[[Call]] returns the buffer for data views", () => {
903 const src
= Uint8Array
.from([2, 3, 1]);
904 const buffer
= toArrayBuffer(new DataView(src
.buffer
));
905 assert(buffer
instanceof ArrayBuffer
);
906 assertEquals(new Uint8Array(buffer
), src
);
909 it("[[Call]] returns the buffer for typed arrays", () => {
910 const src
= Uint8Array
.from([2, 3, 1]);
911 const buffer
= toArrayBuffer(src
);
912 assert(buffer
instanceof ArrayBuffer
);
913 assertEquals(new Uint8Array(buffer
), src
);
916 it("[[Call]] throws for others", () => {
931 toArrayBuffer(value
);
937 describe("wrmgBase32Binary", () => {
938 it("[[Call]] returns the correct data", () => {
940 new Uint8Array(wrmgBase32Binary("")),
945 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
947 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
948 ($) => $.charCodeAt(0),
950 "01H00R80EC06A01P00T0",
953 new Uint16Array(wrmgBase32Binary("8ZT0")),
954 new Uint16Array([62535]),
958 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
959 new Uint8ClampedArray([75, 73, 66, 73]),
963 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
964 new Uint8Array([98, 97, 115, 101, 54, 52]),
968 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
969 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
973 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
974 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
978 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
979 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
983 new Uint8Array(wrmgBase32Binary("50")),
984 new Uint8Array([0x28]),
988 new Uint8Array(wrmgBase32Binary("TR")),
989 new Uint8Array([0xD6]),
993 new Uint16Array(wrmgBase32Binary("TVW0")),
994 new Uint16Array([0xF8D6]),
998 new Uint8Array(wrmgBase32Binary("TVW00")),
999 new Uint8Array([0xD6, 0xF8, 0x00]),
1003 new Uint8Array(wrmgBase32Binary("TVW10")),
1004 new Uint8Array([0xD6, 0xF8, 0x10]),
1008 new Uint32Array(wrmgBase32Binary("TVW1230")),
1009 new Uint32Array([0x0C11F8D6]),
1013 new Uint8Array(wrmgBase32Binary("TVW12340")),
1014 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1018 new Uint16Array(wrmgBase32Binary("TVW1234560")),
1019 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
1024 it("[[Call]] is case‐insensitive", () => {
1026 new Uint8Array(wrmgBase32Binary("tVw12340")),
1027 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1032 it("[[Call]] casts I, L & O", () => {
1034 new Uint8Array(wrmgBase32Binary("TVWI234O")),
1035 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1039 new Uint8Array(wrmgBase32Binary("TVWi234o")),
1040 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1044 new Uint8Array(wrmgBase32Binary("TVWL234O")),
1045 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1049 new Uint8Array(wrmgBase32Binary("TVWl234o")),
1050 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1055 it("[[Call]] ignores hyphens", () => {
1057 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
1058 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1059 "--TVW---123-40-----",
1063 it("[[Call]] throws when provided with an invalid character", () => {
1064 assertThrows(() => wrmgBase32Binary("ABCu"));
1065 assertThrows(() => wrmgBase32Binary("ABCU"));
1066 assertThrows(() => wrmgBase32Binary("ABC="));
1069 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
1070 assertThrows(() => base32Binary("TVW123400"));
1071 assertThrows(() => base32Binary("TVW123400======="));
1072 assertThrows(() => base32Binary("M78"));
1073 assertThrows(() => base32Binary("M78M93"));
1076 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
1077 assertThrows(() => wrmgBase32Binary("234BCDEAA"));
1078 assertThrows(() => wrmgBase32Binary("2-34-B--CD-EA-A-"));
1082 describe("wrmgBase32String", () => {
1083 it("[[Call]] returns the correct string", () => {
1084 for (const [source
, { wrmg
}] of data
) {
1085 assertStrictEquals(wrmgBase32String(source
), wrmg
);
This page took 0.245875 seconds and 3 git commands to generate.