|
| 1 | +import test from 'ava'; |
| 2 | + |
| 3 | +import {alloc} from '@array-like/alloc'; |
| 4 | +import {copy} from '@array-like/copy'; |
| 5 | + |
| 6 | +import {reverse} from '../../src/index.js'; |
| 7 | + |
| 8 | +const macro = (t, a, i, j, expected) => { |
| 9 | + const reference = alloc(a.length); |
| 10 | + copy(a, 0, a.length, reference, 0); |
| 11 | + |
| 12 | + reverse(a, i, j); |
| 13 | + t.deepEqual(a, expected, 'reverse x 1'); |
| 14 | + |
| 15 | + reverse(a, i, j); |
| 16 | + t.deepEqual(a, reference, 'reverse x 2'); |
| 17 | +}; |
| 18 | + |
| 19 | +const repr = JSON.stringify; |
| 20 | + |
| 21 | +macro.title = (title, a, i, j, expected) => |
| 22 | + title ?? `reverse(${repr(a)}, ${i}, ${j}) is ${repr(expected)}`; |
| 23 | + |
| 24 | +const immutable = (t, a, i, j) => macro(t, a, i, j, a.slice()); |
| 25 | +immutable.title = (title, a, i, j) => macro.title(title, a, i, j, a.slice()); |
| 26 | + |
| 27 | +const whole = (t, a, expected) => macro(t, a, 0, a.length, expected); |
| 28 | +whole.title = (title, a, expected) => |
| 29 | + macro.title(title, a, 0, a.length, expected); |
| 30 | + |
| 31 | +test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 4, 4); |
| 32 | +test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 5); |
| 33 | +test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 1); |
| 34 | +test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, -1); |
| 35 | +test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 1, 0); |
| 36 | +test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 9, 0); |
| 37 | +test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 27, -32); |
| 38 | + |
| 39 | +test(whole, [], []); |
| 40 | +test(whole, [1], [1]); |
| 41 | +test(whole, [1, 2, 3], [3, 2, 1]); |
| 42 | +test(whole, [1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1]); |
| 43 | +test(whole, [1, 2, 3, 4, 5, 6, 7, 8, 9], [9, 8, 7, 6, 5, 4, 3, 2, 1]); |
| 44 | + |
| 45 | +test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 7, [1, 2, 3, 7, 6, 5, 4, 8, 9]); |
| 46 | +test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 8, [1, 2, 3, 8, 7, 6, 5, 4, 9]); |
| 47 | +test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 9, [1, 2, 3, 9, 8, 7, 6, 5, 4]); |
| 48 | +test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 6, [6, 5, 4, 3, 2, 1, 7, 8, 9]); |
| 49 | +test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 5, [5, 4, 3, 2, 1, 6, 7, 8, 9]); |
| 50 | +test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 4, [4, 3, 2, 1, 5, 6, 7, 8, 9]); |
0 commit comments