Skip to content

Commit 039744c

Browse files
committed
fixup! fix and test set bound
1 parent 087a6fc commit 039744c

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

packages/ERTP/src/mathHelpers/setMathHelpers.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @jessie-check
22

3+
import { Fail, q } from '@endo/errors';
34
import { passStyleOf } from '@endo/pass-style';
45
import { isKey, assertKey, mustMatch, matches } from '@endo/patterns';
56
import {
@@ -57,6 +58,7 @@ export const setMathHelpers = harden({
5758
return true;
5859
}
5960
}
61+
return false;
6062
},
6163
doIsEqual: (x, y) => elementsCompare(x, y) === 0,
6264
doAdd: elementsDisjointUnion,
@@ -69,19 +71,23 @@ export const setMathHelpers = harden({
6971
payload: [elementPatt, bound],
7072
} = rightBound;
7173
if (bound === 0n) {
72-
return [];
74+
return left;
7375
}
7476
let count = 0n;
7577
const result = [];
7678
for (const element of left) {
77-
if (matches(element, elementPatt)) {
78-
count += 1n;
79+
if (count < bound) {
80+
if (matches(element, elementPatt)) {
81+
count += 1n;
82+
} else {
83+
result.push(element);
84+
}
7985
} else {
8086
result.push(element);
8187
}
82-
if (count >= bound) {
83-
return result;
84-
}
8588
}
89+
count >= bound ||
90+
Fail`Only has ${q(count)} matches, but needs at least ${bound}`;
91+
return result;
8692
},
8793
});

packages/ERTP/test/unitTests/mathHelpers/copySetMathHelpers.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ test('copySet with strings subtract', t => {
287287
harden({ brand: mockBrand, value: makeCopySet(['a']) }),
288288
),
289289
{ brand: mockBrand, value: makeCopySet(['b']) },
290-
`['a', 'b'] - ['a'] = ['a']`,
290+
`['a', 'b'] - ['a'] = ['b']`,
291291
);
292292
});
293293

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
2+
import { M } from '@endo/patterns';
3+
4+
import { AmountMath as m } from '../../../src/index.js';
5+
import { mockSetBrand as mockBrand } from './mockBrand.js';
6+
7+
const mock = value => harden({ brand: mockBrand, value });
8+
9+
// The "unit tests" for MathHelpers actually make the calls through
10+
// AmountMath so that we can test that any duplication is handled
11+
// correctly.
12+
13+
test('set minus setBound', t => {
14+
t.deepEqual(
15+
m.subtract(mock(['a', 'b']), mock(M.has(M.any(), 1n))),
16+
mock(['a']),
17+
);
18+
t.deepEqual(m.subtract(mock(['a', 'b']), mock(M.has('a', 1n))), mock(['b']));
19+
t.deepEqual(
20+
m.subtract(mock(['a', 'b']), mock(M.has(M.any(), 1n))),
21+
mock(['a']),
22+
);
23+
t.deepEqual(
24+
m.subtract(mock(['a', 'b']), mock(M.has(M.any(), 0n))),
25+
mock(['b', 'a']),
26+
);
27+
t.deepEqual(m.subtract(mock(['a', 'b']), mock(M.has(M.any(), 2n))), mock([]));
28+
29+
t.throws(() => m.subtract(mock(['a', 'b']), mock(M.has(M.any(), 3n))), {
30+
message: 'Only has "[2n]" matches, but needs at least "[3n]"',
31+
});
32+
33+
t.throws(() => m.subtract(mock(['a', 'b']), mock(M.has('c', 1n))), {
34+
message: 'Only has "[0n]" matches, but needs at least "[1n]"',
35+
});
36+
37+
t.throws(() => m.subtract(mock(['a', 'b']), mock(M.has(M.any()))), {
38+
message:
39+
'right amount bound: value: "[match:has]" - Must match one of ["[match:or]","[match:tagged]"]',
40+
});
41+
});
42+
43+
test('set isGTE setBound', t => {
44+
t.true(m.isGTE(mock(['a', 'b']), mock(M.has(M.any(), 1n))));
45+
t.true(m.isGTE(mock(['a', 'b']), mock(M.has('a', 1n))));
46+
t.true(m.isGTE(mock(['a', 'b']), mock(M.has(M.any(), 1n))));
47+
t.true(m.isGTE(mock(['a', 'b']), mock(M.has(M.any(), 0n))));
48+
t.true(m.isGTE(mock(['a', 'b']), mock(M.has(M.any(), 2n))));
49+
t.false(m.isGTE(mock(['a', 'b']), mock(M.has(M.any(), 3n))));
50+
t.false(m.isGTE(mock(['a', 'b']), mock(M.has('c', 1n))));
51+
52+
t.throws(() => m.isGTE(mock(['a', 'b']), mock(M.has(M.any()))), {
53+
message:
54+
'right amount bound: value: "[match:has]" - Must match one of ["[match:or]","[match:tagged]"]',
55+
});
56+
});

packages/ERTP/test/unitTests/mathHelpers/setMathHelpers.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { mockSetBrand as mockBrand } from './mockBrand.js';
99
// AmountMath so that we can test that any duplication is handled
1010
// correctly.
1111

12-
const runSetMathHelpersTests = (t, [a, b, c], a2) => {
12+
const runSetMathHelpersTests = (
13+
t,
14+
[a, b, c],
15+
a2 = /** @type {any} */ (undefined),
16+
) => {
1317
// a2 is a copy of a which should have the same values but not same
1418
// identity. This doesn't make sense to use for handle tests, but
1519
// makes sense for anything where the identity is based on data.

0 commit comments

Comments
 (0)