|
| 1 | +import test from 'ava'; |
| 2 | + |
| 3 | +import {list, range} from '@aureooms/js-itertools'; |
| 4 | + |
| 5 | +import {str} from './_fixtures.js'; |
| 6 | + |
| 7 | +import {from, rotate_left, rotate_right, len, values} from '../../src/index.js'; |
| 8 | + |
| 9 | +const toArray = (first) => list(values(first)); |
| 10 | + |
| 11 | +const immutability = (t, input, n) => { |
| 12 | + const x = from(input); |
| 13 | + const y = rotate_left(x, n); |
| 14 | + const z = rotate_right(y, n); |
| 15 | + const w = rotate_left(y, len(x) - n); |
| 16 | + |
| 17 | + t.is(z, x); |
| 18 | + t.deepEqual(toArray(z), toArray(x)); |
| 19 | + t.is(w, x); |
| 20 | + t.deepEqual(toArray(w), toArray(x)); |
| 21 | +}; |
| 22 | + |
| 23 | +immutability.title = (title, input, n) => |
| 24 | + title || `immutability (${str(input)}, ${n})`; |
| 25 | + |
| 26 | +test(immutability, [], 0); |
| 27 | +test(immutability, [], -1); |
| 28 | +test(immutability, [], 1); |
| 29 | +test(immutability, [], -100); |
| 30 | +test(immutability, [], 100); |
| 31 | +test(immutability, 'abc', 0); |
| 32 | +test(immutability, 'abc', -1); |
| 33 | +test(immutability, 'abc', 1); |
| 34 | +test(immutability, 'abc', -100); |
| 35 | +test(immutability, 'abc', 100); |
| 36 | +test(immutability, list(range(1000)), 0); |
| 37 | +test(immutability, list(range(1000)), -1); |
| 38 | +test(immutability, list(range(1000)), 1); |
| 39 | +test(immutability, list(range(1000)), -100); |
| 40 | +test(immutability, list(range(1000)), 100); |
| 41 | + |
| 42 | +const mutability = (t, rotate, input, n, output) => { |
| 43 | + const expected = list(output); |
| 44 | + const x = from(input); |
| 45 | + const y = rotate(x, n); |
| 46 | + const actual = toArray(y); |
| 47 | + t.deepEqual(actual, expected); |
| 48 | +}; |
| 49 | + |
| 50 | +mutability.title = (title, rotate, input, n, output) => |
| 51 | + title || `mutability ${rotate.name}(${str(input)}, ${n}) = ${str(output)}`; |
| 52 | + |
| 53 | +test(mutability, rotate_left, [], 0, []); |
| 54 | +test(mutability, rotate_left, [], -1, []); |
| 55 | +test(mutability, rotate_left, [], 1, []); |
| 56 | +test(mutability, rotate_left, [], -100, []); |
| 57 | +test(mutability, rotate_left, [], 100, []); |
| 58 | +test(mutability, rotate_right, [], 0, []); |
| 59 | +test(mutability, rotate_right, [], -1, []); |
| 60 | +test(mutability, rotate_right, [], 1, []); |
| 61 | +test(mutability, rotate_right, [], -100, []); |
| 62 | +test(mutability, rotate_right, [], 100, []); |
| 63 | +test(mutability, rotate_left, 'abc', 0, 'abc'); |
| 64 | +test(mutability, rotate_left, 'abc', -1, 'bca'); |
| 65 | +test(mutability, rotate_left, 'abc', 1, 'cab'); |
| 66 | +test(mutability, rotate_right, 'abc', 0, 'abc'); |
| 67 | +test(mutability, rotate_right, 'abc', -1, 'cab'); |
| 68 | +test(mutability, rotate_right, 'abc', 1, 'bca'); |
0 commit comments