-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.test.ts
113 lines (101 loc) · 3.42 KB
/
hashing.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// This file is lacking types because the tests were originally written in JS
import { expect } from "chai";
import * as hashing from "../dst/hashing";
function getTestFuncs(hashFn) {
return {
assertHashes(expectedHashes) {
expectedHashes.forEach(([string, expectedValue]) => {
it(`should return ${expectedValue} for "${string}"`, function() {
expect(hashFn(string)).to.equal(expectedValue);
});
});
},
assertIgnoreCase() {
it('should ignore case', function() {
expect(hashFn('hello world')).to.equal(hashFn('HELLO WORLD'));
});
}
}
}
describe('hashing', function() {
describe('#fnv24()', function() {
const fns = getTestFuncs(value => hashing.fnv24(value));
fns.assertIgnoreCase();
fns.assertHashes([
['', 0x001C9D44],
['this is a string', 12795340],
['Hé110 : wør!D', 2405745],
['안녕하세요 세계', 0x008F64AA]
]);
});
describe('#fnv32()', function() {
context('no high bit', function() {
const fns = getTestFuncs(value => hashing.fnv32(value));
fns.assertIgnoreCase();
fns.assertHashes([
['', 0x811C9DC5],
['this is a string', 1103314317],
['Hé110 : wør!D', 2133112078],
['안녕하세요 세계', 0xBA8F6410]
]);
});
context('high bit', function() {
const fns = getTestFuncs(value => hashing.fnv32(value, true));
fns.assertIgnoreCase();
fns.assertHashes([
['', 2166136261],
['this is a string', 3250797965],
['Hé110 : wør!D', 4280595726],
['안녕하세요 세계', 0xBA8F6410]
]);
});
});
describe('#fnv56()', function() {
const fns = getTestFuncs(value => hashing.fnv56(value));
fns.assertIgnoreCase();
fns.assertHashes([
['', 0x00F29CE4842223EEn],
['this is a string', 45046755487557328n],
['Hé110 : wør!D', 7003673140831451n],
['안녕하세요 세계', 0x00E4D7A3C0FFE45Dn]
]);
});
describe('#fnv64()', function() {
context('no high bit', function() {
const fns = getTestFuncs(value => hashing.fnv64(value));
fns.assertIgnoreCase();
fns.assertHashes([
['', 0xCBF29CE484222325n],
['this is a string', 4440559991801161453n],
['Hé110 : wør!D', 17661114212433175598n],
['안녕하세요 세계', 0x2DE4D7A3C0FFE470n]
]);
});
context('high bit', function() {
const fns = getTestFuncs(value => hashing.fnv64(value, true));
fns.assertIgnoreCase();
fns.assertHashes([
['', 0xCBF29CE484222325n],
['this is a string', 0xBDA009C9131EA6EDn],
['Hé110 : wør!D', 0xF518E1CDB9AEA42En],
['안녕하세요 세계', 0xADE4D7A3C0FFE470n]
]);
});
});
describe('#fnv32to24()', function() {
it('should return 2405745 for 2133112078', function() {
expect(hashing.fnv32to24(2133112078)).to.equal(2405745);
});
it('should return 1875268 for 2166136261', function() {
expect(hashing.fnv32to24(2166136261)).to.equal(1875268);
});
});
describe('#fnv64to56()', function() {
it('should return 68289449647285230 for 14695981039346656037', function() {
expect(hashing.fnv64to56(14695981039346656037n)).to.equal(68289449647285230n);
});
it('should return 45046755487557328 for 4440559991801161453', function() {
expect(hashing.fnv64to56(4440559991801161453n)).to.equal(45046755487557328n);
});
});
});