-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
52 lines (43 loc) · 1.54 KB
/
test.js
File metadata and controls
52 lines (43 loc) · 1.54 KB
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
/* Testing predicate: used to validate an array-based test result.
*/
var testThat = function(name, result) {
console.info('Testing: ' + name + ': ' + (result ? 'pass' : 'FAIL'));
};
/* Convert a hex message string into a Uint8Array.
*
* For example, fromHexBuf("00AAB411").
*/
var fromHexBuf = function(s) {
if (s.length % 2 != 0) throw "Bad input.";
var a = new Uint8Array(s.length / 2);
for (var i = 0; i < a.length; ++i) {
a[i] = Number.parseInt(s.substr(i * 2, 2), 16);
}
return a;
};
function assertEqual(test, expected, actual) {
/* Check for array equality. */
if (expected['keys']) {
var arrayOK = assertEqual(test + ' array length',
expected.length, actual.length);
if (!arrayOK) return arrayOK;
var equality = expected.map(function(value, idx) {
return assertEqual(test + '[' + idx + ']', value, actual[idx]);
});
if (!equality.every(function(i) { return i; })) {
console.error('Checking ' + test + ', not all elements matched.');
return false;
}
return true;
}
var hexSuffix = (typeof(actual) == typeof(1)) ?
(' (0x' + actual.toString(16) + ')') : '';
if (expected !== actual) {
console.error('Checking ' + test + ', expected ' + expected + ', got ' +
actual + hexSuffix);
} else {
console.debug('OK: ' + test + ', expected ' + expected + ', got ' +
actual + hexSuffix);
}
return expected === actual;
}