-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
238 lines (215 loc) · 7.03 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
var cityhash = require('./');
var passed = true;
function equal(expected, actual) {
var type_expected = typeof expected;
var type_actual = typeof actual;
if(type_expected != type_actual) {
return false;
} else if(type_expected === 'object') {
for(var key in expected) {
var expected_value = expected[key];
var actual_value = actual[key];
if(!equal(expected_value, actual_value)) {
return false;
}
}
} else if(expected != actual) {
return false;
}
return true;
}
function assertEqual(expected, actual, message) {
var failed = !equal(expected, actual);
if(failed) {
if(message) {
message += '. ';
} else {
message = '';
}
passed = false;
console.log('[Error]' + message + 'Expected ' + JSON.stringify(expected) + ' , but actual is ' + JSON.stringify(actual));
} else {
console.log('[PASS] ' + message);
}
}
function end() {
if(passed) {
console.log('All tests are passed.');
}
}
assertEqual('6596376470467341850', cityhash.stringify({low:1727229466, high:1535838579}), 'Stringify for uint64 object');
assertEqual({low:1727229466, high:1535838579, uint64:true}, cityhash.objectify('6596376470467341850'), 'Objectify for uint64 hash string');
assertEqual('9138004313465017137,12242971252332641544', cityhash.stringify({
"low": {
"low": 68277041,
"high": 2127607426
},
"high": {
"low": 3936042248,
"high": 2850538876
}}), 'Stringify for uint128 object');
assertEqual({
"low": {
"low": 68277041,
"high": 2127607426
},
"high": {
"low": 3936042248,
"high": 2850538876
}}, cityhash.objectify('9138004313465017137,12242971252332641544'), 'Stringify for uint128 object');
assertEqual({
low: 1727229466,
high: 1535838579,
uint64: true,
value:'6596376470467341850'
}, cityhash.hash64('Hello'), 'Hash64 for "Hello"');
assertEqual({
low: 4079208671,
high: 600288677,
uint64: true,
value: '2578220239953316063'
}, cityhash.hash64('hello'), 'Hash64 for "hello"');
assertEqual({
low: 2569634289,
high: 3664379964,
uint64: true,
value: '15738392108067291633'
}, cityhash.hash64('Hello', 87392039), 'Hash64 for "hello" with seed 87392039');
var b = new Buffer(4)
b.writeUInt32LE(7771789, 0)
assertEqual({
low: 3883967176,
high: 3780832496,
uint64: true,
value:'16238551925858017992'
}, cityhash.hash64(b, "2310915232984335893"), 'Hash64 for Buffer');
assertEqual({
low: 2569634289,
high: 3664379964,
uint64: true,
value: '15738392108067291633'
}, cityhash.hash64('Hello', cityhash.objectify(87392039)), 'Hash64 for "hello" with objectify seed 87392039');
assertEqual({
low: 2389520903,
high: 3787608545,
value: '16267654833214665223',
uint64: true,
}, cityhash.hash64('Hello', 87392039, 1230234), 'Hash64 for "hello" with seed 87392039 and 1230234');
assertEqual({
"low": {
"low": 68277041,
"high": 2127607426,
"value": "9138004313465017137",
"uint64": true
},
"high": {
"low": 3936042248,
"high": 2850538876,
"value": "12242971252332641544",
"uint64": true
},
"value": "9138004313465017137,12242971252332641544",
"uint128": true
}, cityhash.hash128('Hello'), 'Hash128 for "Hello"');
assertEqual({
"low": {
"low": 3440602095,
"high": 3148776037,
"value": "13523890104784088047",
"uint64": true
},
"high": {
"low": 2750723564,
"high": 4052229467,
"value": "17404193039403234796",
"uint64": true
},
"value": "13523890104784088047,17404193039403234796",
"uint128": true
}, cityhash.hash128('hello'), 'Hash128 for "hello"');
assertEqual({
"low": {
"low": 3184066266,
"high": 3674042232,
"value": "15779891233746910938",
"uint64": true
},
"high": {
"low": 4196783977,
"high": 3519958761,
"value": "15118107765960464233",
"uint64": true
},
"value": "15779891233746910938,15118107765960464233",
"uint128": true
}, cityhash.hash128('Hello', '12343,30293'), 'Hash128 for "Hello" with seed 12343,30293');
assertEqual({
"low": {
"low": 3184066266,
"high": 3674042232,
"value": "15779891233746910938",
"uint64": true
},
"high": {
"low": 4196783977,
"high": 3519958761,
"value": "15118107765960464233",
"uint64": true
},
"value": "15779891233746910938,15118107765960464233",
"uint128": true
}, cityhash.hash128('Hello', cityhash.objectify('12343,30293')), 'Hash128 for "Hello" with objectify seed 12343,30293');
if (cityhash.isCrcSupported()) {
assertEqual({
"low": {
"low": 68277041,
"high": 2127607426,
"value": "9138004313465017137",
"uint64": true
},
"high": {
"low": 3936042248,
"high": 2850538876,
"value": "12242971252332641544",
"uint64": true
},
"value": "9138004313465017137,12242971252332641544",
"uint128": true
}, cityhash.crc128('Hello'), 'Crc128 for "Hello"');
assertEqual({
"low": {
"low": 3184066266,
"high": 3674042232,
"value": "15779891233746910938",
"uint64": true
},
"high": {
"low": 4196783977,
"high": 3519958761,
"value": "15118107765960464233",
"uint64": true
},
"value": "15779891233746910938,15118107765960464233",
"uint128": true
}, cityhash.crc128('Hello', '12343,30293'), 'Crc128 for "Hello" with seed 12343,30293');
assertEqual({
"low": {
"low": 3184066266,
"high": 3674042232,
"value": "15779891233746910938",
"uint64": true
},
"high": {
"low": 4196783977,
"high": 3519958761,
"value": "15118107765960464233",
"uint64": true
},
"value": "15779891233746910938,15118107765960464233",
"uint128": true
}, cityhash.crc128('Hello', cityhash.objectify('12343,30293')), 'Crc128 for "Hello" with objectify seed 12343,30293');
// since my computer does not support SSE4.2, can not calculate the CRC by CityHashCrc256 algorithm.
// assertEqual({
// }, cityhash.crc256('Hello'), 'Crc256 for "Hello"');
}
end();