1
+ import PolynomialHash from '../PolynomialHash' ;
2
+
3
+ describe ( 'PolynomialHash' , ( ) => {
4
+ it ( 'should calculate new hash based on previous one' , ( ) => {
5
+ const bases = [ 3 , 79 , 101 , 3251 , 13229 , 122743 , 3583213 ] ;
6
+ const mods = [ 79 , 101 ] ;
7
+ const frameSizes = [ 5 , 20 ] ;
8
+
9
+ // @TODO : Provide Unicode support.
10
+ const text = 'Lorem Ipsum is simply dummy text of the printing and '
11
+ + 'typesetting industry. Lorem Ipsum has been the industry\'s standard '
12
+ + 'galley of type and \u{ffff} scrambled it to make a type specimen book. It '
13
+ + 'electronic 耀 typesetting, remaining essentially unchanged. It was '
14
+ // + 'popularised in the \u{20005} \u{20000}1960s with the release of Letraset sheets '
15
+ + 'publishing software like Aldus PageMaker 耀 including versions of Lorem.' ;
16
+
17
+ // Check hashing for different prime base.
18
+ bases . forEach ( ( base ) => {
19
+ mods . forEach ( ( modulus ) => {
20
+ const polynomialHash = new PolynomialHash ( { base, modulus } ) ;
21
+
22
+ // Check hashing for different word lengths.
23
+ frameSizes . forEach ( ( frameSize ) => {
24
+ let previousWord = text . substr ( 0 , frameSize ) ;
25
+ let previousHash = polynomialHash . hash ( previousWord ) ;
26
+
27
+ // Shift frame through the whole text.
28
+ for ( let frameShift = 1 ; frameShift < ( text . length - frameSize ) ; frameShift += 1 ) {
29
+ const currentWord = text . substr ( frameShift , frameSize ) ;
30
+ const currentHash = polynomialHash . hash ( currentWord ) ;
31
+ const currentRollingHash = polynomialHash . roll ( previousHash , previousWord , currentWord ) ;
32
+
33
+ // Check that rolling hash is the same as directly calculated hash.
34
+ expect ( currentRollingHash ) . toBe ( currentHash ) ;
35
+
36
+ previousWord = currentWord ;
37
+ previousHash = currentHash ;
38
+ }
39
+ } ) ;
40
+ } ) ;
41
+ } ) ;
42
+ } ) ;
43
+
44
+ it ( 'should generate numeric hashed less than 100' , ( ) => {
45
+ const polynomialHash = new PolynomialHash ( { modulus : 100 } ) ;
46
+
47
+ expect ( polynomialHash . hash ( 'Some long text that is used as a key' ) ) . toBe ( 41 ) ;
48
+ expect ( polynomialHash . hash ( 'Test' ) ) . toBe ( 92 ) ;
49
+ expect ( polynomialHash . hash ( 'a' ) ) . toBe ( 97 ) ;
50
+ expect ( polynomialHash . hash ( 'b' ) ) . toBe ( 98 ) ;
51
+ expect ( polynomialHash . hash ( 'c' ) ) . toBe ( 99 ) ;
52
+ expect ( polynomialHash . hash ( 'd' ) ) . toBe ( 0 ) ;
53
+ expect ( polynomialHash . hash ( 'e' ) ) . toBe ( 1 ) ;
54
+ expect ( polynomialHash . hash ( 'ab' ) ) . toBe ( 87 ) ;
55
+
56
+ // @TODO : Provide Unicode support.
57
+ expect ( polynomialHash . hash ( '\u{20000}' ) ) . toBe ( 92 ) ;
58
+ } ) ;
59
+ } ) ;
0 commit comments