Skip to content

Commit 70144b1

Browse files
committed
update
1 parent 418e3e4 commit 70144b1

File tree

6 files changed

+416
-1
lines changed

6 files changed

+416
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from "vitest";
2+
import { assertSize } from "./assert-size.js";
3+
4+
describe("assertSize", () => {
5+
it("should not throw an error for hex strings within the specified size", () => {
6+
expect(() => assertSize("0x1a4", { size: 2 })).not.toThrow();
7+
expect(() => assertSize("0x1234", { size: 2 })).not.toThrow();
8+
});
9+
10+
it("should throw an error for hex strings exceeding the specified size", () => {
11+
expect(() => assertSize("0x123456", { size: 2 })).toThrow(
12+
"Size overflow: 3 > 2",
13+
);
14+
expect(() => assertSize("0xabcdef", { size: 2 })).toThrow(
15+
"Size overflow: 3 > 2",
16+
);
17+
});
18+
19+
it("should not throw an error for Uint8Array within the specified size", () => {
20+
expect(() =>
21+
assertSize(new Uint8Array([1, 2, 3]), { size: 3 }),
22+
).not.toThrow();
23+
expect(() => assertSize(new Uint8Array([]), { size: 0 })).not.toThrow();
24+
});
25+
26+
it("should throw an error for Uint8Array exceeding the specified size", () => {
27+
expect(() => assertSize(new Uint8Array([1, 2, 3, 4]), { size: 3 })).toThrow(
28+
"Size overflow: 4 > 3",
29+
);
30+
});
31+
32+
it("should not throw an error for empty hex strings", () => {
33+
expect(() => assertSize("0x", { size: 0 })).not.toThrow();
34+
});
35+
36+
it("should handle boundary conditions correctly", () => {
37+
expect(() => assertSize("0x12", { size: 1 })).not.toThrow();
38+
expect(() => assertSize("0x12", { size: 0 })).toThrow(
39+
"Size overflow: 1 > 0",
40+
);
41+
});
42+
43+
it("should not throw an error for hex strings exactly at the specified size", () => {
44+
expect(() => assertSize("0x12", { size: 1 })).not.toThrow();
45+
expect(() => assertSize("0x1234", { size: 2 })).not.toThrow();
46+
});
47+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from "vitest";
2+
import { byteSize } from "./byte-size.js";
3+
4+
describe("byteSize", () => {
5+
it('should calculate the byte size for a valid hex string with "0x" prefix', () => {
6+
expect(byteSize("0x1a4")).toBe(2); // "1a4" is 1 byte + 1 nibble = 2 bytes
7+
expect(byteSize("0x123456")).toBe(3); // "123456" is 3 bytes
8+
});
9+
10+
it("should return 0 for an empty hex string", () => {
11+
expect(byteSize("0x")).toBe(0);
12+
});
13+
14+
it("should calculate the byte size for a Uint8Array", () => {
15+
expect(byteSize(new Uint8Array([1, 2, 3]))).toBe(3);
16+
expect(byteSize(new Uint8Array([]))).toBe(0);
17+
});
18+
19+
it('should handle a single byte hex string with "0x" prefix', () => {
20+
expect(byteSize("0x1")).toBe(1); // "1" is half a byte, treated as 1 byte
21+
expect(byteSize("0x12")).toBe(1); // "12" is 1 byte
22+
});
23+
24+
it("should handle longer hex strings", () => {
25+
expect(byteSize("0x1234567890abcdef")).toBe(8); // "1234567890abcdef" is 8 bytes
26+
expect(byteSize("0xabcdef")).toBe(3); // "abcdef" is 3 bytes
27+
});
28+
29+
it("should return 0 for non-hex string inputs in Uint8Array form", () => {
30+
expect(byteSize(new Uint8Array([]))).toBe(0); // Empty Uint8Array
31+
});
32+
33+
it('should handle strings that are not valid hex but start with "0x"', () => {
34+
expect(byteSize("0xg")).toBe(1); // Not a valid hex string
35+
});
36+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from "vitest";
2+
import { charCodeToBase16 } from "./charcode-to-base-16.js";
3+
4+
describe("charCodeToBase16", () => {
5+
it("should return correct values for digits (0-9)", () => {
6+
expect(charCodeToBase16("0".charCodeAt(0))).toBe(0);
7+
expect(charCodeToBase16("5".charCodeAt(0))).toBe(5);
8+
expect(charCodeToBase16("9".charCodeAt(0))).toBe(9);
9+
});
10+
11+
it("should return correct values for uppercase letters (A-F)", () => {
12+
expect(charCodeToBase16("A".charCodeAt(0))).toBe(10);
13+
expect(charCodeToBase16("C".charCodeAt(0))).toBe(12);
14+
expect(charCodeToBase16("F".charCodeAt(0))).toBe(15);
15+
});
16+
17+
it("should return correct values for lowercase letters (a-f)", () => {
18+
expect(charCodeToBase16("a".charCodeAt(0))).toBe(10);
19+
expect(charCodeToBase16("c".charCodeAt(0))).toBe(12);
20+
expect(charCodeToBase16("f".charCodeAt(0))).toBe(15);
21+
});
22+
23+
// Test cases for invalid inputs
24+
it("should return undefined for invalid inputs", () => {
25+
expect(charCodeToBase16("G".charCodeAt(0))).toBeUndefined();
26+
expect(charCodeToBase16("z".charCodeAt(0))).toBeUndefined();
27+
expect(charCodeToBase16(" ".charCodeAt(0))).toBeUndefined();
28+
expect(charCodeToBase16("!".charCodeAt(0))).toBeUndefined();
29+
});
30+
31+
it("should handle edge cases correctly", () => {
32+
expect(charCodeToBase16("0".charCodeAt(0) - 1)).toBeUndefined(); // Just below '0'
33+
expect(charCodeToBase16("9".charCodeAt(0) + 1)).toBeUndefined(); // Just above '9'
34+
expect(charCodeToBase16("A".charCodeAt(0) - 1)).toBeUndefined(); // Just below 'A'
35+
expect(charCodeToBase16("F".charCodeAt(0) + 1)).toBeUndefined(); // Just above 'F'
36+
expect(charCodeToBase16("a".charCodeAt(0) - 1)).toBeUndefined(); // Just below 'a'
37+
expect(charCodeToBase16("f".charCodeAt(0) + 1)).toBeUndefined(); // Just above 'f'
38+
});
39+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isHex } from "./is-hex.js";
3+
4+
describe("isHex", () => {
5+
it('should return true for valid hex strings with "0x" prefix in strict mode', () => {
6+
expect(isHex("0x1a4", { strict: true })).toBe(true);
7+
expect(isHex("0xABCDEF", { strict: true })).toBe(true);
8+
});
9+
10+
it('should return false for hex strings without "0x" prefix in strict mode', () => {
11+
expect(isHex("1a4", { strict: true })).toBe(false);
12+
expect(isHex("abcdef", { strict: true })).toBe(false);
13+
});
14+
15+
it('should return true for valid hex strings with or without "0x" prefix in non-strict mode', () => {
16+
expect(isHex("0x1a4", { strict: false })).toBe(true);
17+
expect(isHex("1a4", { strict: false })).toBe(false);
18+
});
19+
20+
it("should return false for invalid hex strings in strict mode", () => {
21+
expect(isHex("0x1g4", { strict: true })).toBe(false);
22+
expect(isHex("0xZXY", { strict: true })).toBe(false);
23+
});
24+
25+
it("should return false for invalid hex strings in non-strict mode", () => {
26+
expect(isHex("0x1g4", { strict: false })).toBe(true);
27+
expect(isHex("0xZXY", { strict: false })).toBe(true);
28+
});
29+
30+
it("should return false for non-string inputs", () => {
31+
expect(isHex(123, { strict: true })).toBe(false);
32+
expect(isHex(null, { strict: true })).toBe(false);
33+
expect(isHex(undefined, { strict: true })).toBe(false);
34+
expect(isHex({}, { strict: true })).toBe(false);
35+
});
36+
37+
it("should return false for empty strings", () => {
38+
expect(isHex("", { strict: true })).toBe(false);
39+
expect(isHex("", { strict: false })).toBe(false);
40+
});
41+
42+
it("should return true for valid empty hex prefix in non-strict mode", () => {
43+
expect(isHex("0x", { strict: false })).toBe(true);
44+
});
45+
46+
it("should return true for valid empty hex prefix in strict mode", () => {
47+
expect(isHex("0x", { strict: true })).toBe(true);
48+
});
49+
50+
it("should use strict mode by default", () => {
51+
expect(isHex("0x1a4")).toBe(true);
52+
expect(isHex("1a4")).toBe(false);
53+
});
54+
});

0 commit comments

Comments
 (0)