|
| 1 | +import test from 'ava'; |
| 2 | + |
| 3 | +import {sorted} from '../../src/index.js'; |
| 4 | +import * as primitive from '@total-order/primitive'; |
| 5 | +import {tee} from '@aureooms/js-itertools'; |
| 6 | + |
| 7 | +const increasing = (a, b) => primitive.increasing(a, b); |
| 8 | +const decreasing = (a, b) => primitive.decreasing(a, b); |
| 9 | + |
| 10 | +const macro = (t, compare, array) => { |
| 11 | + const [reference, input] = tee(array, 2); |
| 12 | + |
| 13 | + const output = sorted(compare, input); |
| 14 | + const expected = Array.from(reference).sort(compare); |
| 15 | + |
| 16 | + t.not(output, input, 'check that output is not the same object as input'); |
| 17 | + t.is(output.length, expected.length, 'length check'); |
| 18 | + t.deepEqual(output, expected, 'check that output is as expected'); |
| 19 | +}; |
| 20 | + |
| 21 | +macro.title = (title, compare, array) => |
| 22 | + title ?? `sorted(${compare.name}, ${JSON.stringify(array)})`; |
| 23 | + |
| 24 | +for (const compare of [increasing, decreasing]) { |
| 25 | + const array = []; |
| 26 | + |
| 27 | + const n = 100; |
| 28 | + |
| 29 | + for (let i = n; i--; ) { |
| 30 | + array.push(Math.random()); |
| 31 | + } |
| 32 | + |
| 33 | + test(macro, compare, array); |
| 34 | +} |
0 commit comments