-
Notifications
You must be signed in to change notification settings - Fork 191
/
test-rankOrder.js
368 lines (340 loc) · 8.88 KB
/
test-rankOrder.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// @ts-check
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { Far, makeTagged } from '@endo/marshal';
import fc from 'fast-check';
import {
FullRankCover,
compareRank,
isRankSorted,
sortByRank,
getIndexCover,
getPassStyleCover,
assertRankSorted,
} from '../src/patterns/rankOrder.js';
const { quote: q } = assert;
/**
* The only elements with identity. Everything else should be equal
* by contents.
*/
const alice = Far('alice', {});
const bob = Far('bob', {});
const carol = Far('carol', {});
/**
* A factory for arbitrary passables
*/
const { passable } = fc.letrec(tie => {
return {
passable: tie('dag').map(x => harden(x)),
dag: fc.oneof(
{ depthFactor: 0.5, withCrossShrink: true },
// a tagged value whose payload is an array of [key, leaf] pairs
// where each key is unique within the payload
// XXX can the payload be generalized further?
fc
.record({
type: fc.constantFrom('copyMap', 'copySet', 'nonsense'),
payload: fc.set(fc.fullUnicodeString(), { maxLength: 3 }).chain(k => {
return fc.tuple(fc.constant(k), tie('leaf'));
}),
})
.map(({ type, payload }) => makeTagged(type, payload)),
fc.array(tie('dag'), { maxLength: 3 }),
fc.dictionary(
fc.fullUnicodeString().filter(s => s !== 'then'),
tie('dag'),
{ maxKeys: 3 },
),
tie('dag').map(v => Promise.resolve(v)),
tie('leaf'),
),
leaf: fc.oneof(
fc.record({}),
fc.fullUnicodeString(),
fc.fullUnicodeString().map(s => Symbol.for(s)),
fc.fullUnicodeString().map(s => new Error(s)),
// primordial symbols and registered lookalikes
fc.constantFrom(
...Object.getOwnPropertyNames(Symbol).flatMap(k => {
const v = Symbol[k];
if (typeof v !== 'symbol') return [];
return [v, Symbol.for(k), Symbol.for(`@@${k}`)];
}),
),
fc.bigInt(),
fc.integer(),
fc.constantFrom(-0, NaN, Infinity, -Infinity),
fc.constantFrom(null, undefined, false, true),
fc.constantFrom(alice, bob, carol),
// unresolved promise
fc.constant(new Promise(() => {})),
),
};
});
test('compareRank is reflexive', async t => {
await fc.assert(
fc.property(passable, x => {
return t.is(compareRank(x, x), 0);
}),
);
});
test('compareRank totally orders ranks', async t => {
await fc.assert(
fc.property(passable, passable, (a, b) => {
const ab = compareRank(a, b);
const ba = compareRank(b, a);
if (ab === 0) {
return t.is(ba, 0);
}
return (
t.true(Math.abs(ab) > 0) &&
t.true(Math.abs(ba) > 0) &&
t.is(Math.sign(ba), -Math.sign(ab))
);
}),
);
});
test('compareRank is transitive', async t => {
await fc.assert(
fc.property(
// operate on a set of three passables covering at least two ranks
fc
.set(passable, { minLength: 3, maxLength: 3 })
.filter(
([a, b, c]) => compareRank(a, b) !== 0 || compareRank(a, c) !== 0,
),
triple => {
const sorted = harden(triple.sort(compareRank));
assertRankSorted(sorted, compareRank);
const [a, b, c] = sorted;
const failures = [];
let result;
let resultOk;
result = compareRank(a, b);
resultOk = t.true(result <= 0, 'a <= b');
if (!resultOk) {
failures.push(`Expected <= 0: ${result} from ${q(a)} vs. ${q(b)}`);
}
result = compareRank(a, c);
resultOk = t.true(result <= 0, 'a <= c');
if (!resultOk) {
failures.push(`Expected <= 0: ${result} from ${q(a)} vs. ${q(c)}`);
}
result = compareRank(b, c);
resultOk = t.true(result <= 0, 'b <= c');
if (!resultOk) {
failures.push(`Expected <= 0: ${result} from ${q(b)} vs. ${q(c)}`);
}
result = compareRank(c, b);
resultOk = t.true(result >= 0, 'c >= b');
if (!resultOk) {
failures.push(`Expected >= 0: ${result} from ${q(c)} vs. ${q(b)}`);
}
result = compareRank(c, a);
resultOk = t.true(result >= 0, 'c >= a');
if (!resultOk) {
failures.push(`Expected >= 0: ${result} from ${q(c)} vs. ${q(a)}`);
}
result = compareRank(b, a);
resultOk = t.true(result >= 0, 'b >= a');
if (!resultOk) {
failures.push(`Expected >= 0: ${result} from ${q(b)} vs. ${q(a)}`);
}
return t.deepEqual(failures, []);
},
),
);
});
/**
* An unordered copyArray of some passables
*/
export const sample = harden([
makeTagged('copySet', [
['b', 3],
['a', 4],
]),
'foo',
3n,
'barr',
undefined,
[5, { foo: 4 }],
2,
null,
[5, { foo: 4, bar: null }],
bob,
0,
makeTagged('copySet', [
['a', 4],
['b', 3],
]),
NaN,
true,
undefined,
-Infinity,
[5],
alice,
[],
Symbol.for('foo'),
new Error('not erroneous'),
Symbol.for('@@foo'),
[5, { bar: 5 }],
Symbol.for(''),
false,
carol,
-0,
{},
[5, undefined],
-3,
makeTagged('copyMap', [
['a', 4],
['b', 3],
]),
true,
'bar',
[5, null],
new Promise(() => {}), // forever unresolved
makeTagged('nonsense', [
['a', 4],
['b', 3],
]),
Infinity,
Symbol.isConcatSpreadable,
[5, { foo: 4, bar: undefined }],
Promise.resolve('fulfillment'),
[5, { foo: 4 }],
]);
const rejectedP = Promise.reject(new Error('broken'));
rejectedP.catch(() => {}); // Suppress unhandled rejection warning/error
/**
* The correctly stable rank sorting of `sample`
*/
const sortedSample = harden([
// All errors are tied.
new Error('different'),
{},
// Lexicographic tagged: tag then payload
makeTagged('copyMap', [
['a', 4],
['b', 3],
]),
makeTagged('copySet', [
['a', 4],
['b', 3],
]),
// Doesn't care if a valid copySet
makeTagged('copySet', [
['b', 3],
['a', 4],
]),
// Doesn't care if a recognized tagged tag
makeTagged('nonsense', [
['a', 4],
['b', 3],
]),
// All promises are tied.
rejectedP,
rejectedP,
// Lexicographic arrays. Shorter beats longer.
// Lexicographic records by reverse sorted property name, then by values
// in that order.
[],
[5],
[5, { bar: 5 }],
[5, { foo: 4 }],
[5, { foo: 4 }],
[5, { foo: 4, bar: null }],
[5, { foo: 4, bar: undefined }],
[5, null],
[5, undefined],
false,
true,
true,
// -0 is equivalent enough to 0. NaN after all numbers.
-Infinity,
-3,
-0,
0,
2,
Infinity,
NaN,
3n,
// All remotables are tied for the same rank and the sort is stable,
// so their relative order is preserved
bob,
alice,
carol,
// Lexicographic strings. Shorter beats longer.
// TODO Probe UTF-16 vs Unicode vs UTF-8 (Moddable) ordering.
'bar',
'barr',
'foo',
null,
Symbol.for(''),
Symbol.for('@@foo'),
Symbol.isConcatSpreadable,
Symbol.for('foo'),
undefined,
undefined,
]);
test('compare and sort by rank', t => {
assertRankSorted(sortedSample, compareRank);
t.false(isRankSorted(sample, compareRank));
const sorted = sortByRank(sample, compareRank);
t.is(
compareRank(sorted, sortedSample),
0,
`Not sorted as expected: ${q(sorted)}`,
);
});
const rangeSample = harden([
{}, // 0 -- prefix are earlier, so empty is earliest
{ bar: null }, // 1
{ bar: undefined }, // 2 -- records with same names grouped together
{ foo: 'x' }, // 3 -- name subsets before supersets
{ bar: 'y', foo: 'x' }, // 5
{ bar: 'y', foo: 'x' }, // 6
{ bar: null, foo: 'x' }, // 4
{ bar: undefined, foo: 'x' }, // 7
{ bar: 'y', foo: 'y' }, // 8 -- reverse sort so foo: tested before bar:
makeTagged('', null), // 9
['a'], // 10
['a', 'b'], // 11
['a', 'x'], // 12
['y', 'x'], // 13
]);
/** @type {[RankCover, IndexCover][]} */
const queries = harden([
[
[['c'], ['c']],
// first > last implies absent.
[12, 11],
],
[
[['a'], ['a', undefined]],
[9, 11],
],
[
[
['a', null],
['a', undefined],
],
[10, 11],
],
[FullRankCover, [0, 13]],
[getPassStyleCover('string'), [0, -1]],
[getPassStyleCover('copyRecord'), [0, 8]],
[getPassStyleCover('copyArray'), [9, 13]], // cover includes non-array
[getPassStyleCover('remotable'), [14, 13]],
]);
// XXX This test is skipped because of unresolved impedance mismatch between the
// older value-as-cover scheme and the newer string-encoded-key-as-cover scheme
// that we currently use. Whoever sorts that mismatch out (likely as part of
// adding composite key handling to the durable store implementation) will need
// to reenable and (likely) update this test.
test.skip('range queries', t => {
t.assert(isRankSorted(rangeSample, compareRank));
for (const [rankCover, indexRange] of queries) {
const range = getIndexCover(rangeSample, compareRank, rankCover);
t.is(range[0], indexRange[0]);
t.is(range[1], indexRange[1]);
}
});