-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
79 lines (68 loc) · 2.47 KB
/
test.js
File metadata and controls
79 lines (68 loc) · 2.47 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
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
import {encode, decode, base, salt} from './main.js';
// Warn if overriding existing method
if(Array.prototype.equals)
console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});
function test() {
// setup code
let cbase = parseInt( document.querySelector('#code-base').value );
if (!isNaN(cbase)) {
base(cbase);
console.log('code base:', cbase);
}
let csalt = document.querySelector('#code-salt').value;
if (csalt.length > 0) {
salt(csalt);
console.log('code salt:', csalt);
}
// run test
const BASE = parseInt( document.querySelector('#test-base').value );
console.log("testing base:", BASE);
let c = 0;
let errors = 0;
// exhaustive test of 5 digits
for (let i = 0; i<BASE; i++) {
for (let j = 0; j<BASE; j++) {
for (let k = 0; k<BASE; k++) {
for (let l = 0; l<BASE; l++) {
for (let m = 0; m<BASE; m++) {
c++;
let input = [i, j, k, l, m];
let enc = encode(input);
let output = decode(enc);
if (c % 100000 === 0) console.log(`(${c}) Checking`, JSON.stringify(input));
let test = input.equals(output);
if (!test) errors++;
console.assert(test, 'input', JSON.stringify(input), 'output', JSON.stringify(output));
}
}
}
}
}
console.log('DONE');
console.log('ERRORS', errors);
}
document.querySelector('button').addEventListener('click', test);