Skip to content

Commit fb617de

Browse files
committed
feat: add overflow warning
1 parent c77a4ce commit fb617de

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

cli.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ const args = parse(rawArgs);
1414

1515
const bit = args.b ?? args.bit ?? args.bits ?? 53;
1616
const options = Hasher.generate(bit);
17+
const hasher = new Hasher(options);
1718

1819
console.log(JSON.stringify(options, null, " "));
1920
console.error(`
2021
$ ${cmd}${cmdSuffix ? " " + cmdSuffix : ""} | pbcopy
2122
2223
Now go ahead and paste it into your code! Good luck. :-)
2324
24-
Note: The supported range of integers is from min: 0 to max: ${
25-
(1n << BigInt(bit)) - 1n
26-
}.
25+
Note: The supported range of integers is from min: 0 to max: ${hasher._max}.
2726
Please make sure your inputs fall within this range.`);
2827

2928
type Args = {

deno.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
},
1212
"imports": {
1313
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",
14-
"@std/assert": "jsr:@std/assert@^0.221.0",
15-
"@std/fmt": "jsr:@std/fmt@^0.221.0"
14+
"@std/assert": "jsr:@std/assert@^0.222.0",
15+
"@std/fmt": "jsr:@std/fmt@^0.222.0",
16+
"@std/testing": "jsr:@std/testing@^0.222.1"
1617
},
1718
"exports": {
1819
".": "./mod.ts",

hasher.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assertEquals, assertNotEquals } from "@std/assert";
2+
import { assertSpyCall, assertSpyCalls, spy } from "@std/testing/mock";
23
import { Hasher } from "./hasher.ts";
34

45
Deno.test("hasher, encode and decode", () => {
@@ -82,6 +83,18 @@ Deno.test("full coverage of 8bit", () => {
8283
for (let i = 0; i < 256; i++) {
8384
assertEquals(hasher.decode(hasher.encode(i)), i);
8485
}
85-
assertEquals(hasher.decode(hasher.encode(256)), 0); // overflow
8686
}
8787
});
88+
89+
Deno.test("overflow", () => {
90+
const hasher = new Hasher(Hasher.generate(8));
91+
92+
const logSpy = spy(console, "warn");
93+
94+
hasher.encode(256);
95+
96+
assertSpyCall(logSpy, 0, {
97+
args: ["input 256 is greater than max 255"],
98+
});
99+
assertSpyCalls(logSpy, 1);
100+
});

hasher.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ export class Hasher {
5151
_inverse: bigint;
5252
_xor: bigint;
5353
_mask: bigint;
54+
_max: bigint;
5455

5556
constructor({ bits, prime, inverse, xor }: HasherOptions) {
5657
this._prime = BigInt(prime);
5758
this._inverse = BigInt(inverse);
5859
this._xor = BigInt(xor);
5960
this._mask = 2n ** BigInt(bits) - 1n;
61+
this._max = (1n << BigInt(bits)) - 1n;
6062
}
6163

6264
encode(n: number): number;
@@ -69,6 +71,9 @@ export class Hasher {
6971
if (typeof n === "number") {
7072
return Number(this.encode(BigInt(n)));
7173
}
74+
if (n > this._max) {
75+
console.warn(`input ${n} is greater than max ${this._max}`);
76+
}
7277
return n * this._prime & this._mask ^ this._xor;
7378
}
7479

0 commit comments

Comments
 (0)