-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
135 lines (108 loc) · 4.69 KB
/
test.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
var MetroHash64 = require('./index').MetroHash64;
var MetroHash128 = require('./index').MetroHash128;
var metrohash64 = require('./index').metrohash64;
var metrohash128 = require('./index').metrohash128;
var expect = require('chai').expect;
const TESTVECTOR_S = '012345678901234567890123456789012345678901234567890123456789012';
const TESTVECTOR_B = Buffer.from(TESTVECTOR_S);
const CLS_TESTS = [
{ cls : MetroHash64, expected : '6b753dae06704bad' },
{ cls : MetroHash64, seed : 0, expected : '6b753dae06704bad' },
{ cls : MetroHash64, seed : 1, expected : '3b0d481cf4b9b8df' },
{ cls : MetroHash128, expected : 'c77ce2bfa4ed9f9b0548b2ac5074a297' },
{ cls : MetroHash128, seed : 0, expected : 'c77ce2bfa4ed9f9b0548b2ac5074a297' },
{ cls : MetroHash128, seed : 1, expected : '45a3cdb838199d7fbdd68d867a14ecef' },
];
const FUN_TESTS = [
{ fn : metrohash64, expected : '6b753dae06704bad' },
{ fn : metrohash64, seed : 0, expected : '6b753dae06704bad' },
{ fn : metrohash64, seed : 1, expected : '3b0d481cf4b9b8df' },
{ fn : metrohash128, expected : 'c77ce2bfa4ed9f9b0548b2ac5074a297' },
{ fn : metrohash128, seed : 0, expected : 'c77ce2bfa4ed9f9b0548b2ac5074a297' },
{ fn : metrohash128, seed : 1, expected : '45a3cdb838199d7fbdd68d867a14ecef' },
];
describe('Instantiation and method calling', function() {
it('new MetroHash64() should work', function() {
expect(function() { new MetroHash64(); }).to.not.throw();
});
it('MetroHash64() should work', function() {
expect(function() { MetroHash64(); }).to.not.throw();
});
it('new MetroHash128() should work', function() {
expect(function() { new MetroHash128(); }).to.not.throw();
});
it('MetroHash128() should work', function() {
expect(function() { MetroHash128(); }).to.not.throw();
});
it('MetroHash64() should allow chaining', function() {
expect(function() {
new MetroHash64().update(TESTVECTOR_B).digest();
}).to.not.throw();
});
it('MetroHash128() should allow chaining', function() {
expect(function() {
new MetroHash128().update(TESTVECTOR_B).digest();
}).to.not.throw();
});
});
describe('Test vectors', function() {
CLS_TESTS.forEach(function(test) {
var suffix = test.seed !== undefined ? ', seed = ' + test.seed + ')' : ')';
it('new ' + test.cls.name + '(String' + suffix, function() {
var instance = new test.cls(test.seed);
instance.update(TESTVECTOR_S);
expect(instance.digest()).to.equal(test.expected);
});
it(test.cls.name + '(String' + suffix, function() {
var instance = test.cls(test.seed);
instance.update(TESTVECTOR_S);
expect(instance.digest()).to.equal(test.expected);
});
it('new ' + test.cls.name + '(Buffer' + suffix, function() {
var instance = new test.cls(test.seed);
instance.update(TESTVECTOR_B);
expect(instance.digest()).to.equal(test.expected);
});
it(test.cls.name + '(Buffer' + suffix, function() {
var instance = test.cls(test.seed);
instance.update(TESTVECTOR_B);
expect(instance.digest()).to.equal(test.expected);
});
it(test.cls.name + '(String' + suffix + ' incremental', function() {
var instance = test.cls(test.seed);
TESTVECTOR_S.split('').forEach(function(c) {
instance.update(c);
});
expect(instance.digest()).to.equal(test.expected);
});
it(test.cls.name + '(Buffer' + suffix + ' incremental', function() {
var instance = test.cls(test.seed);
for (var i = 0; i < TESTVECTOR_B.length; i++) {
instance.update(TESTVECTOR_B.slice(i, i + 1));
}
expect(instance.digest()).to.equal(test.expected);
});
});
FUN_TESTS.forEach(function(test) {
let hasSeed = test.seed !== undefined;
let suffix = hasSeed ? ', seed = ' + test.seed + ')' : ')';
it('Standalone: ' + test.fn.name + '(String' + suffix, function() {
let result = hasSeed ? test.fn(TESTVECTOR_S, test.seed) : test.fn(TESTVECTOR_S);
expect(result).to.equal(test.expected);
});
it('Standalone: ' + test.fn.name + '(Buffer' + suffix, function() {
let result = hasSeed ? test.fn(TESTVECTOR_B, test.seed) : test.fn(TESTVECTOR_B);
expect(result).to.equal(test.expected);
});
it('Standalone: ' + test.fn.name + ', handle invalid data', function() {
expect(function() { test.fn(123) }).to.fail;
});
it('Standalone: ' + test.fn.name + ', handle invalid seed', function() {
expect(function() { test.fn(TESTVECTOR_S, 'foo') }).to.fail;
});
it('Standalone: ' + test.fn.name + ', handle invalid data and seed', function() {
expect(function() { test.fn(123, 'foo') }).to.fail;
});
});
});