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