-
Notifications
You must be signed in to change notification settings - Fork 191
/
test-encodePassable.js
148 lines (131 loc) · 3.89 KB
/
test-encodePassable.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// @ts-check
/* eslint-disable no-bitwise */
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import fc from 'fast-check';
import { isKey } from '../src/keys/checkKey.js';
import { compareKeys, keyEQ } from '../src/keys/compareKeys.js';
import {
makeEncodePassable,
makeDecodePassable,
} from '../src/patterns/encodePassable.js';
import { compareRank, makeComparatorKit } from '../src/patterns/rankOrder.js';
import { sample } from './test-rankOrder.js';
const { details: X } = assert;
const r2e = new Map();
const e2r = [];
const encodeRemotable = r => {
if (r2e.has(r)) {
return r2e.get(r);
}
const result = `r${e2r.length}`;
r2e.set(r, result);
e2r.push(r);
return result;
};
const decodeRemotable = e => {
assert(e.startsWith('r'), X`unexpected encoding ${e}`);
const i = Number(BigInt(e.substring(1)));
assert(i >= 0 && i < e2r.length);
return e2r[i];
};
const compareRemotables = (x, y) =>
compareRank(encodeRemotable(x), encodeRemotable(y));
const encodeKey = makeEncodePassable({ encodeRemotable });
const decodeKey = makeDecodePassable({ decodeRemotable });
const { comparator: compareFull } = makeComparatorKit(compareRemotables);
const asNumber = new Float64Array(1);
const asBits = new BigUint64Array(asNumber.buffer);
const getNaN = (hexEncoding = '0008000000000000') => {
let bits = BigInt(`0x${hexEncoding}`);
bits |= 0x7ff0000000000000n;
if (!(bits & 0x0001111111111111n)) {
bits |= 0x0008000000000000n;
}
asBits[0] = bits;
return asNumber[0];
};
const NegativeNaN = getNaN('ffffffffffffffff');
/** @type {[Key, string][]} */
const goldenPairs = harden([
[1, 'fbff0000000000000'],
[-1, 'f400fffffffffffff'],
[NaN, 'ffff8000000000000'],
[NegativeNaN, 'ffff8000000000000'],
[0, 'f8000000000000000'],
[Infinity, 'ffff0000000000000'],
[-Infinity, 'f000fffffffffffff'],
[-1234567890n, 'n#90:8765432110'],
[-123456789n, 'n1:876543211'],
[-1000n, 'n6:9000'],
[-999n, 'n7:001'],
[-1n, 'n9:9'],
[-0n, 'p1:0'],
[37n, 'p2:37'],
[123456789n, 'p9:123456789'],
[1234567890n, 'p~10:1234567890'],
[934857932847598725662n, 'p~21:934857932847598725662'],
]);
test('golden round trips', t => {
for (const [k, e] of goldenPairs) {
t.is(encodeKey(k), e, 'does k encode as expected');
t.is(decodeKey(e), k, 'does the key round trip through the encoding');
}
// Not round trips
t.is(encodeKey(-0), 'f8000000000000000');
t.is(decodeKey('f0000000000000000'), NaN);
});
const orderInvariants = (t, x, y) => {
const rankComp = compareRank(x, y);
const fullComp = compareFull(x, y);
if (rankComp !== 0) {
t.is(rankComp, fullComp);
}
if (fullComp === 0) {
t.is(rankComp, 0);
} else {
t.assert(rankComp === 0 || rankComp === fullComp);
}
if (isKey(x) && isKey(y)) {
const keyComp = compareKeys(x, y);
if (keyComp === 0) {
t.is(fullComp, 0);
} else if (Number.isNaN(keyComp)) {
t.not(fullComp, 0);
} else {
t.is(keyComp, fullComp);
t.is(keyComp, rankComp);
}
const ex = encodeKey(x);
const ey = encodeKey(y);
const encComp = compareRank(ex, ey);
const dx = decodeKey(ex);
const dy = decodeKey(ey);
t.assert(keyEQ(x, dx));
t.assert(keyEQ(y, dy));
t.is(encComp, fullComp);
}
};
test('order invariants', t => {
for (let i = 0; i < sample.length; i += 1) {
for (let j = i; j < sample.length; j += 1) {
orderInvariants(t, sample[i], sample[j]);
}
}
});
test('BigInt values round-trip', async t => {
await fc.assert(
fc.property(fc.bigInt(), n => {
const rt = decodeKey(encodeKey(n));
return t.is(rt, n);
}),
);
});
test('BigInt encoding comparison corresponds with numeric comparison', async t => {
await fc.assert(
fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {
const ea = encodeKey(a);
const eb = encodeKey(b);
return t.is(a < b, ea < eb) && t.is(a > b, ea > eb);
}),
);
});