]>
Lady’s Gitweb - Pisces/blob - binary.test.js
00860edbed74e01d98a9997fb773e0d667e84bfb
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()),
593 isArrayBuffer(new SharedArrayBuffer()),
598 it("[[Call]] returns false for others", () => {
611 new DataView(new ArrayBuffer()),
614 assertStrictEquals(isArrayBuffer(value
), false)
619 describe("isArrayBufferView", () => {
620 it("[[Call]] returns true for data views", () => {
622 isArrayBufferView(new DataView(new ArrayBuffer())),
627 it("[[Call]] returns true for typed arrays", () => {
629 isArrayBufferView(new Uint8ClampedArray()),
632 assertStrictEquals(isArrayBufferView(new BigInt64Array()), true);
635 it("[[Call]] returns false for others", () => {
649 new SharedArrayBuffer(),
651 assertStrictEquals(isArrayBufferView(value
), false)
656 describe("isBase16", () => {
657 it("[[Call]] returns true for base64 strings", () => {
658 for (const { base16
} of data
.values()) {
659 assertStrictEquals(isBase16(base16
), true);
660 assertStrictEquals(isBase16(base16
.toLowerCase()), true);
664 it("[[Call]] returns false for others", () => {
680 ].forEach((value
) => assertStrictEquals(isBase16(value
), false));
684 describe("isBase32", () => {
685 it("[[Call]] returns true for base32 strings", () => {
686 for (const { base32
} of data
.values()) {
687 assertStrictEquals(isBase32(base32
), true);
688 assertStrictEquals(isBase32(base32
.toLowerCase()), true);
692 it("[[Call]] returns false for others", () => {
707 ].forEach((value
) => assertStrictEquals(isBase32(value
), false));
711 describe("isBase64", () => {
712 it("[[Call]] returns true for base64 strings", () => {
713 for (const { base64
} of data
.values()) {
714 assertStrictEquals(isBase64(base64
), true);
718 it("[[Call]] returns false for others", () => {
733 ].forEach((value
) => assertStrictEquals(isBase64(value
), false));
737 describe("isFilenameSafeBase64", () => {
738 it("[[Call]] returns true for filename‐safe base64 strings", () => {
739 for (const { base64
} of data
.values()) {
741 isFilenameSafeBase64(
742 base64
.replace("+", "-").replace("/", "_"),
749 it("[[Call]] returns false for others", () => {
765 assertStrictEquals(isFilenameSafeBase64(value
), false)
770 describe("isSharedArrayBuffer", () => {
771 it("[[Call]] returns true for shared array buffers", () => {
773 isSharedArrayBuffer(new SharedArrayBuffer()),
778 it("[[Call]] returns false for others", () => {
792 new DataView(new ArrayBuffer()),
795 assertStrictEquals(isSharedArrayBuffer(value
), false)
800 describe("isTypedArray", () => {
801 it("[[Call]] returns true for typed arrays", () => {
803 isTypedArray(new Uint8Array()),
807 isTypedArray(new BigInt64Array()),
812 it("[[Call]] returns false for others", () => {
826 new SharedArrayBuffer(),
827 new DataView(new ArrayBuffer()),
829 assertStrictEquals(isTypedArray(value
), false)
834 describe("isWRMGBase32", () => {
835 it("[[Call]] returns true for W·R·M·G base32 strings", () => {
836 for (const { wrmg
} of data
.values()) {
837 assertStrictEquals(isWRMGBase32(wrmg
), true);
838 assertStrictEquals(isWRMGBase32(wrmg
.toLowerCase()), true);
839 assertStrictEquals(isWRMGBase32(`--${wrmg}--`), true);
841 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "I")),
845 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "L")),
849 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "O")),
853 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "i")),
857 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "l")),
861 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "o")),
865 isWRMGBase32(wrmg
.replaceAll(/./gu
, ($) => {
866 const rand
= Math
.random();
880 it("[[Call]] returns false for others", () => {
896 assertStrictEquals(isWRMGBase32(value
), false)
901 describe("toArrayBuffer", () => {
902 it("[[Call]] returns the argument for array buffers", () => {
903 const buffer
= new ArrayBuffer();
904 assertStrictEquals(toArrayBuffer(buffer
), buffer
);
907 it("[[Call]] returns the buffer for data views", () => {
908 const src
= Uint8Array
.from([2, 3, 1]);
909 const buffer
= toArrayBuffer(new DataView(src
.buffer
));
910 assert(buffer
instanceof ArrayBuffer
);
911 assertEquals(new Uint8Array(buffer
), src
);
914 it("[[Call]] returns the buffer for typed arrays", () => {
915 const src
= Uint8Array
.from([2, 3, 1]);
916 const buffer
= toArrayBuffer(src
);
917 assert(buffer
instanceof ArrayBuffer
);
918 assertEquals(new Uint8Array(buffer
), src
);
921 it("[[Call]] throws for others", () => {
936 toArrayBuffer(value
);
942 describe("wrmgBase32Binary", () => {
943 it("[[Call]] returns the correct data", () => {
945 new Uint8Array(wrmgBase32Binary("")),
950 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
952 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
953 ($) => $.charCodeAt(0),
955 "01H00R80EC06A01P00T0",
958 new Uint16Array(wrmgBase32Binary("8ZT0")),
959 new Uint16Array([62535]),
963 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
964 new Uint8ClampedArray([75, 73, 66, 73]),
968 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
969 new Uint8Array([98, 97, 115, 101, 54, 52]),
973 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
974 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
978 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
979 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
983 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
984 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
988 new Uint8Array(wrmgBase32Binary("50")),
989 new Uint8Array([0x28]),
993 new Uint8Array(wrmgBase32Binary("TR")),
994 new Uint8Array([0xD6]),
998 new Uint16Array(wrmgBase32Binary("TVW0")),
999 new Uint16Array([0xF8D6]),
1003 new Uint8Array(wrmgBase32Binary("TVW00")),
1004 new Uint8Array([0xD6, 0xF8, 0x00]),
1008 new Uint8Array(wrmgBase32Binary("TVW10")),
1009 new Uint8Array([0xD6, 0xF8, 0x10]),
1013 new Uint32Array(wrmgBase32Binary("TVW1230")),
1014 new Uint32Array([0x0C11F8D6]),
1018 new Uint8Array(wrmgBase32Binary("TVW12340")),
1019 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1023 new Uint16Array(wrmgBase32Binary("TVW1234560")),
1024 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
1029 it("[[Call]] is case‐insensitive", () => {
1031 new Uint8Array(wrmgBase32Binary("tVw12340")),
1032 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1037 it("[[Call]] casts I, L & O", () => {
1039 new Uint8Array(wrmgBase32Binary("TVWI234O")),
1040 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1044 new Uint8Array(wrmgBase32Binary("TVWi234o")),
1045 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1049 new Uint8Array(wrmgBase32Binary("TVWL234O")),
1050 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1054 new Uint8Array(wrmgBase32Binary("TVWl234o")),
1055 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1060 it("[[Call]] ignores hyphens", () => {
1062 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
1063 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1064 "--TVW---123-40-----",
1068 it("[[Call]] throws when provided with an invalid character", () => {
1069 assertThrows(() => wrmgBase32Binary("ABCu"));
1070 assertThrows(() => wrmgBase32Binary("ABCU"));
1071 assertThrows(() => wrmgBase32Binary("ABC="));
1074 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
1075 assertThrows(() => base32Binary("TVW123400"));
1076 assertThrows(() => base32Binary("TVW123400======="));
1077 assertThrows(() => base32Binary("M78"));
1078 assertThrows(() => base32Binary("M78M93"));
1081 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
1082 assertThrows(() => wrmgBase32Binary("234BCDEAA"));
1083 assertThrows(() => wrmgBase32Binary("2-34-B--CD-EA-A-"));
1087 describe("wrmgBase32String", () => {
1088 it("[[Call]] returns the correct string", () => {
1089 for (const [source
, { wrmg
}] of data
) {
1090 assertStrictEquals(wrmgBase32String(source
), wrmg
);
This page took 0.194977 seconds and 3 git commands to generate.