Skip to content

Commit

Permalink
chore(ERTP): AmountMath compare should be same as Key compare'
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jan 10, 2023
1 parent 229708b commit 4e96088
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/ERTP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"@endo/eventual-send": "^0.16.8",
"@endo/far": "^0.2.14",
"@endo/marshal": "^0.8.1",
"@endo/promise-kit": "^0.2.52"
"@endo/promise-kit": "^0.2.52",
"@fast-check/ava": "^1.1.3"
},
"devDependencies": {
"@endo/bundle-source": "^2.4.2",
"@fast-check/ava": "^1.1.3",
"ava": "^5.1.0",
"tsd": "^0.25.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@agoric/store';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { getCopySetKeys, makeCopySet } from '@agoric/store';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { M } from '@agoric/store';

import { Far } from '@endo/marshal';
import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { Far } from '@endo/marshal';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
22 changes: 22 additions & 0 deletions packages/ERTP/test/unitTests/test-amount-key-arith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { fc } from '@fast-check/ava';
import { keyEQ, keyGTE } from '@agoric/store';

import { AmountMath as m } from '../../src/index.js';
import { arbAmount } from '../../tools/arb-amount.js';

test('amount equality iff key equality', async t => {
await fc.assert(
fc.property(fc.record({ x: arbAmount, y: arbAmount }), ({ x, y }) => {
return t.true(m.isEqual(x, y) === keyEQ(x, y));
}),
);
});

test('amount >= iff key >=', async t => {
await fc.assert(
fc.property(fc.record({ x: arbAmount, y: arbAmount }), ({ x, y }) => {
return t.true(m.isGTE(x, y) === keyGTE(x, y));
}),
);
});
24 changes: 2 additions & 22 deletions packages/ERTP/test/unitTests/test-amountProperties.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { makeCopyBag } from '@agoric/store';
import { fc } from '@fast-check/ava';

import { AmountMath as m, AssetKind } from '../../src/index.js';
import { mockBrand } from './mathHelpers/mockBrand.js';

// Perhaps makeCopyBag should coalesce duplicate labels, but for now, it does
// not.
const distinctLabels = pairs =>
new Set(pairs.map(([label, _qty]) => label)).size === pairs.length;
const positiveCounts = pairs =>
pairs.filter(([_l, qty]) => qty > 0n).length === pairs.length;
const arbBagContents = fc
.nat(7)
.chain(size =>
fc.array(
fc.tuple(fc.string(), fc.bigUint({ max: 1_000_000_000_000_000n })),
{ minLength: size, maxLength: size },
),
)
.filter(pairs => distinctLabels(pairs) && positiveCounts(pairs));

const arbAmount = arbBagContents.map(contents =>
m.make(mockBrand, harden(makeCopyBag(contents))),
);
import { mockBrand } from '../../tools/mockBrand.js';
import { arbAmount } from '../../tools/arb-amount.js';

// Note: we write P => Q as !P || Q since JS has no logical => operator
const implies = (p, q) => !p || q;
Expand Down
26 changes: 26 additions & 0 deletions packages/ERTP/tools/arb-amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { makeCopyBag } from '@agoric/store';
import { fc } from '@fast-check/ava';

import { AmountMath as m } from '../src/index.js';
import { mockBrand } from './mockBrand.js';

// Perhaps makeCopyBag should coalesce duplicate labels, but for now, it does
// not.
const distinctLabels = pairs =>
new Set(pairs.map(([label, _qty]) => label)).size === pairs.length;
const positiveCounts = pairs =>
pairs.filter(([_l, qty]) => qty > 0n).length === pairs.length;
const arbBagContents = fc
.nat(7)
.chain(size =>
fc.array(
fc.tuple(fc.string(), fc.bigUint({ max: 1_000_000_000_000_000n })),
{ minLength: size, maxLength: size },
),
)
.filter(pairs => distinctLabels(pairs) && positiveCounts(pairs));

// TODO: should include many non-bag amounts too
export const arbAmount = arbBagContents.map(contents =>
m.make(mockBrand, harden(makeCopyBag(contents))),
);
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Far } from '@endo/marshal';
import { AssetKind } from '../../../src/index.js';
import { AssetKind } from '../src/index.js';

/** @type {Brand<AssetKind>} */
export const mockBrand = Far('brand', {
// eslint-disable-next-line no-unused-vars
isMyIssuer: async allegedIssuer => false,
isMyIssuer: async _allegedIssuer => false,
getAllegedName: () => 'mock',
getAmountShape: () => {},
getDisplayInfo: () => ({
Expand Down

0 comments on commit 4e96088

Please sign in to comment.