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