File tree Expand file tree Collapse file tree 4 files changed +24
-6
lines changed Expand file tree Collapse file tree 4 files changed +24
-6
lines changed Original file line number Diff line number Diff line change @@ -14,16 +14,15 @@ const args = parse(rawArgs);
1414
1515const bit = args . b ?? args . bit ?? args . bits ?? 53 ;
1616const options = Hasher . generate ( bit ) ;
17+ const hasher = new Hasher ( options ) ;
1718
1819console . log ( JSON . stringify ( options , null , " " ) ) ;
1920console . error ( `
2021$ ${ cmd } ${ cmdSuffix ? " " + cmdSuffix : "" } | pbcopy
2122
2223Now 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 } .
2726Please make sure your inputs fall within this range.` ) ;
2827
2928type Args = {
Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 11import { assertEquals , assertNotEquals } from "@std/assert" ;
2+ import { assertSpyCall , assertSpyCalls , spy } from "@std/testing/mock" ;
23import { Hasher } from "./hasher.ts" ;
34
45Deno . 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+ } ) ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments