-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunLengthEncTests.js
More file actions
31 lines (26 loc) · 1021 Bytes
/
Copy pathrunLengthEncTests.js
File metadata and controls
31 lines (26 loc) · 1021 Bytes
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
import 'babel-polyfill';
import jsc from 'jsverify';
import { runLengthEnc, runLengthDec } from './runLengthEnc';
describe('run length encoding', () => {
const alphaNumChar = jsc.suchthat(jsc.asciichar, ch => /[A-Za-z0-9]/.test(ch));
const rleItem = jsc.tuple([jsc.integer(1, 20), alphaNumChar]);
const rleList = size => {
if (size <= 1) return [rleItem.generator(size)];
const tail = rleList(size - 1);
const [[, c1],] = tail;
const head = jsc.suchthat(rleItem, ([, c2]) => c2 !== c1).generator(size);
return [head, ...tail];
};
const arb = jsc.bless({
generator: rleList,
shrink: a => jsc.shrink.array(rleItem.shrink)(a)
});
it('property test', () => {
const prop = r => {
const decoded = Array.from(runLengthDec(r));
const encoded = Array.from(runLengthEnc(decoded));
return jsc.utils.isApproxEqual(encoded, r);
};
return jsc.assert(jsc.forall(arb, prop));
});
});