]> Lady’s Gitweb - Pisces/blob - binary.test.js
Apply various minor formatting changes
[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 isDataView,
33 isFilenameSafeBase64,
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 });
593
594 it("[[Call]] returns false for others", () => {
595 [
596 undefined,
597 null,
598 true,
599 Symbol(),
600 27,
601 98n,
602 {},
603 [],
604 () => {},
605 new Proxy({}, {}),
606 "string",
607 new SharedArrayBuffer(),
608 new DataView(new ArrayBuffer()),
609 new Uint8Array(),
610 ].forEach((value) =>
611 assertStrictEquals(isArrayBuffer(value), false)
612 );
613 });
614 });
615
616 describe("isArrayBufferView", () => {
617 it("[[Call]] returns true for data views", () => {
618 assertStrictEquals(
619 isArrayBufferView(new DataView(new ArrayBuffer())),
620 true,
621 );
622 });
623
624 it("[[Call]] returns true for typed arrays", () => {
625 assertStrictEquals(
626 isArrayBufferView(new Uint8ClampedArray()),
627 true,
628 );
629 assertStrictEquals(isArrayBufferView(new BigInt64Array()), true);
630 });
631
632 it("[[Call]] returns false for others", () => {
633 [
634 undefined,
635 null,
636 true,
637 Symbol(),
638 27,
639 98n,
640 {},
641 [],
642 () => {},
643 new Proxy({}, {}),
644 "string",
645 new ArrayBuffer(),
646 new SharedArrayBuffer(),
647 ].forEach((value) =>
648 assertStrictEquals(isArrayBufferView(value), false)
649 );
650 });
651 });
652
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);
658 }
659 });
660
661 it("[[Call]] returns false for others", () => {
662 [
663 undefined,
664 null,
665 true,
666 Symbol(),
667 27,
668 98n,
669 {},
670 [],
671 () => {},
672 new Proxy({}, {}),
673 "abc_",
674 "a",
675 "abc",
676 "abcg",
677 ].forEach((value) => assertStrictEquals(isBase16(value), false));
678 });
679 });
680
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);
686 }
687 });
688
689 it("[[Call]] returns false for others", () => {
690 [
691 undefined,
692 null,
693 true,
694 Symbol(),
695 27,
696 98n,
697 {},
698 [],
699 () => {},
700 new Proxy({}, {}),
701 "ABC1",
702 "A=======",
703 "ABCDEFGHI",
704 ].forEach((value) => assertStrictEquals(isBase32(value), false));
705 });
706 });
707
708 describe("isBase64", () => {
709 it("[[Call]] returns true for base64 strings", () => {
710 for (const { base64 } of data.values()) {
711 assertStrictEquals(isBase64(base64), true);
712 }
713 });
714
715 it("[[Call]] returns false for others", () => {
716 [
717 undefined,
718 null,
719 true,
720 Symbol(),
721 27,
722 98n,
723 {},
724 [],
725 () => {},
726 new Proxy({}, {}),
727 "abc_",
728 "a",
729 "abc==",
730 ].forEach((value) => assertStrictEquals(isBase64(value), false));
731 });
732 });
733
734 describe("isDataView", () => {
735 it("[[Call]] returns true for data views", () => {
736 assertStrictEquals(
737 isDataView(new DataView(new ArrayBuffer())),
738 true,
739 );
740 });
741
742 it("[[Call]] returns false for others", () => {
743 [
744 undefined,
745 null,
746 true,
747 Symbol(),
748 27,
749 98n,
750 {},
751 [],
752 () => {},
753 new Proxy({}, {}),
754 "string",
755 new ArrayBuffer(),
756 new SharedArrayBuffer(),
757 new Uint8Array(),
758 ].forEach((value) => assertStrictEquals(isDataView(value), false));
759 });
760 });
761
762 describe("isFilenameSafeBase64", () => {
763 it("[[Call]] returns true for filename‐safe base64 strings", () => {
764 for (const { base64 } of data.values()) {
765 assertStrictEquals(
766 isFilenameSafeBase64(
767 base64.replace("+", "-").replace("/", "_"),
768 ),
769 true,
770 );
771 }
772 });
773
774 it("[[Call]] returns false for others", () => {
775 [
776 undefined,
777 null,
778 true,
779 Symbol(),
780 27,
781 98n,
782 {},
783 [],
784 () => {},
785 new Proxy({}, {}),
786 "abc/",
787 "a",
788 "abc==",
789 ].forEach((value) =>
790 assertStrictEquals(isFilenameSafeBase64(value), false)
791 );
792 });
793 });
794
795 describe("isTypedArray", () => {
796 it("[[Call]] returns true for typed arrays", () => {
797 assertStrictEquals(
798 isTypedArray(new Uint8Array()),
799 true,
800 );
801 assertStrictEquals(
802 isTypedArray(new BigInt64Array()),
803 true,
804 );
805 });
806
807 it("[[Call]] returns false for others", () => {
808 [
809 undefined,
810 null,
811 true,
812 Symbol(),
813 27,
814 98n,
815 {},
816 [],
817 () => {},
818 new Proxy({}, {}),
819 "string",
820 new ArrayBuffer(),
821 new SharedArrayBuffer(),
822 new DataView(new ArrayBuffer()),
823 ].forEach((value) =>
824 assertStrictEquals(isTypedArray(value), false)
825 );
826 });
827 });
828
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);
835 assertStrictEquals(
836 isWRMGBase32(wrmg.replaceAll(/1/gu, "I")),
837 true,
838 );
839 assertStrictEquals(
840 isWRMGBase32(wrmg.replaceAll(/1/gu, "L")),
841 true,
842 );
843 assertStrictEquals(
844 isWRMGBase32(wrmg.replaceAll(/0/gu, "O")),
845 true,
846 );
847 assertStrictEquals(
848 isWRMGBase32(wrmg.replaceAll(/1/gu, "i")),
849 true,
850 );
851 assertStrictEquals(
852 isWRMGBase32(wrmg.replaceAll(/1/gu, "l")),
853 true,
854 );
855 assertStrictEquals(
856 isWRMGBase32(wrmg.replaceAll(/0/gu, "o")),
857 true,
858 );
859 assertStrictEquals(
860 isWRMGBase32(wrmg.replaceAll(/./gu, ($) => {
861 const rand = Math.random();
862 return rand < 0.25
863 ? $
864 : rand < 0.5
865 ? `-${$}`
866 : rand < 0.75
867 ? `${$}-`
868 : `-${$}-`;
869 })),
870 true,
871 );
872 }
873 });
874
875 it("[[Call]] returns false for others", () => {
876 [
877 undefined,
878 null,
879 true,
880 Symbol(),
881 27,
882 98n,
883 {},
884 [],
885 () => {},
886 new Proxy({}, {}),
887 "ABCU",
888 "A",
889 "ABCDEFGH1",
890 ].forEach((value) =>
891 assertStrictEquals(isWRMGBase32(value), false)
892 );
893 });
894 });
895
896 describe("toArrayBuffer", () => {
897 it("[[Call]] returns the argument for array buffers", () => {
898 const buffer = new ArrayBuffer();
899 assertStrictEquals(toArrayBuffer(buffer), buffer);
900 });
901
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);
907 });
908
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);
914 });
915
916 it("[[Call]] throws for others", () => {
917 [
918 undefined,
919 null,
920 true,
921 Symbol(),
922 27,
923 98n,
924 {},
925 [],
926 () => {},
927 new Proxy({}, {}),
928 "string",
929 ].forEach((value) =>
930 assertThrows(() => {
931 toArrayBuffer(value);
932 })
933 );
934 });
935 });
936
937 describe("wrmgBase32Binary", () => {
938 it("[[Call]] returns the correct data", () => {
939 assertEquals(
940 new Uint8Array(wrmgBase32Binary("")),
941 new Uint8Array([]),
942 "<empty>",
943 );
944 assertEquals(
945 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
946 Uint8Array.from(
947 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
948 ($) => $.charCodeAt(0),
949 ),
950 "01H00R80EC06A01P00T0",
951 );
952 assertEquals(
953 new Uint16Array(wrmgBase32Binary("8ZT0")),
954 new Uint16Array([62535]),
955 "8ZT0",
956 );
957 assertEquals(
958 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
959 new Uint8ClampedArray([75, 73, 66, 73]),
960 "9D4M4J8",
961 );
962 assertEquals(
963 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
964 new Uint8Array([98, 97, 115, 101, 54, 52]),
965 "C9GQ6S9P6G",
966 );
967 assertEquals(
968 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
969 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
970 "2KXSR0YSFR",
971 );
972 assertEquals(
973 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
974 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
975 "2KXSR0YS",
976 );
977 assertEquals(
978 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
979 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
980 "2KXSR0R",
981 );
982 assertEquals(
983 new Uint8Array(wrmgBase32Binary("50")),
984 new Uint8Array([0x28]),
985 "50",
986 );
987 assertEquals(
988 new Uint8Array(wrmgBase32Binary("TR")),
989 new Uint8Array([0xD6]),
990 "TR",
991 );
992 assertEquals(
993 new Uint16Array(wrmgBase32Binary("TVW0")),
994 new Uint16Array([0xF8D6]),
995 "TVW0",
996 );
997 assertEquals(
998 new Uint8Array(wrmgBase32Binary("TVW00")),
999 new Uint8Array([0xD6, 0xF8, 0x00]),
1000 "TVW00",
1001 );
1002 assertEquals(
1003 new Uint8Array(wrmgBase32Binary("TVW10")),
1004 new Uint8Array([0xD6, 0xF8, 0x10]),
1005 "TVW10",
1006 );
1007 assertEquals(
1008 new Uint32Array(wrmgBase32Binary("TVW1230")),
1009 new Uint32Array([0x0C11F8D6]),
1010 "TVW1230",
1011 );
1012 assertEquals(
1013 new Uint8Array(wrmgBase32Binary("TVW12340")),
1014 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1015 "TVW12340",
1016 );
1017 assertEquals(
1018 new Uint16Array(wrmgBase32Binary("TVW1234560")),
1019 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
1020 "TVW1234560",
1021 );
1022 });
1023
1024 it("[[Call]] is case‐insensitive", () => {
1025 assertEquals(
1026 new Uint8Array(wrmgBase32Binary("tVw12340")),
1027 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1028 "tVw12340",
1029 );
1030 });
1031
1032 it("[[Call]] casts I, L & O", () => {
1033 assertEquals(
1034 new Uint8Array(wrmgBase32Binary("TVWI234O")),
1035 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1036 "TVWI234O",
1037 );
1038 assertEquals(
1039 new Uint8Array(wrmgBase32Binary("TVWi234o")),
1040 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1041 "TVWi234o",
1042 );
1043 assertEquals(
1044 new Uint8Array(wrmgBase32Binary("TVWL234O")),
1045 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1046 "TVWL234O",
1047 );
1048 assertEquals(
1049 new Uint8Array(wrmgBase32Binary("TVWl234o")),
1050 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1051 "TVWl234o",
1052 );
1053 });
1054
1055 it("[[Call]] ignores hyphens", () => {
1056 assertEquals(
1057 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
1058 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
1059 "--TVW---123-40-----",
1060 );
1061 });
1062
1063 it("[[Call]] throws when provided with an invalid character", () => {
1064 assertThrows(() => wrmgBase32Binary("ABCu"));
1065 assertThrows(() => wrmgBase32Binary("ABCU"));
1066 assertThrows(() => wrmgBase32Binary("ABC="));
1067 });
1068
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"));
1074 });
1075
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-"));
1079 });
1080 });
1081
1082 describe("wrmgBase32String", () => {
1083 it("[[Call]] returns the correct string", () => {
1084 for (const [source, { wrmg }] of data) {
1085 assertStrictEquals(wrmgBase32String(source), wrmg);
1086 }
1087 });
1088 });
This page took 0.294455 seconds and 5 git commands to generate.