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