@@ -2,102 +2,58 @@ import PolynomialHash from '../PolynomialHash';
22
33describe ( 'PolynomialHash' , ( ) => {
44 it ( 'should calculate new hash based on previous one' , ( ) => {
5- // const primes = [3, 79, 101, 3251, 13229, 122743, 3583213];
6- // const frameSizes = [5, 20];
7-
8- const primes = [ 3 ] ;
9- const frameSizes = [ 20 ] ;
5+ const bases = [ 3 , 79 , 101 , 3251 , 13229 , 122743 , 3583213 ] ;
6+ const mods = [ 79 , 101 ] ;
7+ const frameSizes = [ 5 , 20 ] ;
108
9+ // @TODO : Provide Unicode support.
1110 const text = 'Lorem Ipsum is simply dummy text of the printing and '
1211 + 'typesetting industry. Lorem Ipsum has been the industry\'s standard '
1312 + 'galley of type and \u{ffff} scrambled it to make a type specimen book. It '
1413 + 'electronic 耀 typesetting, remaining essentially unchanged. It was '
15- + 'popularised in the \u{20005} \u{20000}1960s with the release of Letraset sheets '
14+ // + 'popularised in the \u{20005} \u{20000}1960s with the release of Letraset sheets '
1615 + 'publishing software like Aldus PageMaker 耀 including versions of Lorem.' ;
1716
1817 // Check hashing for different prime base.
19- primes . forEach ( ( prime ) => {
20- const polynomialHash = new PolynomialHash ( prime ) ;
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- }
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+ } ) ;
3940 } ) ;
4041 } ) ;
4142 } ) ;
4243
43- // it('should calculate new hash based on previous one', () => {
44- // const polynomialHash = new PolynomialHash();
45- //
46- // const wordLength = 3;
47- // const string = 'Hello World!';
48- //
49- // const word1 = string.substr(0, wordLength);
50- // const word2 = string.substr(1, wordLength);
51- // const word3 = string.substr(2, wordLength);
52- // const word4 = string.substr(3, wordLength);
53- //
54- // const directHash1 = polynomialHash.hash(word1);
55- // const directHash2 = polynomialHash.hash(word2);
56- // const directHash3 = polynomialHash.hash(word3);
57- // const directHash4 = polynomialHash.hash(word4);
58- //
59- // const rollingHash2 = polynomialHash.roll(directHash1, word1, word2);
60- // const rollingHash3 = polynomialHash.roll(directHash2, word2, word3);
61- // const rollingHash4 = polynomialHash.roll(directHash3, word3, word4);
62- //
63- // expect(directHash1).toBe(151661);
64- // expect(directHash2).toBe(151949);
65- // expect(directHash3).toBe(156063);
66- // expect(directHash4).toBe(48023);
67- //
68- // expect(rollingHash2).toBe(directHash2);
69- // expect(rollingHash3).toBe(directHash3);
70- // expect(rollingHash4).toBe(directHash4);
71- // });
72- //
73- // it('should calculate new hash based on previous one with 3 as a primeModulus', () => {
74- // const PRIME = 3;
75- // const polynomialHash = new PolynomialHash(PRIME);
76- //
77- // const wordLength = 3;
78- // const string = 'Hello World!';
79- //
80- // const word1 = string.substr(0, wordLength);
81- // const word2 = string.substr(1, wordLength);
82- // const word3 = string.substr(2, wordLength);
83- // const word4 = string.substr(3, wordLength);
84- //
85- // const directHash1 = polynomialHash.hash(word1);
86- // const directHash2 = polynomialHash.hash(word2);
87- // const directHash3 = polynomialHash.hash(word3);
88- // const directHash4 = polynomialHash.hash(word4);
89- //
90- // const rollingHash2 = polynomialHash.roll(directHash1, word1, word2);
91- // const rollingHash3 = polynomialHash.roll(directHash2, word2, word3);
92- // const rollingHash4 = polynomialHash.roll(directHash3, word3, word4);
93- //
94- // expect(directHash1).toBe(1347);
95- // expect(directHash2).toBe(1397);
96- // expect(directHash3).toBe(1431);
97- // expect(directHash4).toBe(729);
98- //
99- // expect(rollingHash2).toBe(directHash2);
100- // expect(rollingHash3).toBe(directHash3);
101- // expect(rollingHash4).toBe(directHash4);
102- // });
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+ } ) ;
10359} ) ;
0 commit comments