forked from adraffy/ens-normalize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoder.js
executable file
·29 lines (26 loc) · 898 Bytes
/
coder.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
import {encode_arithmetic, unsafe_btoa, MAX_LINEAR} from '../src/encoder.js';
import {decode_arithmetic, unsafe_atob} from '../src/decoder.js';
import {compare_arrays} from '../src/utils.js';
function rng(n) {
return (Math.random() * n)|0;
}
for (let i = 0; i < 5000; i++) {
let v0 = Array.from({length: rng(10000)}, () => rng(256));
let v1 = unsafe_atob(unsafe_btoa(v0));
if (compare_arrays(v0, v1)) {
console.log({v0, v1});
throw new Error('base64');
}
}
console.log('PASS base64');
for (let i = 0; i < 1000; i++) {
let linear = 1 + rng(MAX_LINEAR);
let v0 = Array.from({length: 1 + rng(10000)}, () => rng(Math.random() < 0.5 ? linear : 0xFFFFFF));
let v1 = encode_arithmetic(v0, linear);
let v2 = decode_arithmetic(v1);
if (compare_arrays(v0, v2)) {
console.log({v0, v1, v2});
throw new Error('arithmetic');
}
}
console.log('PASS arithmetic');