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";
26 filenameSafeBase64Binary
,
27 filenameSafeBase64String
,
28 get16BitSignedIntegralItem
,
29 get16BitUnsignedIntegralItem
,
30 get32BitFloatingPointItem
,
31 get32BitSignedIntegralItem
,
32 get32BitUnsignedIntegralItem
,
33 get64BitFloatingPointItem
,
34 get64BitSignedIntegralItem
,
35 get64BitUnsignedIntegralItem
,
36 get8BitSignedIntegralItem
,
37 get8BitUnsignedIntegralItem
,
48 set32BitFloatingPointItem
,
50 set64BitFloatingPointItem
,
58 // These tests assume a LITTLE‐ENDIAN environment.
59 const data
= new Map([
67 base16
: "006200610073006500360034",
68 base32
: "ABRAAYIAOMAGKABWAA2A====",
69 base64
: "AGIAYQBzAGUANgA0",
70 wrmg
: "01H00R80EC06A01P00T0",
72 [new Uint16Array([62535]), {
78 [new Uint8ClampedArray([75, 73, 66, 73]).buffer
, {
84 [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer
), {
85 base16
: "626173653634",
86 base32
: "MJQXGZJWGQ======",
91 // The following three examples are from RFC 3548.
92 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
93 base16
: "14FB9C03D97E",
94 base32
: "CT5ZYA6ZPY======",
98 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
104 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
111 // The following examples are from the Ruby base32 gem.
112 [new Uint8Array([0x28]), {
118 [new Uint8Array([0xD6]), {
124 [new Uint16Array([0xF8D6]), {
130 [new Uint8Array([0xD6, 0xF8, 0x00]), {
136 [new Uint8Array([0xD6, 0xF8, 0x10]), {
142 [new Uint32Array([0x0C11F8D6]), {
148 [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
149 base16
: "D6F8110C80",
154 [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
155 base16
: "D6F8110C8530",
156 base32
: "234BCDEFGA======",
162 describe("arrayBufferSlice", () => {
163 it("[[Call]] slices an `ArrayBuffer`", () => {
164 const baseBuffer
= Uint8Array
.from([2, 3, 1, 9, 8, 5]).buffer
;
166 new Uint8Array(arrayBufferSlice(baseBuffer
, 1, 4)),
167 Uint8Array
.from([3, 1, 9]),
171 it("[[Call]] slices an `SharedArrayBuffer`", () => {
172 const baseBuffer
= new SharedArrayBuffer(6);
173 new Uint8Array(baseBuffer
).set([2, 3, 1, 9, 8, 5], 0);
175 new Uint8Array(arrayBufferSlice(baseBuffer
, 1, 4)),
176 Uint8Array
.from([3, 1, 9]),
180 it("[[Call]] throws for others", () => {
193 new DataView(new ArrayBuffer()),
196 assertThrows(() => arrayBufferSlice(value
, 0, 0))
201 describe("base16Binary", () => {
202 it("[[Call]] returns the correct data", () => {
204 new Uint8Array(base16Binary("")),
209 new Uint8Array(base16Binary("006200610073006500360034")),
211 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
212 ($) => $.charCodeAt(0),
214 "006200610073006500360034",
217 new Uint16Array(base16Binary("47F4")),
218 new Uint16Array([62535]),
222 new Uint8ClampedArray(base16Binary("4B494249")),
223 new Uint8ClampedArray([75, 73, 66, 73]),
227 new Uint8Array(base16Binary("626173653634")),
228 new Uint8Array([98, 97, 115, 101, 54, 52]),
232 new Uint8Array(base16Binary("14FB9C03D97E")),
233 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
237 new Uint8Array(base16Binary("14FB9C03D9")),
238 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
242 new Uint8Array(base16Binary("14FB9C03")),
243 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
247 new Uint8Array(base16Binary("28")),
248 new Uint8Array([0x28]),
252 new Uint8Array(base16Binary("D6")),
253 new Uint8Array([0xD6]),
257 new Uint16Array(base16Binary("D6F8")),
258 new Uint16Array([0xF8D6]),
262 new Uint8Array(base16Binary("D6F800")),
263 new Uint8Array([0xD6, 0xF8, 0x00]),
267 new Uint8Array(base16Binary("D6F810")),
268 new Uint8Array([0xD6, 0xF8, 0x10]),
272 new Uint32Array(base16Binary("D6F8110C")),
273 new Uint32Array([0x0C11F8D6]),
277 new Uint8Array(base16Binary("D6F8110C80")),
278 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
282 new Uint16Array(base16Binary("D6F8110C8530")),
283 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
288 it("[[Call]] is case‐insensitive", () => {
290 new Uint8Array(base16Binary("d6f8110C80")),
291 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
296 it("[[Call]] throws when provided with an invalid character", () => {
297 assertThrows(() => base16Binary("ABCG"));
300 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 2", () => {
301 assertThrows(() => base16Binary("A"));
302 assertThrows(() => base16Binary("ABC"));
306 describe("base16String", () => {
307 it("[[Call]] returns the correct string", () => {
308 for (const [source
, { base16
}] of data
) {
309 assertStrictEquals(base16String(source
), base16
);
314 describe("base32Binary", () => {
315 it("[[Call]] returns the correct data", () => {
317 new Uint8Array(base32Binary("")),
322 new Uint8Array(base32Binary("ABRAAYIAOMAGKABWAA2A====")),
324 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
325 ($) => $.charCodeAt(0),
327 "ABRAAYIAOMAGKABWAA2A====",
330 new Uint16Array(base32Binary("I72A====")),
331 new Uint16Array([62535]),
335 new Uint8ClampedArray(base32Binary("JNEUESI=")),
336 new Uint8ClampedArray([75, 73, 66, 73]),
340 new Uint8Array(base32Binary("MJQXGZJWGQ======")),
341 new Uint8Array([98, 97, 115, 101, 54, 52]),
345 new Uint8Array(base32Binary("CT5ZYA6ZPY======")),
346 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
350 new Uint8Array(base32Binary("CT5ZYA6Z")),
351 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
355 new Uint8Array(base32Binary("CT5ZYAY=")),
356 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
360 new Uint8Array(base32Binary("FA======")),
361 new Uint8Array([0x28]),
365 new Uint8Array(base32Binary("2Y======")),
366 new Uint8Array([0xD6]),
370 new Uint16Array(base32Binary("234A====")),
371 new Uint16Array([0xF8D6]),
375 new Uint8Array(base32Binary("234AA===")),
376 new Uint8Array([0xD6, 0xF8, 0x00]),
380 new Uint8Array(base32Binary("234BA===")),
381 new Uint8Array([0xD6, 0xF8, 0x10]),
385 new Uint32Array(base32Binary("234BCDA=")),
386 new Uint32Array([0x0C11F8D6]),
390 new Uint8Array(base32Binary("234BCDEA")),
391 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
395 new Uint16Array(base32Binary("234BCDEFGA======")),
396 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
401 it("[[Call]] is case‐insensitive", () => {
403 new Uint8Array(base32Binary("234bcdEA")),
404 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
409 it("[[Call]] throws when provided with an invalid character", () => {
410 assertThrows(() => base32Binary("ABC0"));
411 assertThrows(() => base32Binary("ABC1"));
412 assertThrows(() => base32Binary("ABC8"));
413 assertThrows(() => base32Binary("ABC9"));
416 it("[[Call]] throws when provided with an invalid equals", () => {
417 assertThrows(() => base32Binary("CT3ZYAY=="));
420 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
421 assertThrows(() => base32Binary("234BCDEAA"));
422 assertThrows(() => base32Binary("234BCDEAA======="));
423 assertThrows(() => base32Binary("UHI"));
424 assertThrows(() => base32Binary("UHIUJD"));
428 describe("base32String", () => {
429 it("[[Call]] returns the correct string", () => {
430 for (const [source
, { base32
}] of data
) {
431 assertStrictEquals(base32String(source
), base32
);
436 describe("base64Binary", () => {
437 it("[[Call]] returns the correct data", () => {
439 new Uint8Array(base64Binary("")),
444 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
446 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
447 ($) => $.charCodeAt(0),
452 new Uint16Array(base64Binary("R/Q=")),
453 new Uint16Array([62535]),
457 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
458 new Uint8ClampedArray([75, 73, 66, 73]),
462 new Uint8Array(base64Binary("YmFzZTY0")),
463 new Uint8Array([98, 97, 115, 101, 54, 52]),
467 new Uint8Array(base64Binary("FPucA9l+")),
468 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
472 new Uint8Array(base64Binary("FPucA9k=")),
473 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
477 new Uint8Array(base64Binary("FPucAw==")),
478 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
482 new Uint8Array(base64Binary("KA==")),
483 new Uint8Array([0x28]),
487 new Uint8Array(base64Binary("1g==")),
488 new Uint8Array([0xD6]),
492 new Uint16Array(base64Binary("1vg")),
493 new Uint16Array([0xF8D6]),
497 new Uint8Array(base64Binary("1vgA")),
498 new Uint8Array([0xD6, 0xF8, 0x00]),
502 new Uint8Array(base64Binary("1vgQ")),
503 new Uint8Array([0xD6, 0xF8, 0x10]),
507 new Uint32Array(base64Binary("1vgRDA==")),
508 new Uint32Array([0x0C11F8D6]),
512 new Uint8Array(base64Binary("1vgRDIA=")),
513 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
517 new Uint16Array(base64Binary("1vgRDIUw")),
518 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
523 it("[[Call]] throws when provided with an invalid character", () => {
524 assertThrows(() => base64Binary("abc_"));
527 it("[[Call]] throws when provided with an invalid equals", () => {
528 assertThrows(() => base64Binary("abc=="));
531 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
532 assertThrows(() => base64Binary("abcde"));
533 assertThrows(() => base64Binary("abcde==="));
537 describe("base64String", () => {
538 it("[[Call]] returns the correct string", () => {
539 for (const [source
, { base64
}] of data
) {
540 assertStrictEquals(base64String(source
), base64
);
545 describe("filenameSafeBase64Binary", () => {
546 it("[[Call]] returns the correct data", () => {
548 new Uint8Array(filenameSafeBase64Binary("")),
553 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
555 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
556 ($) => $.charCodeAt(0),
561 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
562 new Uint16Array([62535]),
566 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
567 new Uint8ClampedArray([75, 73, 66, 73]),
571 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
572 new Uint8Array([98, 97, 115, 101, 54, 52]),
576 new Uint8Array(filenameSafeBase64Binary("KA==")),
577 new Uint8Array([0x28]),
581 new Uint8Array(filenameSafeBase64Binary("1g==")),
582 new Uint8Array([0xD6]),
586 new Uint16Array(filenameSafeBase64Binary("1vg")),
587 new Uint16Array([0xF8D6]),
591 new Uint8Array(filenameSafeBase64Binary("1vgA")),
592 new Uint8Array([0xD6, 0xF8, 0x00]),
596 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
597 new Uint8Array([0xD6, 0xF8, 0x10]),
601 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
602 new Uint32Array([0x0C10F8D6]),
606 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
607 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
611 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
612 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
617 it("[[Call]] throws when provided with an invalid character", () => {
618 assertThrows(() => filenameSafeBase64Binary("abc/"));
621 it("[[Call]] throws when provided with an invalid equals", () => {
622 assertThrows(() => filenameSafeBase64Binary("abc=="));
625 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
626 assertThrows(() => filenameSafeBase64Binary("abcde"));
627 assertThrows(() => filenameSafeBase64Binary("abcde==="));
631 describe("filenameSafeBase64String", () => {
632 it("[[Call]] returns the correct string", () => {
633 for (const [source
, { base64
}] of data
) {
635 filenameSafeBase64String(source
),
636 base64
.replace("+", "-").replace("/", "_"),
642 describe("get8BitSignedIntegralItem", () => {
643 it("[[Call]] gets the item", () => {
644 const buffer
= Int8Array
.from([0, -1, 0]).buffer
;
645 assertStrictEquals(get8BitSignedIntegralItem(buffer
, 1), -1n
);
647 get8BitSignedIntegralItem(new DataView(buffer
), 1),
651 get8BitSignedIntegralItem(new Uint8Array(buffer
), 1),
657 describe("get8BitUnsignedIntegralItem", () => {
658 it("[[Call]] gets the item", () => {
659 const buffer
= Int8Array
.from([0, -1, 0]).buffer
;
660 assertStrictEquals(get8BitUnsignedIntegralItem(buffer
, 1), 255n
);
662 get8BitUnsignedIntegralItem(new DataView(buffer
), 1),
666 get8BitUnsignedIntegralItem(new Int8Array(buffer
), 1),
672 describe("get16BitSignedIntegralItem", () => {
673 it("[[Call]] gets the item", () => {
674 const buffer
= Int8Array
.from([0, 0, -1, -1, 0, 0]).buffer
;
675 assertStrictEquals(get16BitSignedIntegralItem(buffer
, 2), -1n
);
677 get16BitSignedIntegralItem(new DataView(buffer
), 2),
681 get16BitSignedIntegralItem(new Uint16Array(buffer
), 2),
687 describe("get16BitUnsignedIntegralItem", () => {
688 it("[[Call]] gets the item", () => {
689 const buffer
= Int8Array
.from([0, 0, -1, -1, 0, 0]).buffer
;
691 get16BitUnsignedIntegralItem(buffer
, 2),
695 get16BitUnsignedIntegralItem(new DataView(buffer
), 2),
699 get16BitUnsignedIntegralItem(new Int16Array(buffer
), 2),
705 describe("get32BitFloatingPointItem", () => {
706 it("[[Call]] gets the item", () => {
707 const buffer
= new ArrayBuffer(12);
708 const view
= new DataView(buffer
);
709 view
.setFloat32(0, NaN
);
710 view
.setFloat32(4, -Infinity
);
711 view
.setFloat32(8, -0);
712 assertStrictEquals(get32BitFloatingPointItem(buffer
, 0), NaN
);
714 get32BitFloatingPointItem(buffer
, 4),
717 assertStrictEquals(get32BitFloatingPointItem(buffer
, 8), -0);
719 get32BitFloatingPointItem(new DataView(buffer
), 4),
723 get32BitFloatingPointItem(new Uint32Array(buffer
), 4),
729 describe("get32BitSignedIntegralItem", () => {
730 it("[[Call]] gets the item", () => {
731 const buffer
= Int8Array
.from(
732 [0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0],
734 assertStrictEquals(get32BitSignedIntegralItem(buffer
, 4), -1n
);
736 get32BitSignedIntegralItem(new DataView(buffer
), 4),
740 get32BitSignedIntegralItem(new Uint32Array(buffer
), 4),
746 describe("get32BitUnsignedIntegralItem", () => {
747 it("[[Call]] gets the item", () => {
748 const buffer
= Int8Array
.from(
749 [0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0],
752 get32BitUnsignedIntegralItem(buffer
, 4),
756 get32BitUnsignedIntegralItem(new DataView(buffer
), 4),
760 get32BitUnsignedIntegralItem(new Int32Array(buffer
), 4),
766 describe("get64BitFloatingPointItem", () => {
767 it("[[Call]] gets the item", () => {
768 const buffer
= new ArrayBuffer(24);
769 const view
= new DataView(buffer
);
770 view
.setFloat64(0, NaN
);
771 view
.setFloat64(8, -Infinity
);
772 view
.setFloat64(16, -0);
773 assertStrictEquals(get64BitFloatingPointItem(buffer
, 0), NaN
);
775 get64BitFloatingPointItem(buffer
, 8),
778 assertStrictEquals(get64BitFloatingPointItem(buffer
, 16), -0);
780 get64BitFloatingPointItem(new DataView(buffer
), 8),
784 get64BitFloatingPointItem(new BigUint64Array(buffer
), 8),
790 describe("get64BitSignedIntegralItem", () => {
791 it("[[Call]] gets the item", () => {
792 const buffer
= Int8Array
.from(
793 [0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1],
795 assertStrictEquals(get64BitSignedIntegralItem(buffer
, 8), -1n
);
797 get64BitSignedIntegralItem(new DataView(buffer
), 8),
801 get64BitSignedIntegralItem(new BigUint64Array(buffer
), 8),
807 describe("get64BitUnsignedIntegralItem", () => {
808 it("[[Call]] gets the item", () => {
809 const buffer
= Int8Array
.from(
810 [0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1],
813 get64BitUnsignedIntegralItem(buffer
, 8),
817 get64BitUnsignedIntegralItem(new DataView(buffer
), 8),
821 get64BitUnsignedIntegralItem(new BigInt64Array(buffer
), 8),
827 describe("isArrayBuffer", () => {
828 it("[[Call]] returns true for array buffers", () => {
830 isArrayBuffer(new ArrayBuffer()),
834 isArrayBuffer(new SharedArrayBuffer()),
839 it("[[Call]] returns false for others", () => {
852 new DataView(new ArrayBuffer()),
855 assertStrictEquals(isArrayBuffer(value
), false)
860 describe("isArrayBufferView", () => {
861 it("[[Call]] returns true for data views", () => {
863 isArrayBufferView(new DataView(new ArrayBuffer())),
868 it("[[Call]] returns true for typed arrays", () => {
870 isArrayBufferView(new Uint8ClampedArray()),
873 assertStrictEquals(isArrayBufferView(new BigInt64Array()), true);
876 it("[[Call]] returns false for others", () => {
890 new SharedArrayBuffer(),
892 assertStrictEquals(isArrayBufferView(value
), false)
897 describe("isBase16", () => {
898 it("[[Call]] returns true for base64 strings", () => {
899 for (const { base16
} of data
.values()) {
900 assertStrictEquals(isBase16(base16
), true);
901 assertStrictEquals(isBase16(base16
.toLowerCase()), true);
905 it("[[Call]] returns false for others", () => {
921 ].forEach((value
) => assertStrictEquals(isBase16(value
), false));
925 describe("isBase32", () => {
926 it("[[Call]] returns true for base32 strings", () => {
927 for (const { base32
} of data
.values()) {
928 assertStrictEquals(isBase32(base32
), true);
929 assertStrictEquals(isBase32(base32
.toLowerCase()), true);
933 it("[[Call]] returns false for others", () => {
948 ].forEach((value
) => assertStrictEquals(isBase32(value
), false));
952 describe("isBase64", () => {
953 it("[[Call]] returns true for base64 strings", () => {
954 for (const { base64
} of data
.values()) {
955 assertStrictEquals(isBase64(base64
), true);
959 it("[[Call]] returns false for others", () => {
974 ].forEach((value
) => assertStrictEquals(isBase64(value
), false));
978 describe("isFilenameSafeBase64", () => {
979 it("[[Call]] returns true for filename‐safe base64 strings", () => {
980 for (const { base64
} of data
.values()) {
982 isFilenameSafeBase64(
983 base64
.replace("+", "-").replace("/", "_"),
990 it("[[Call]] returns false for others", () => {
1005 ].forEach((value
) =>
1006 assertStrictEquals(isFilenameSafeBase64(value
), false)
1011 describe("isSharedArrayBuffer", () => {
1012 it("[[Call]] returns true for shared array buffers", () => {
1014 isSharedArrayBuffer(new SharedArrayBuffer()),
1019 it("[[Call]] returns false for others", () => {
1033 new DataView(new ArrayBuffer()),
1035 ].forEach((value
) =>
1036 assertStrictEquals(isSharedArrayBuffer(value
), false)
1041 describe("isTypedArray", () => {
1042 it("[[Call]] returns true for typed arrays", () => {
1044 isTypedArray(new Uint8Array()),
1048 isTypedArray(new BigInt64Array()),
1053 it("[[Call]] returns false for others", () => {
1067 new SharedArrayBuffer(),
1068 new DataView(new ArrayBuffer()),
1069 ].forEach((value
) =>
1070 assertStrictEquals(isTypedArray(value
), false)
1075 describe("isWRMGBase32", () => {
1076 it("[[Call]] returns true for W·R·M·G base32 strings", () => {
1077 for (const { wrmg
} of data
.values()) {
1078 assertStrictEquals(isWRMGBase32(wrmg
), true);
1079 assertStrictEquals(isWRMGBase32(wrmg
.toLowerCase()), true);
1080 assertStrictEquals(isWRMGBase32(`--${wrmg}--`), true);
1082 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "I")),
1086 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "L")),
1090 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "O")),
1094 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "i")),
1098 isWRMGBase32(wrmg
.replaceAll(/1/gu
, "l")),
1102 isWRMGBase32(wrmg
.replaceAll(/0/gu
, "o")),
1106 isWRMGBase32(wrmg
.replaceAll(/./gu
, ($) => {
1107 const rand
= Math
.random();
1121 it("[[Call]] returns false for others", () => {
1136 ].forEach((value
) =>
1137 assertStrictEquals(isWRMGBase32(value
), false)
1142 describe("set8BitIntegralItem", () => {
1143 it("[[Call]] sets the item", () => {
1144 const buffer
= new ArrayBuffer(3);
1145 set8BitIntegralItem(buffer
, 1, -1n
);
1146 set8BitIntegralItem(buffer
, 2, (1 << 8) - 1);
1148 new Int8Array(buffer
),
1149 Int8Array
.from([0, -1, -1]),
1154 describe("set16BitIntegralItem", () => {
1155 it("[[Call]] sets the item", () => {
1156 const buffer
= new ArrayBuffer(6);
1157 set16BitIntegralItem(buffer
, 2, -1n
);
1158 set16BitIntegralItem(buffer
, 4, (1 << 16) - 1);
1160 new Int8Array(buffer
),
1161 Int8Array
.from([0, 0, -1, -1, -1, -1]),
1166 describe("set32BitFloatingPointItem", () => {
1167 it("[[Call]] sets the item", () => {
1168 const buffer
= new ArrayBuffer(12);
1169 const expected
= new ArrayBuffer(12);
1170 const view
= new DataView(expected
);
1171 view
.setFloat32(0, NaN
);
1172 set32BitFloatingPointItem(buffer
, 0, NaN
);
1173 view
.setFloat32(4, -Infinity
);
1174 set32BitFloatingPointItem(buffer
, 4, -Infinity
);
1175 view
.setFloat32(8, -0);
1176 set32BitFloatingPointItem(buffer
, 8, -0);
1178 new Uint8Array(buffer
),
1179 new Uint8Array(expected
),
1184 describe("set32BitIntegralItem", () => {
1185 it("[[Call]] sets the item", () => {
1186 const buffer
= new ArrayBuffer(12);
1187 set32BitIntegralItem(buffer
, 4, -1n
);
1188 set32BitIntegralItem(buffer
, 8, -1 >>> 0);
1190 new Int8Array(buffer
),
1191 Int8Array
.from([0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1]),
1196 describe("set64BitFloatingPointItem", () => {
1197 it("[[Call]] sets the item", () => {
1198 const buffer
= new ArrayBuffer(24);
1199 const expected
= new ArrayBuffer(24);
1200 const view
= new DataView(expected
);
1201 view
.setFloat64(0, NaN
);
1202 set64BitFloatingPointItem(buffer
, 0, NaN
);
1203 view
.setFloat64(4, -Infinity
);
1204 set64BitFloatingPointItem(buffer
, 4, -Infinity
);
1205 view
.setFloat64(8, -0);
1206 set64BitFloatingPointItem(buffer
, 8, -0);
1208 new Uint8Array(buffer
),
1209 new Uint8Array(expected
),
1214 describe("set64BitIntegralItem", () => {
1215 it("[[Call]] sets the item", () => {
1216 const buffer
= new ArrayBuffer(12);
1217 set64BitIntegralItem(buffer
, 4, -1n
);
1219 new Int8Array(buffer
),
1220 Int8Array
.from([0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1]),
1225 describe("toArrayBuffer", () => {
1226 it("[[Call]] returns the argument for array buffers", () => {
1227 const buffer
= new ArrayBuffer();
1228 assertStrictEquals(toArrayBuffer(buffer
), buffer
);
1231 it("[[Call]] returns the buffer for data views", () => {
1232 const src
= Uint8Array
.from([2, 3, 1]);
1233 const buffer
= toArrayBuffer(new DataView(src
.buffer
));
1234 assert(buffer
instanceof ArrayBuffer
);
1235 assertEquals(new Uint8Array(buffer
), src
);
1238 it("[[Call]] returns the buffer for typed arrays", () => {
1239 const src
= Uint8Array
.from([2, 3, 1]);
1240 const buffer
= toArrayBuffer(src
);
1241 assert(buffer
instanceof ArrayBuffer
);
1242 assertEquals(new Uint8Array(buffer
), src
);
1245 it("[[Call]] throws for others", () => {
1258 ].forEach((value
) =>
1259 assertThrows(() => {
1260 toArrayBuffer(value
);
1266 describe("wrmgBase32Binary", () => {
1267 it("[[Call]] returns the correct data", () => {
1269 new Uint8Array(wrmgBase32Binary("")),
1274 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
1276 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
1277 ($) => $.charCodeAt(0),
1279 "01H00R80EC06A01P00T0",
1282 new Uint16Array(wrmgBase32Binary("8ZT0")),
1283 new Uint16Array([62535]),
1287 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
1288 new Uint8ClampedArray([75, 73, 66, 73]),
1292 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
1293 new Uint8Array([98, 97, 115, 101, 54, 52]),
1297 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
1298 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
1302 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
1303 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
1307 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
1308 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
1312 new Uint8Array(wrmgBase32Binary("50")),
1313 new Uint8Array([0x28]),
1317 new Uint8Array(wrmgBase32Binary("TR")),
1318 new Uint8Array([0xD6]),
1322 new Uint16Array(wrmgBase32Binary("TVW0")),
1323 new Uint16Array([0xF8D6]),
1327 new Uint8Array(wrmgBase32Binary("TVW00")),
1328 new Uint8Array([0xD6, 0xF8, 0x00]),
1332 new Uint8Array(wrmgBase32Binary("TVW10")),
1333 new Uint8Array([0xD6, 0xF8, 0x10]),
1337 new Uint32Array(wrmgBase32Binary("TVW1230")),
1338 new Uint32Array([0x0C11F8D6]),
1342 new Uint8Array(wrmgBase32Binary("TVW12340")),
1343 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1347 new Uint16Array(wrmgBase32Binary("TVW1234560")),
1348 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
1353 it("[[Call]] is case‐insensitive", () => {
1355 new Uint8Array(wrmgBase32Binary("tVw12340")),
1356 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1361 it("[[Call]] casts I, L & O", () => {
1363 new Uint8Array(wrmgBase32Binary("TVWI234O")),
1364 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1368 new Uint8Array(wrmgBase32Binary("TVWi234o")),
1369 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1373 new Uint8Array(wrmgBase32Binary("TVWL234O")),
1374 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1378 new Uint8Array(wrmgBase32Binary("TVWl234o")),
1379 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1384 it("[[Call]] ignores hyphens", () => {
1386 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
1387 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1388 "--TVW---123-40-----",
1392 it("[[Call]] throws when provided with an invalid character", () => {
1393 assertThrows(() => wrmgBase32Binary("ABCu"));
1394 assertThrows(() => wrmgBase32Binary("ABCU"));
1395 assertThrows(() => wrmgBase32Binary("ABC="));
1398 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
1399 assertThrows(() => base32Binary("TVW123400"));
1400 assertThrows(() => base32Binary("TVW123400======="));
1401 assertThrows(() => base32Binary("M78"));
1402 assertThrows(() => base32Binary("M78M93"));
1405 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
1406 assertThrows(() => wrmgBase32Binary("234BCDEAA"));
1407 assertThrows(() => wrmgBase32Binary("2-34-B--CD-EA-A-"));
1411 describe("wrmgBase32String", () => {
1412 it("[[Call]] returns the correct string", () => {
1413 for (const [source
, { wrmg
}] of data
) {
1414 assertStrictEquals(wrmgBase32String(source
), wrmg
);