]> Lady’s Gitweb - Pisces/blob - binary.test.js
00860edbed74e01d98a9997fb773e0d667e84bfb
[Pisces] / binary.test.js
1 // ♓🌟 Piscēs ∷ binary.test.js
2 // ====================================================================
3 //
4 // Copyright © 2020–2023 Lady [@ Lady’s Computer].
5 //
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/>.
9
10 import {
11 assert,
12 assertEquals,
13 assertStrictEquals,
14 assertThrows,
15 describe,
16 it,
17 } from "./dev-deps.js";
18 import {
19 base16Binary,
20 base16String,
21 base32Binary,
22 base32String,
23 base64Binary,
24 base64String,
25 filenameSafeBase64Binary,
26 filenameSafeBase64String,
27 isArrayBuffer,
28 isArrayBufferView,
29 isBase16,
30 isBase32,
31 isBase64,
32 isFilenameSafeBase64,
33 isSharedArrayBuffer,
34 isTypedArray,
35 isWRMGBase32,
36 toArrayBuffer,
37 wrmgBase32Binary,
38 wrmgBase32String,
39 } from "./binary.js";
40
41 // These tests assume a LITTLE‐ENDIAN environment.
42 const data = new Map([
43 ["", {
44 base16: "",
45 base32: "",
46 base64: "",
47 wrmg: "",
48 }],
49 ["base64", {
50 base16: "006200610073006500360034",
51 base32: "ABRAAYIAOMAGKABWAA2A====",
52 base64: "AGIAYQBzAGUANgA0",
53 wrmg: "01H00R80EC06A01P00T0",
54 }],
55 [new Uint16Array([62535]), {
56 base16: "47F4",
57 base32: "I72A====",
58 base64: "R/Q=",
59 wrmg: "8ZT0",
60 }],
61 [new Uint8ClampedArray([75, 73, 66, 73]).buffer, {
62 base16: "4B494249",
63 base32: "JNEUESI=",
64 base64: "S0lCSQ==",
65 wrmg: "9D4M4J8",
66 }],
67 [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer), {
68 base16: "626173653634",
69 base32: "MJQXGZJWGQ======",
70 base64: "YmFzZTY0",
71 wrmg: "C9GQ6S9P6G",
72 }],
73
74 // The following three examples are from RFC 3548.
75 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
76 base16: "14FB9C03D97E",
77 base32: "CT5ZYA6ZPY======",
78 base64: "FPucA9l+",
79 wrmg: "2KXSR0YSFR",
80 }],
81 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
82 base16: "14FB9C03D9",
83 base32: "CT5ZYA6Z",
84 base64: "FPucA9k=",
85 wrmg: "2KXSR0YS",
86 }],
87 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
88 base16: "14FB9C03",
89 base32: "CT5ZYAY=",
90 base64: "FPucAw==",
91 wrmg: "2KXSR0R",
92 }],
93
94 // The following examples are from the Ruby base32 gem.
95 [new Uint8Array([0x28]), {
96 base16: "28",
97 base32: "FA======",
98 base64: "KA==",
99 wrmg: "50",
100 }],
101 [new Uint8Array([0xD6]), {
102 base16: "D6",
103 base32: "2Y======",
104 base64: "1g==",
105 wrmg: "TR",
106 }],
107 [new Uint16Array([0xF8D6]), {
108 base16: "D6F8",
109 base32: "234A====",
110 base64: "1vg=",
111 wrmg: "TVW0",
112 }],
113 [new Uint8Array([0xD6, 0xF8, 0x00]), {
114 base16: "D6F800",
115 base32: "234AA===",
116 base64: "1vgA",
117 wrmg: "TVW00",
118 }],
119 [new Uint8Array([0xD6, 0xF8, 0x10]), {
120 base16: "D6F810",
121 base32: "234BA===",
122 base64: "1vgQ",
123 wrmg: "TVW10",
124 }],
125 [new Uint32Array([0x0C11F8D6]), {
126 base16: "D6F8110C",
127 base32: "234BCDA=",
128 base64: "1vgRDA==",
129 wrmg: "TVW1230",
130 }],
131 [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
132 base16: "D6F8110C80",
133 base32: "234BCDEA",
134 base64: "1vgRDIA=",
135 wrmg: "TVW12340",
136 }],
137 [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
138 base16: "D6F8110C8530",
139 base32: "234BCDEFGA======",
140 base64: "1vgRDIUw",
141 wrmg: "TVW1234560",
142 }],
143 ]);
144
145 describe("base16Binary", () => {
146 it("[[Call]] returns the correct data", () => {
147 assertEquals(
148 new Uint8Array(base16Binary("")),
149 new Uint8Array([]),
150 "<empty>",
151 );
152 assertEquals(
153 new Uint8Array(base16Binary("006200610073006500360034")),
154 Uint8Array.from(
155 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
156 ($) => $.charCodeAt(0),
157 ),
158 "006200610073006500360034",
159 );
160 assertEquals(
161 new Uint16Array(base16Binary("47F4")),
162 new Uint16Array([62535]),
163 "47F4",
164 );
165 assertEquals(
166 new Uint8ClampedArray(base16Binary("4B494249")),
167 new Uint8ClampedArray([75, 73, 66, 73]),
168 "4B494249",
169 );
170 assertEquals(
171 new Uint8Array(base16Binary("626173653634")),
172 new Uint8Array([98, 97, 115, 101, 54, 52]),
173 "626173653634",
174 );
175 assertEquals(
176 new Uint8Array(base16Binary("14FB9C03D97E")),
177 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
178 "14FB9C03D97E",
179 );
180 assertEquals(
181 new Uint8Array(base16Binary("14FB9C03D9")),
182 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
183 "14FB9C03D9",
184 );
185 assertEquals(
186 new Uint8Array(base16Binary("14FB9C03")),
187 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
188 "14FB9C03",
189 );
190 assertEquals(
191 new Uint8Array(base16Binary("28")),
192 new Uint8Array([0x28]),
193 "28",
194 );
195 assertEquals(
196 new Uint8Array(base16Binary("D6")),
197 new Uint8Array([0xD6]),
198 "D6",
199 );
200 assertEquals(
201 new Uint16Array(base16Binary("D6F8")),
202 new Uint16Array([0xF8D6]),
203 "D6F8",
204 );
205 assertEquals(
206 new Uint8Array(base16Binary("D6F800")),
207 new Uint8Array([0xD6, 0xF8, 0x00]),
208 "D6F800",
209 );
210 assertEquals(
211 new Uint8Array(base16Binary("D6F810")),
212 new Uint8Array([0xD6, 0xF8, 0x10]),
213 "D6F810",
214 );
215 assertEquals(
216 new Uint32Array(base16Binary("D6F8110C")),
217 new Uint32Array([0x0C11F8D6]),
218 "D6F8110C",
219 );
220 assertEquals(
221 new Uint8Array(base16Binary("D6F8110C80")),
222 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
223 "D6F8110C80",
224 );
225 assertEquals(
226 new Uint16Array(base16Binary("D6F8110C8530")),
227 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
228 "D6F8110C8530",
229 );
230 });
231
232 it("[[Call]] is case‐insensitive", () => {
233 assertEquals(
234 new Uint8Array(base16Binary("d6f8110C80")),
235 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
236 "d6f8110C80",
237 );
238 });
239
240 it("[[Call]] throws when provided with an invalid character", () => {
241 assertThrows(() => base16Binary("ABCG"));
242 });
243
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"));
247 });
248 });
249
250 describe("base16String", () => {
251 it("[[Call]] returns the correct string", () => {
252 for (const [source, { base16 }] of data) {
253 assertStrictEquals(base16String(source), base16);
254 }
255 });
256 });
257
258 describe("base32Binary", () => {
259 it("[[Call]] returns the correct data", () => {
260 assertEquals(
261 new Uint8Array(base32Binary("")),
262 new Uint8Array([]),
263 "<empty>",
264 );
265 assertEquals(
266 new Uint8Array(base32Binary("ABRAAYIAOMAGKABWAA2A====")),
267 Uint8Array.from(
268 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
269 ($) => $.charCodeAt(0),
270 ),
271 "ABRAAYIAOMAGKABWAA2A====",
272 );
273 assertEquals(
274 new Uint16Array(base32Binary("I72A====")),
275 new Uint16Array([62535]),
276 "I72A====",
277 );
278 assertEquals(
279 new Uint8ClampedArray(base32Binary("JNEUESI=")),
280 new Uint8ClampedArray([75, 73, 66, 73]),
281 "JNEUESI=",
282 );
283 assertEquals(
284 new Uint8Array(base32Binary("MJQXGZJWGQ======")),
285 new Uint8Array([98, 97, 115, 101, 54, 52]),
286 "MJQXGZJWGQ======",
287 );
288 assertEquals(
289 new Uint8Array(base32Binary("CT5ZYA6ZPY======")),
290 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
291 "CT5ZYA6ZPY======",
292 );
293 assertEquals(
294 new Uint8Array(base32Binary("CT5ZYA6Z")),
295 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
296 "CT5ZYA6Z",
297 );
298 assertEquals(
299 new Uint8Array(base32Binary("CT5ZYAY=")),
300 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
301 "CT5ZYAY=",
302 );
303 assertEquals(
304 new Uint8Array(base32Binary("FA======")),
305 new Uint8Array([0x28]),
306 "FA======",
307 );
308 assertEquals(
309 new Uint8Array(base32Binary("2Y======")),
310 new Uint8Array([0xD6]),
311 "2Y======",
312 );
313 assertEquals(
314 new Uint16Array(base32Binary("234A====")),
315 new Uint16Array([0xF8D6]),
316 "234A====",
317 );
318 assertEquals(
319 new Uint8Array(base32Binary("234AA===")),
320 new Uint8Array([0xD6, 0xF8, 0x00]),
321 "234AA===",
322 );
323 assertEquals(
324 new Uint8Array(base32Binary("234BA===")),
325 new Uint8Array([0xD6, 0xF8, 0x10]),
326 "234BA===",
327 );
328 assertEquals(
329 new Uint32Array(base32Binary("234BCDA=")),
330 new Uint32Array([0x0C11F8D6]),
331 "234BCDA=",
332 );
333 assertEquals(
334 new Uint8Array(base32Binary("234BCDEA")),
335 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
336 "234BCDEA",
337 );
338 assertEquals(
339 new Uint16Array(base32Binary("234BCDEFGA======")),
340 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
341 "234BCDEFGA======",
342 );
343 });
344
345 it("[[Call]] is case‐insensitive", () => {
346 assertEquals(
347 new Uint8Array(base32Binary("234bcdEA")),
348 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
349 "234bcdEA",
350 );
351 });
352
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"));
358 });
359
360 it("[[Call]] throws when provided with an invalid equals", () => {
361 assertThrows(() => base32Binary("CT3ZYAY=="));
362 });
363
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"));
369 });
370 });
371
372 describe("base32String", () => {
373 it("[[Call]] returns the correct string", () => {
374 for (const [source, { base32 }] of data) {
375 assertStrictEquals(base32String(source), base32);
376 }
377 });
378 });
379
380 describe("base64Binary", () => {
381 it("[[Call]] returns the correct data", () => {
382 assertEquals(
383 new Uint8Array(base64Binary("")),
384 new Uint8Array([]),
385 "<empty>",
386 );
387 assertEquals(
388 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
389 Uint8Array.from(
390 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
391 ($) => $.charCodeAt(0),
392 ),
393 "AGIAYQBzAGUANgA0",
394 );
395 assertEquals(
396 new Uint16Array(base64Binary("R/Q=")),
397 new Uint16Array([62535]),
398 "R/Q=",
399 );
400 assertEquals(
401 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
402 new Uint8ClampedArray([75, 73, 66, 73]),
403 "S0lCSQ==",
404 );
405 assertEquals(
406 new Uint8Array(base64Binary("YmFzZTY0")),
407 new Uint8Array([98, 97, 115, 101, 54, 52]),
408 "YmFzZTY0",
409 );
410 assertEquals(
411 new Uint8Array(base64Binary("FPucA9l+")),
412 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
413 "FPucA9l+",
414 );
415 assertEquals(
416 new Uint8Array(base64Binary("FPucA9k=")),
417 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
418 "FPucA9k=",
419 );
420 assertEquals(
421 new Uint8Array(base64Binary("FPucAw==")),
422 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
423 "FPucAw==",
424 );
425 assertEquals(
426 new Uint8Array(base64Binary("KA==")),
427 new Uint8Array([0x28]),
428 "KA==",
429 );
430 assertEquals(
431 new Uint8Array(base64Binary("1g==")),
432 new Uint8Array([0xD6]),
433 "1g==",
434 );
435 assertEquals(
436 new Uint16Array(base64Binary("1vg")),
437 new Uint16Array([0xF8D6]),
438 "1vg",
439 );
440 assertEquals(
441 new Uint8Array(base64Binary("1vgA")),
442 new Uint8Array([0xD6, 0xF8, 0x00]),
443 "1vgA",
444 );
445 assertEquals(
446 new Uint8Array(base64Binary("1vgQ")),
447 new Uint8Array([0xD6, 0xF8, 0x10]),
448 "1vgQ",
449 );
450 assertEquals(
451 new Uint32Array(base64Binary("1vgRDA==")),
452 new Uint32Array([0x0C11F8D6]),
453 "1vgRDA==",
454 );
455 assertEquals(
456 new Uint8Array(base64Binary("1vgRDIA=")),
457 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
458 "1vgRDIA=",
459 );
460 assertEquals(
461 new Uint16Array(base64Binary("1vgRDIUw")),
462 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
463 "1vgRDIUw",
464 );
465 });
466
467 it("[[Call]] throws when provided with an invalid character", () => {
468 assertThrows(() => base64Binary("abc_"));
469 });
470
471 it("[[Call]] throws when provided with an invalid equals", () => {
472 assertThrows(() => base64Binary("abc=="));
473 });
474
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==="));
478 });
479 });
480
481 describe("base64String", () => {
482 it("[[Call]] returns the correct string", () => {
483 for (const [source, { base64 }] of data) {
484 assertStrictEquals(base64String(source), base64);
485 }
486 });
487 });
488
489 describe("filenameSafeBase64Binary", () => {
490 it("[[Call]] returns the correct data", () => {
491 assertEquals(
492 new Uint8Array(filenameSafeBase64Binary("")),
493 new Uint8Array([]),
494 "<empty>",
495 );
496 assertEquals(
497 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
498 Uint8Array.from(
499 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
500 ($) => $.charCodeAt(0),
501 ),
502 "AGIAYQBzAGUANgA0",
503 );
504 assertEquals(
505 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
506 new Uint16Array([62535]),
507 "R/Q=",
508 );
509 assertEquals(
510 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
511 new Uint8ClampedArray([75, 73, 66, 73]),
512 "S0lCSQ==",
513 );
514 assertEquals(
515 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
516 new Uint8Array([98, 97, 115, 101, 54, 52]),
517 "YmFzZTY0",
518 );
519 assertEquals(
520 new Uint8Array(filenameSafeBase64Binary("KA==")),
521 new Uint8Array([0x28]),
522 "KA==",
523 );
524 assertEquals(
525 new Uint8Array(filenameSafeBase64Binary("1g==")),
526 new Uint8Array([0xD6]),
527 "1g==",
528 );
529 assertEquals(
530 new Uint16Array(filenameSafeBase64Binary("1vg")),
531 new Uint16Array([0xF8D6]),
532 "1vg",
533 );
534 assertEquals(
535 new Uint8Array(filenameSafeBase64Binary("1vgA")),
536 new Uint8Array([0xD6, 0xF8, 0x00]),
537 "1vgA",
538 );
539 assertEquals(
540 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
541 new Uint8Array([0xD6, 0xF8, 0x10]),
542 "1vgQ",
543 );
544 assertEquals(
545 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
546 new Uint32Array([0x0C10F8D6]),
547 "1vgQDA==",
548 );
549 assertEquals(
550 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
551 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
552 "1vgQDIA=",
553 );
554 assertEquals(
555 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
556 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
557 "1vgQDIUw",
558 );
559 });
560
561 it("[[Call]] throws when provided with an invalid character", () => {
562 assertThrows(() => filenameSafeBase64Binary("abc/"));
563 });
564
565 it("[[Call]] throws when provided with an invalid equals", () => {
566 assertThrows(() => filenameSafeBase64Binary("abc=="));
567 });
568
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==="));
572 });
573 });
574
575 describe("filenameSafeBase64String", () => {
576 it("[[Call]] returns the correct string", () => {
577 for (const [source, { base64 }] of data) {
578 assertStrictEquals(
579 filenameSafeBase64String(source),
580 base64.replace("+", "-").replace("/", "_"),
581 );
582 }
583 });
584 });
585
586 describe("isArrayBuffer", () => {
587 it("[[Call]] returns true for array buffers", () => {
588 assertStrictEquals(
589 isArrayBuffer(new ArrayBuffer()),
590 true,
591 );
592 assertStrictEquals(
593 isArrayBuffer(new SharedArrayBuffer()),
594 true,
595 );
596 });
597
598 it("[[Call]] returns false for others", () => {
599 [
600 undefined,
601 null,
602 true,
603 Symbol(),
604 27,
605 98n,
606 {},
607 [],
608 () => {},
609 new Proxy({}, {}),
610 "string",
611 new DataView(new ArrayBuffer()),
612 new Uint8Array(),
613 ].forEach((value) =>
614 assertStrictEquals(isArrayBuffer(value), false)
615 );
616 });
617 });
618
619 describe("isArrayBufferView", () => {
620 it("[[Call]] returns true for data views", () => {
621 assertStrictEquals(
622 isArrayBufferView(new DataView(new ArrayBuffer())),
623 true,
624 );
625 });
626
627 it("[[Call]] returns true for typed arrays", () => {
628 assertStrictEquals(
629 isArrayBufferView(new Uint8ClampedArray()),
630 true,
631 );
632 assertStrictEquals(isArrayBufferView(new BigInt64Array()), true);
633 });
634
635 it("[[Call]] returns false for others", () => {
636 [
637 undefined,
638 null,
639 true,
640 Symbol(),
641 27,
642 98n,
643 {},
644 [],
645 () => {},
646 new Proxy({}, {}),
647 "string",
648 new ArrayBuffer(),
649 new SharedArrayBuffer(),
650 ].forEach((value) =>
651 assertStrictEquals(isArrayBufferView(value), false)
652 );
653 });
654 });
655
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);
661 }
662 });
663
664 it("[[Call]] returns false for others", () => {
665 [
666 undefined,
667 null,
668 true,
669 Symbol(),
670 27,
671 98n,
672 {},
673 [],
674 () => {},
675 new Proxy({}, {}),
676 "abc_",
677 "a",
678 "abc",
679 "abcg",
680 ].forEach((value) => assertStrictEquals(isBase16(value), false));
681 });
682 });
683
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);
689 }
690 });
691
692 it("[[Call]] returns false for others", () => {
693 [
694 undefined,
695 null,
696 true,
697 Symbol(),
698 27,
699 98n,
700 {},
701 [],
702 () => {},
703 new Proxy({}, {}),
704 "ABC1",
705 "A=======",
706 "ABCDEFGHI",
707 ].forEach((value) => assertStrictEquals(isBase32(value), false));
708 });
709 });
710
711 describe("isBase64", () => {
712 it("[[Call]] returns true for base64 strings", () => {
713 for (const { base64 } of data.values()) {
714 assertStrictEquals(isBase64(base64), true);
715 }
716 });
717
718 it("[[Call]] returns false for others", () => {
719 [
720 undefined,
721 null,
722 true,
723 Symbol(),
724 27,
725 98n,
726 {},
727 [],
728 () => {},
729 new Proxy({}, {}),
730 "abc_",
731 "a",
732 "abc==",
733 ].forEach((value) => assertStrictEquals(isBase64(value), false));
734 });
735 });
736
737 describe("isFilenameSafeBase64", () => {
738 it("[[Call]] returns true for filename‐safe base64 strings", () => {
739 for (const { base64 } of data.values()) {
740 assertStrictEquals(
741 isFilenameSafeBase64(
742 base64.replace("+", "-").replace("/", "_"),
743 ),
744 true,
745 );
746 }
747 });
748
749 it("[[Call]] returns false for others", () => {
750 [
751 undefined,
752 null,
753 true,
754 Symbol(),
755 27,
756 98n,
757 {},
758 [],
759 () => {},
760 new Proxy({}, {}),
761 "abc/",
762 "a",
763 "abc==",
764 ].forEach((value) =>
765 assertStrictEquals(isFilenameSafeBase64(value), false)
766 );
767 });
768 });
769
770 describe("isSharedArrayBuffer", () => {
771 it("[[Call]] returns true for shared array buffers", () => {
772 assertStrictEquals(
773 isSharedArrayBuffer(new SharedArrayBuffer()),
774 true,
775 );
776 });
777
778 it("[[Call]] returns false for others", () => {
779 [
780 undefined,
781 null,
782 true,
783 Symbol(),
784 27,
785 98n,
786 {},
787 [],
788 () => {},
789 new Proxy({}, {}),
790 "string",
791 new ArrayBuffer(),
792 new DataView(new ArrayBuffer()),
793 new Uint8Array(),
794 ].forEach((value) =>
795 assertStrictEquals(isSharedArrayBuffer(value), false)
796 );
797 });
798 });
799
800 describe("isTypedArray", () => {
801 it("[[Call]] returns true for typed arrays", () => {
802 assertStrictEquals(
803 isTypedArray(new Uint8Array()),
804 true,
805 );
806 assertStrictEquals(
807 isTypedArray(new BigInt64Array()),
808 true,
809 );
810 });
811
812 it("[[Call]] returns false for others", () => {
813 [
814 undefined,
815 null,
816 true,
817 Symbol(),
818 27,
819 98n,
820 {},
821 [],
822 () => {},
823 new Proxy({}, {}),
824 "string",
825 new ArrayBuffer(),
826 new SharedArrayBuffer(),
827 new DataView(new ArrayBuffer()),
828 ].forEach((value) =>
829 assertStrictEquals(isTypedArray(value), false)
830 );
831 });
832 });
833
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);
840 assertStrictEquals(
841 isWRMGBase32(wrmg.replaceAll(/1/gu, "I")),
842 true,
843 );
844 assertStrictEquals(
845 isWRMGBase32(wrmg.replaceAll(/1/gu, "L")),
846 true,
847 );
848 assertStrictEquals(
849 isWRMGBase32(wrmg.replaceAll(/0/gu, "O")),
850 true,
851 );
852 assertStrictEquals(
853 isWRMGBase32(wrmg.replaceAll(/1/gu, "i")),
854 true,
855 );
856 assertStrictEquals(
857 isWRMGBase32(wrmg.replaceAll(/1/gu, "l")),
858 true,
859 );
860 assertStrictEquals(
861 isWRMGBase32(wrmg.replaceAll(/0/gu, "o")),
862 true,
863 );
864 assertStrictEquals(
865 isWRMGBase32(wrmg.replaceAll(/./gu, ($) => {
866 const rand = Math.random();
867 return rand < 0.25
868 ? $
869 : rand < 0.5
870 ? `-${$}`
871 : rand < 0.75
872 ? `${$}-`
873 : `-${$}-`;
874 })),
875 true,
876 );
877 }
878 });
879
880 it("[[Call]] returns false for others", () => {
881 [
882 undefined,
883 null,
884 true,
885 Symbol(),
886 27,
887 98n,
888 {},
889 [],
890 () => {},
891 new Proxy({}, {}),
892 "ABCU",
893 "A",
894 "ABCDEFGH1",
895 ].forEach((value) =>
896 assertStrictEquals(isWRMGBase32(value), false)
897 );
898 });
899 });
900
901 describe("toArrayBuffer", () => {
902 it("[[Call]] returns the argument for array buffers", () => {
903 const buffer = new ArrayBuffer();
904 assertStrictEquals(toArrayBuffer(buffer), buffer);
905 });
906
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);
912 });
913
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);
919 });
920
921 it("[[Call]] throws for others", () => {
922 [
923 undefined,
924 null,
925 true,
926 Symbol(),
927 27,
928 98n,
929 {},
930 [],
931 () => {},
932 new Proxy({}, {}),
933 "string",
934 ].forEach((value) =>
935 assertThrows(() => {
936 toArrayBuffer(value);
937 })
938 );
939 });
940 });
941
942 describe("wrmgBase32Binary", () => {
943 it("[[Call]] returns the correct data", () => {
944 assertEquals(
945 new Uint8Array(wrmgBase32Binary("")),
946 new Uint8Array([]),
947 "<empty>",
948 );
949 assertEquals(
950 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
951 Uint8Array.from(
952 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
953 ($) => $.charCodeAt(0),
954 ),
955 "01H00R80EC06A01P00T0",
956 );
957 assertEquals(
958 new Uint16Array(wrmgBase32Binary("8ZT0")),
959 new Uint16Array([62535]),
960 "8ZT0",
961 );
962 assertEquals(
963 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
964 new Uint8ClampedArray([75, 73, 66, 73]),
965 "9D4M4J8",
966 );
967 assertEquals(
968 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
969 new Uint8Array([98, 97, 115, 101, 54, 52]),
970 "C9GQ6S9P6G",
971 );
972 assertEquals(
973 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
974 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
975 "2KXSR0YSFR",
976 );
977 assertEquals(
978 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
979 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
980 "2KXSR0YS",
981 );
982 assertEquals(
983 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
984 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
985 "2KXSR0R",
986 );
987 assertEquals(
988 new Uint8Array(wrmgBase32Binary("50")),
989 new Uint8Array([0x28]),
990 "50",
991 );
992 assertEquals(
993 new Uint8Array(wrmgBase32Binary("TR")),
994 new Uint8Array([0xD6]),
995 "TR",
996 );
997 assertEquals(
998 new Uint16Array(wrmgBase32Binary("TVW0")),
999 new Uint16Array([0xF8D6]),
1000 "TVW0",
1001 );
1002 assertEquals(
1003 new Uint8Array(wrmgBase32Binary("TVW00")),
1004 new Uint8Array([0xD6, 0xF8, 0x00]),
1005 "TVW00",
1006 );
1007 assertEquals(
1008 new Uint8Array(wrmgBase32Binary("TVW10")),
1009 new Uint8Array([0xD6, 0xF8, 0x10]),
1010 "TVW10",
1011 );
1012 assertEquals(
1013 new Uint32Array(wrmgBase32Binary("TVW1230")),
1014 new Uint32Array([0x0C11F8D6]),
1015 "TVW1230",
1016 );
1017 assertEquals(
1018 new Uint8Array(wrmgBase32Binary("TVW12340")),
1019 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1020 "TVW12340",
1021 );
1022 assertEquals(
1023 new Uint16Array(wrmgBase32Binary("TVW1234560")),
1024 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
1025 "TVW1234560",
1026 );
1027 });
1028
1029 it("[[Call]] is case‐insensitive", () => {
1030 assertEquals(
1031 new Uint8Array(wrmgBase32Binary("tVw12340")),
1032 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1033 "tVw12340",
1034 );
1035 });
1036
1037 it("[[Call]] casts I, L & O", () => {
1038 assertEquals(
1039 new Uint8Array(wrmgBase32Binary("TVWI234O")),
1040 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1041 "TVWI234O",
1042 );
1043 assertEquals(
1044 new Uint8Array(wrmgBase32Binary("TVWi234o")),
1045 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1046 "TVWi234o",
1047 );
1048 assertEquals(
1049 new Uint8Array(wrmgBase32Binary("TVWL234O")),
1050 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1051 "TVWL234O",
1052 );
1053 assertEquals(
1054 new Uint8Array(wrmgBase32Binary("TVWl234o")),
1055 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1056 "TVWl234o",
1057 );
1058 });
1059
1060 it("[[Call]] ignores hyphens", () => {
1061 assertEquals(
1062 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
1063 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1064 "--TVW---123-40-----",
1065 );
1066 });
1067
1068 it("[[Call]] throws when provided with an invalid character", () => {
1069 assertThrows(() => wrmgBase32Binary("ABCu"));
1070 assertThrows(() => wrmgBase32Binary("ABCU"));
1071 assertThrows(() => wrmgBase32Binary("ABC="));
1072 });
1073
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"));
1079 });
1080
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-"));
1084 });
1085 });
1086
1087 describe("wrmgBase32String", () => {
1088 it("[[Call]] returns the correct string", () => {
1089 for (const [source, { wrmg }] of data) {
1090 assertStrictEquals(wrmgBase32String(source), wrmg);
1091 }
1092 });
1093 });
This page took 0.194977 seconds and 3 git commands to generate.