diff options
author | João Augusto Costa Branco Marado Torres <torres.dev@disroot.org> | 2025-06-24 12:08:41 -0300 |
---|---|---|
committer | João Augusto Costa Branco Marado Torres <torres.dev@disroot.org> | 2025-06-24 12:50:43 -0300 |
commit | f9a77c5c27aede4e5978eb55d9b7af781b680a1d (patch) | |
tree | d545e325ba1ae756fc2eac66fac1001b6753c40d /src/utils/bases.test.ts |
feat!: initial commit
Signed-off-by: João Augusto Costa Branco Marado Torres <torres.dev@disroot.org>
Diffstat (limited to 'src/utils/bases.test.ts')
-rw-r--r-- | src/utils/bases.test.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/utils/bases.test.ts b/src/utils/bases.test.ts new file mode 100644 index 0000000..9341b18 --- /dev/null +++ b/src/utils/bases.test.ts @@ -0,0 +1,32 @@ +import { assertEquals, assertThrows } from "@std/assert"; +import { describe, it } from "@std/testing/bdd"; +import { bufferToBase } from "./bases.ts"; + +describe("bufferToBase", () => { + it("returns an empty string for an empty Uint8Array", () => { + assertEquals(bufferToBase(new Uint8Array([]), 16), ""); + }); + + it("converts bytes to hexadecimal (base 16)", () => { + const input = new Uint8Array([0, 1, 15, 16, 255]); + const expected = "00010f10ff"; + assertEquals(bufferToBase(input, 16), expected); + }); + + it("converts bytes to binary (base 2)", () => { + const input = new Uint8Array([255, 0, 1]); + const expected = "111111110000000000000001"; + assertEquals(bufferToBase(input, 2), expected); + }); + + it("converts bytes to octal (base 8)", () => { + const input = new Uint8Array([8, 64, 255]); + const expected = "010100377"; + assertEquals(bufferToBase(input, 8), expected); + }); + + it("throws on invalid base", () => { + assertThrows(() => bufferToBase(new Uint8Array([1, 2]), 1), RangeError); + assertThrows(() => bufferToBase(new Uint8Array([1, 2]), 37), RangeError); + }); +}); |