-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreed_solomon_tests.js
106 lines (95 loc) · 2.54 KB
/
reed_solomon_tests.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
const randomInt = (min, max) => {
return min + Math.floor(Math.random() * (max-min));
}
// Some simple test cases to verify that everything is not horribly broken.
const runTests = () => {
const goodCases = [
{
input: "",
corruption: {},
},
{
input: " ",
corruption: {},
},
{
input: "hello world 1",
corruption: {},
},
{
input: "hello world 2",
corruption: {5: 0x00, 14: 0x33}, // Less than t/2 errors.
}
];
const badCases = [
{
input: "",
corruption: {0: 0x01, 3: 0x03, 4: 0x04},
},
{
input: "hello world 1",
// Less than t errors, but more than t/2.
corruption: {5: 0x00, 14: 0x33, 16: 0x00},
},
{
input: "hello world 2",
// t errors.
corruption: {0: 0x01, 5: 0x00, 9:0x12, 14: 0x33, 16: 0x00},
},
{
input: "hello world 3",
// More than t errors.
corruption: {0: 0x01, 1: 0x02, 5: 0x00, 9:0x12, 14: 0x33, 16: 0x00},
}
];
let rs = new ReedSolomon(5);
// Encode then decode string.
const processTestInput = (test) => {
let encoded = rs.encodeString(test.input);
for (const [pos, v] of Object.entries(test.corruption)) {
encoded[pos] = v;
}
let decoded = rs.decodeToString(encoded);
return decoded;
};
const processRandomInput = (numErrors) => {
const len = randomInt(1, 256);
const input = window.crypto.getRandomValues(new Uint8Array(len));
let encoded = rs.encode(input);
for (let i = 0; i < numErrors; i++) {
encoded[randomInt(0, encoded.length)] = randomInt(0, 256);
}
const decoded = rs.decode(encoded);
return [input, encoded, decoded];
};
// Good cases.
for (const test of goodCases) {
const decoded = processTestInput(test);
if (decoded != test.input) {
console.error(test);
throw 'Good test failed: ' + test.input;
}
}
for (let i = 0; i < 100; i++) {
// Run a bunch of test cases with random errors.
const [input, encoded, decoded] = processRandomInput(randomInt(0, 2));
if (decoded.join() != input.join()) {
console.error({input, encoded, decoded});
throw 'Good test failed: ' + input;
}
}
// Bad cases.
for (const test of badCases) {
try {
const decoded = processTestInput(test);
console.error(test);
throw 'Expected an error';
} catch (e) {
if (!(e instanceof ReedSolomonException)) {
console.error(test);
throw 'Bad test failed: ' + test.input;
}
}
}
console.log('All tests pass');
};