|
| 1 | +import test from 'ava'; |
| 2 | +import { |
| 3 | + list, |
| 4 | + all, |
| 5 | + map, |
| 6 | + sorted, |
| 7 | + range, |
| 8 | + enumerate, |
| 9 | + nrepeat |
| 10 | +} from '@aureooms/js-itertools'; |
| 11 | +import {quasilexicographical, increasing} from '@aureooms/js-compare'; |
| 12 | +import {randint} from '@aureooms/js-random'; |
| 13 | + |
| 14 | +import sort from '../../../src/array/api/sort'; |
| 15 | + |
| 16 | +const isStable = (t, k, M, data) => { |
| 17 | + t.true(k >= 0); |
| 18 | + const result = sort(k, M, data.slice()); |
| 19 | + t.true(data.length <= 1 || all(map((tuple) => tuple.length >= k, data))); |
| 20 | + const lex = quasilexicographical(increasing); |
| 21 | + const expected = sorted(lex, data); |
| 22 | + t.deepEqual(expected, result); |
| 23 | +}; |
| 24 | + |
| 25 | +const repr = (data) => |
| 26 | + data.length >= 20 |
| 27 | + ? `[${JSON.stringify(data.slice(0, 9)).slice(1, -1)},..,${JSON.stringify( |
| 28 | + data.slice(-9) |
| 29 | + ).slice(1, -1)}]` |
| 30 | + : JSON.stringify(data); |
| 31 | + |
| 32 | +isStable.title = (title, k, M, data) => |
| 33 | + title || `sort(${k}, ${M}, ${repr(data)}) is stable`; |
| 34 | + |
| 35 | +test(isStable, 0, 0, []); |
| 36 | +test(isStable, 0, 0, list(map((x) => [x], range(1000)))); |
| 37 | +test( |
| 38 | + isStable, |
| 39 | + 1, |
| 40 | + 1, |
| 41 | + list(map(([a, b]) => [b, a], enumerate(nrepeat(0, 1000)))) |
| 42 | +); |
| 43 | +test( |
| 44 | + isStable, |
| 45 | + 2, |
| 46 | + 2, |
| 47 | + list(map((i) => [randint(0, 2), randint(0, 2), i], range(1000))) |
| 48 | +); |
0 commit comments