Skip to content

Commit

Permalink
fix: tools arb passable
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Oct 1, 2022
1 parent 84e156b commit bbfe5a8
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 133 deletions.
3 changes: 2 additions & 1 deletion packages/marshal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"dependencies": {
"@endo/eventual-send": "^0.16.5",
"@endo/nat": "^4.1.20",
"@endo/promise-kit": "^0.2.49"
"@endo/promise-kit": "^0.2.49",
"@fast-check/ava": "^1.0.1"
},
"devDependencies": {
"@endo/init": "^0.5.49",
Expand Down
2 changes: 1 addition & 1 deletion packages/marshal/src/encodePassable.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export const makeEncodePassable = ({
const result = encodePromise(passable, encodePassable);
result.startsWith('?') ||
assert.fail(
X`internal: Promise encoding must start with "p": ${result}`,
X`internal: Promise encoding must start with "?": ${result}`,
);
return result;
}
Expand Down
63 changes: 45 additions & 18 deletions packages/marshal/test/test-encodePassable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,44 @@ import {
} from '../src/encodePassable.js';
import { compareRank, makeComparatorKit } from '../src/rankOrder.js';
import { sample } from './test-rankOrder.js';
import { arbPassable } from '../tools/arb-passable.js';

const { details: X } = assert;

const r2e = new Map();
const e2r = [];

const encodeRemotable = r => {
const encodeThing = (prefix, r) => {
if (r2e.has(r)) {
return r2e.get(r);
}
const result = `r${e2r.length}`;
const result = `${prefix}${e2r.length}`;
r2e.set(r, result);
e2r.push(r);
return result;
};

const decodeRemotable = e => {
assert(e.startsWith('r'), X`unexpected encoding ${e}`);
const decodeThing = (prefix, e) => {
assert(e.startsWith(prefix), 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));
compareRank(encodeThing('r', x), encodeThing('r', y));

const encodeKey = makeEncodePassable({ encodeRemotable });
const encodePassable = makeEncodePassable({
encodeRemotable: r => encodeThing('r', r),
encodePromise: p => encodeThing('?', p),
encodeError: er => encodeThing('!', er),
});

const decodeKey = makeDecodePassable({ decodeRemotable });
const decodePassable = makeDecodePassable({
decodeRemotable: e => decodeThing('r', e),
decodePromise: e => decodeThing('?', e),
decodeError: e => decodeThing('!', e),
});

const { comparator: compareFull } = makeComparatorKit(compareRemotables);

Expand Down Expand Up @@ -80,12 +89,12 @@ const goldenPairs = harden([

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');
t.is(encodePassable(k), e, 'does k encode as expected');
t.is(decodePassable(e), k, 'does the key round trip through the encoding');
}
// Not round trips
t.is(encodeKey(-0), 'f8000000000000000');
t.is(decodeKey('f0000000000000000'), NaN);
t.is(encodePassable(-0), 'f8000000000000000');
t.is(decodePassable('f0000000000000000'), NaN);
});

const orderInvariants = (t, x, y) => {
Expand All @@ -99,6 +108,12 @@ const orderInvariants = (t, x, y) => {
} else {
t.assert(rankComp === 0 || rankComp === fullComp);
}
const ex = encodePassable(x);
const ey = encodePassable(y);
const encComp = compareRank(ex, ey);
if (fullComp !== 0) {
t.is(encComp, fullComp);
}
};

test('order invariants', t => {
Expand All @@ -109,21 +124,33 @@ test('order invariants', t => {
}
});

test('BigInt values round-trip', async t => {
test('Passables round-trip', async t => {
await fc.assert(
fc.property(fc.bigInt(), n => {
const rt = decodeKey(encodeKey(n));
return t.is(rt, n);
fc.property(arbPassable, n => {
const en = encodePassable(n);
const rt = decodePassable(en);
const er = encodePassable(rt);
t.is(en, er);
t.is(compareFull(n, rt), 0);
}),
);
});

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);
const ea = encodePassable(a);
const eb = encodePassable(b);
t.is(a < b, ea < eb);
t.is(a > b, ea > eb);
}),
);
});

test('Passable encoding corresponds to rankOrder', async t => {
await fc.assert(
fc.property(arbPassable, arbPassable, (a, b) => {
orderInvariants(t, a, b);
}),
);
});
Loading

0 comments on commit bbfe5a8

Please sign in to comment.