]>
Lady’s Gitweb - Pisces/blob - binary.test.js
be6c6a3682a04b11fbaaaca1611717c020e79cf7
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";
21 filenameSafeBase64Binary
,
22 filenameSafeBase64String
,
27 // These tests assume a LITTLE‐ENDIAN environment.
28 const data
= new Map([
33 base64
: "AGIAYQBzAGUANgA0",
35 [new Uint16Array([62535]), {
38 [new Uint8ClampedArray([75, 73, 66, 73]).buffer
, {
41 [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer
), {
45 // The following three examples are from RFC 3548.
46 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
49 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
52 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
56 // The following examples are from the Ruby base32 gem.
57 [new Uint8Array([0x28]), {
60 [new Uint8Array([0xD6]), {
63 [new Uint16Array([0xF8D6]), {
66 [new Uint8Array([0xD6, 0xF8, 0x00]), {
69 [new Uint8Array([0xD6, 0xF8, 0x10]), {
72 [new Uint32Array([0x0C11F8D6]), {
75 [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
78 [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
83 describe("base64Binary", () => {
84 it("[[Call]] returns the correct data", () => {
86 new Uint8Array(base64Binary("")),
91 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
93 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
94 ($) => $.charCodeAt(0),
99 new Uint16Array(base64Binary("R/Q=")),
100 new Uint16Array([62535]),
104 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
105 new Uint8ClampedArray([75, 73, 66, 73]),
109 new Uint8Array(base64Binary("YmFzZTY0")),
110 new Uint8Array([98, 97, 115, 101, 54, 52]),
114 new Uint8Array(base64Binary("FPucA9l+")),
115 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
119 new Uint8Array(base64Binary("FPucA9k=")),
120 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
124 new Uint8Array(base64Binary("FPucAw==")),
125 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
129 new Uint8Array(base64Binary("KA==")),
130 new Uint8Array([0x28]),
134 new Uint8Array(base64Binary("1g==")),
135 new Uint8Array([0xD6]),
139 new Uint16Array(base64Binary("1vg")),
140 new Uint16Array([0xF8D6]),
144 new Uint8Array(base64Binary("1vgA")),
145 new Uint8Array([0xD6, 0xF8, 0x00]),
149 new Uint8Array(base64Binary("1vgQ")),
150 new Uint8Array([0xD6, 0xF8, 0x10]),
154 new Uint32Array(base64Binary("1vgRDA==")),
155 new Uint32Array([0x0C11F8D6]),
159 new Uint8Array(base64Binary("1vgRDIA=")),
160 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
164 new Uint16Array(base64Binary("1vgRDIUw")),
165 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
170 it("[[Call]] throws when provided with an invalid character", () => {
171 assertThrows(() => base64Binary("abc_"));
174 it("[[Call]] throws when provided with an invalid equals", () => {
175 assertThrows(() => base64Binary("abc=="));
178 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
179 assertThrows(() => base64Binary("abcde"));
180 assertThrows(() => base64Binary("abcde==="));
184 describe("base64String", () => {
185 it("[[Call]] returns the correct string", () => {
186 for (const [source
, { base64
}] of data
) {
187 assertStrictEquals(base64String(source
), base64
);
192 describe("filenameSafeBase64Binary", () => {
193 it("[[Call]] returns the correct data", () => {
195 new Uint8Array(filenameSafeBase64Binary("")),
200 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
202 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
203 ($) => $.charCodeAt(0),
208 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
209 new Uint16Array([62535]),
213 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
214 new Uint8ClampedArray([75, 73, 66, 73]),
218 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
219 new Uint8Array([98, 97, 115, 101, 54, 52]),
223 new Uint8Array(filenameSafeBase64Binary("KA==")),
224 new Uint8Array([0x28]),
228 new Uint8Array(filenameSafeBase64Binary("1g==")),
229 new Uint8Array([0xD6]),
233 new Uint16Array(filenameSafeBase64Binary("1vg")),
234 new Uint16Array([0xF8D6]),
238 new Uint8Array(filenameSafeBase64Binary("1vgA")),
239 new Uint8Array([0xD6, 0xF8, 0x00]),
243 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
244 new Uint8Array([0xD6, 0xF8, 0x10]),
248 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
249 new Uint32Array([0x0C10F8D6]),
253 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
254 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
258 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
259 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
264 it("[[Call]] throws when provided with an invalid character", () => {
265 assertThrows(() => filenameSafeBase64Binary("abc/"));
268 it("[[Call]] throws when provided with an invalid equals", () => {
269 assertThrows(() => filenameSafeBase64Binary("abc=="));
272 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
273 assertThrows(() => filenameSafeBase64Binary("abcde"));
274 assertThrows(() => filenameSafeBase64Binary("abcde==="));
278 describe("filenameSafeBase64String", () => {
279 it("[[Call]] returns the correct string", () => {
280 for (const [source
, { base64
}] of data
) {
282 filenameSafeBase64String(source
),
283 base64
.replace("+", "-").replace("/", "_"),
289 describe("isBase64", () => {
290 it("[[Call]] returns true for base64 strings", () => {
291 for (const { base64
} of data
.values()) {
292 assert(isBase64(base64
));
296 it("[[Call]] returns false for others", () => {
311 ].forEach((value
) => assert(!isBase64(value
)));
315 describe("isFilenameSafeBase64", () => {
316 it("[[Call]] returns true for filename‐safe base64 strings", () => {
317 for (const { base64
} of data
.values()) {
319 isFilenameSafeBase64(
320 base64
.replace("+", "-").replace("/", "_"),
326 it("[[Call]] returns false for others", () => {
341 ].forEach((value
) => assert(!isFilenameSafeBase64(value
)));
This page took 0.107801 seconds and 3 git commands to generate.