]>
Lady’s Gitweb - Pisces/blob - binary.test.js
9dbce5753e7ee3970093973517a82fc6e595d6d1
   1 // ♓🌟 Piscēs ∷ binary.test.js 
   2 // ==================================================================== 
   4 // Copyright © 2020–2023 Lady [@ Lady’s Computer]. 
   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/>. 
  17 } from "./dev-deps.js"; 
  23   filenameSafeBase64Binary
, 
  24   filenameSafeBase64String
, 
  30 // These tests assume a LITTLE‐ENDIAN environment. 
  31 const data 
= new Map([ 
  37     base16: "006200610073006500360034", 
  38     base64: "AGIAYQBzAGUANgA0", 
  40   [new Uint16Array([62535]), { 
  44   [new Uint8ClampedArray([75, 73, 66, 73]).buffer
, { 
  48   [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer
), { 
  49     base16: "626173653634", 
  53   // The following three examples are from RFC 3548. 
  54   [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), { 
  55     base16: "14FB9C03D97E", 
  58   [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), { 
  62   [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), { 
  67   // The following examples are from the Ruby base32 gem. 
  68   [new Uint8Array([0x28]), { 
  72   [new Uint8Array([0xD6]), { 
  76   [new Uint16Array([0xF8D6]), { 
  80   [new Uint8Array([0xD6, 0xF8, 0x00]), { 
  84   [new Uint8Array([0xD6, 0xF8, 0x10]), { 
  88   [new Uint32Array([0x0C11F8D6]), { 
  92   [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), { 
  96   [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), { 
  97     base16: "D6F8110C8530", 
 102 describe("base16Binary", () => { 
 103   it("[[Call]] returns the correct data", () => { 
 105       new Uint8Array(base16Binary("")), 
 110       new Uint8Array(base16Binary("006200610073006500360034")), 
 112         "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4", 
 113         ($) => $.charCodeAt(0), 
 115       "006200610073006500360034", 
 118       new Uint16Array(base16Binary("47F4")), 
 119       new Uint16Array([62535]), 
 123       new Uint8ClampedArray(base16Binary("4B494249")), 
 124       new Uint8ClampedArray([75, 73, 66, 73]), 
 128       new Uint8Array(base16Binary("626173653634")), 
 129       new Uint8Array([98, 97, 115, 101, 54, 52]), 
 133       new Uint8Array(base16Binary("14FB9C03D97E")), 
 134       new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), 
 138       new Uint8Array(base16Binary("14FB9C03D9")), 
 139       new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), 
 143       new Uint8Array(base16Binary("14FB9C03")), 
 144       new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), 
 148       new Uint8Array(base16Binary("28")), 
 149       new Uint8Array([0x28]), 
 153       new Uint8Array(base16Binary("D6")), 
 154       new Uint8Array([0xD6]), 
 158       new Uint16Array(base16Binary("D6F8")), 
 159       new Uint16Array([0xF8D6]), 
 163       new Uint8Array(base16Binary("D6F800")), 
 164       new Uint8Array([0xD6, 0xF8, 0x00]), 
 168       new Uint8Array(base16Binary("D6F810")), 
 169       new Uint8Array([0xD6, 0xF8, 0x10]), 
 173       new Uint32Array(base16Binary("D6F8110C")), 
 174       new Uint32Array([0x0C11F8D6]), 
 178       new Uint8Array(base16Binary("D6F8110C80")), 
 179       new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), 
 183       new Uint16Array(base16Binary("D6F8110C8530")), 
 184       new Uint16Array([0xF8D6, 0x0C11, 0x3085]), 
 189   it("[[Call]] is case‐insensitive", () => { 
 191       new Uint8Array(base16Binary("d6f8110C80")), 
 192       new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), 
 197   it("[[Call]] throws when provided with an invalid character", () => { 
 198     assertThrows(() => base16Binary("ABCG")); 
 201   it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 2", () => { 
 202     assertThrows(() => base16Binary("A")); 
 203     assertThrows(() => base16Binary("ABC")); 
 207 describe("base16String", () => { 
 208   it("[[Call]] returns the correct string", () => { 
 209     for (const [source
, { base16 
}] of data
) { 
 210       assertStrictEquals(base16String(source
), base16
); 
 215 describe("base64Binary", () => { 
 216   it("[[Call]] returns the correct data", () => { 
 218       new Uint8Array(base64Binary("")), 
 223       new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")), 
 225         "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4", 
 226         ($) => $.charCodeAt(0), 
 231       new Uint16Array(base64Binary("R/Q=")), 
 232       new Uint16Array([62535]), 
 236       new Uint8ClampedArray(base64Binary("S0lCSQ==")), 
 237       new Uint8ClampedArray([75, 73, 66, 73]), 
 241       new Uint8Array(base64Binary("YmFzZTY0")), 
 242       new Uint8Array([98, 97, 115, 101, 54, 52]), 
 246       new Uint8Array(base64Binary("FPucA9l+")), 
 247       new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), 
 251       new Uint8Array(base64Binary("FPucA9k=")), 
 252       new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), 
 256       new Uint8Array(base64Binary("FPucAw==")), 
 257       new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), 
 261       new Uint8Array(base64Binary("KA==")), 
 262       new Uint8Array([0x28]), 
 266       new Uint8Array(base64Binary("1g==")), 
 267       new Uint8Array([0xD6]), 
 271       new Uint16Array(base64Binary("1vg")), 
 272       new Uint16Array([0xF8D6]), 
 276       new Uint8Array(base64Binary("1vgA")), 
 277       new Uint8Array([0xD6, 0xF8, 0x00]), 
 281       new Uint8Array(base64Binary("1vgQ")), 
 282       new Uint8Array([0xD6, 0xF8, 0x10]), 
 286       new Uint32Array(base64Binary("1vgRDA==")), 
 287       new Uint32Array([0x0C11F8D6]), 
 291       new Uint8Array(base64Binary("1vgRDIA=")), 
 292       new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), 
 296       new Uint16Array(base64Binary("1vgRDIUw")), 
 297       new Uint16Array([0xF8D6, 0x0C11, 0x3085]), 
 302   it("[[Call]] throws when provided with an invalid character", () => { 
 303     assertThrows(() => base64Binary("abc_")); 
 306   it("[[Call]] throws when provided with an invalid equals", () => { 
 307     assertThrows(() => base64Binary("abc==")); 
 310   it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => { 
 311     assertThrows(() => base64Binary("abcde")); 
 312     assertThrows(() => base64Binary("abcde===")); 
 316 describe("base64String", () => { 
 317   it("[[Call]] returns the correct string", () => { 
 318     for (const [source
, { base64 
}] of data
) { 
 319       assertStrictEquals(base64String(source
), base64
); 
 324 describe("filenameSafeBase64Binary", () => { 
 325   it("[[Call]] returns the correct data", () => { 
 327       new Uint8Array(filenameSafeBase64Binary("")), 
 332       new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")), 
 334         "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4", 
 335         ($) => $.charCodeAt(0), 
 340       new Uint16Array(filenameSafeBase64Binary("R_Q=")), 
 341       new Uint16Array([62535]), 
 345       new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")), 
 346       new Uint8ClampedArray([75, 73, 66, 73]), 
 350       new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")), 
 351       new Uint8Array([98, 97, 115, 101, 54, 52]), 
 355       new Uint8Array(filenameSafeBase64Binary("KA==")), 
 356       new Uint8Array([0x28]), 
 360       new Uint8Array(filenameSafeBase64Binary("1g==")), 
 361       new Uint8Array([0xD6]), 
 365       new Uint16Array(filenameSafeBase64Binary("1vg")), 
 366       new Uint16Array([0xF8D6]), 
 370       new Uint8Array(filenameSafeBase64Binary("1vgA")), 
 371       new Uint8Array([0xD6, 0xF8, 0x00]), 
 375       new Uint8Array(filenameSafeBase64Binary("1vgQ")), 
 376       new Uint8Array([0xD6, 0xF8, 0x10]), 
 380       new Uint32Array(filenameSafeBase64Binary("1vgQDA==")), 
 381       new Uint32Array([0x0C10F8D6]), 
 385       new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")), 
 386       new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]), 
 390       new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")), 
 391       new Uint16Array([0xF8D6, 0x0C10, 0x3085]), 
 396   it("[[Call]] throws when provided with an invalid character", () => { 
 397     assertThrows(() => filenameSafeBase64Binary("abc/")); 
 400   it("[[Call]] throws when provided with an invalid equals", () => { 
 401     assertThrows(() => filenameSafeBase64Binary("abc==")); 
 404   it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => { 
 405     assertThrows(() => filenameSafeBase64Binary("abcde")); 
 406     assertThrows(() => filenameSafeBase64Binary("abcde===")); 
 410 describe("filenameSafeBase64String", () => { 
 411   it("[[Call]] returns the correct string", () => { 
 412     for (const [source
, { base64 
}] of data
) { 
 414         filenameSafeBase64String(source
), 
 415         base64
.replace("+", "-").replace("/", "_"), 
 421 describe("isBase16", () => { 
 422   it("[[Call]] returns true for base64 strings", () => { 
 423     for (const { base16 
} of data
.values()) { 
 424       assert(isBase16(base16
)); 
 425       assert(isBase16(base16
.toLowerCase())); 
 429   it("[[Call]] returns false for others", () => { 
 445     ].forEach((value
) => assert(!isBase16(value
))); 
 449 describe("isBase64", () => { 
 450   it("[[Call]] returns true for base64 strings", () => { 
 451     for (const { base64 
} of data
.values()) { 
 452       assert(isBase64(base64
)); 
 456   it("[[Call]] returns false for others", () => { 
 471     ].forEach((value
) => assert(!isBase64(value
))); 
 475 describe("isFilenameSafeBase64", () => { 
 476   it("[[Call]] returns true for filename‐safe base64 strings", () => { 
 477     for (const { base64 
} of data
.values()) { 
 479         isFilenameSafeBase64( 
 480           base64
.replace("+", "-").replace("/", "_"), 
 486   it("[[Call]] returns false for others", () => { 
 501     ].forEach((value
) => assert(!isFilenameSafeBase64(value
))); 
 
This page took 0.0813 seconds  and 3 git commands  to generate.