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