|
| 1 | +import { rotate } from '../Rotate.js' |
| 2 | + |
| 3 | +describe('rotate', () => { |
| 4 | + test('rotate([1,2,3,4,5], 2) returns [4,5,1,2,3]', () => { |
| 5 | + expect(rotate([1, 2, 3, 4, 5], 2)).toEqual([4, 5, 1, 2, 3]) |
| 6 | + }) |
| 7 | + |
| 8 | + test('rotate([1,2,3], 1) returns [3,1,2]', () => { |
| 9 | + expect(rotate([1, 2, 3], 1)).toEqual([3, 1, 2]) |
| 10 | + }) |
| 11 | + |
| 12 | + test('rotate([1,2,3], 3) returns [1,2,3]', () => { |
| 13 | + expect(rotate([1, 2, 3], 3)).toEqual([1, 2, 3]) |
| 14 | + }) |
| 15 | + |
| 16 | + test('rotate([1,2,3], 0) returns [1,2,3]', () => { |
| 17 | + expect(rotate([1, 2, 3], 0)).toEqual([1, 2, 3]) |
| 18 | + }) |
| 19 | + |
| 20 | + test('rotate with k larger than array length', () => { |
| 21 | + expect(rotate([1, 2, 3], 4)).toEqual([3, 1, 2]) |
| 22 | + }) |
| 23 | + |
| 24 | + test('rotate with negative k rotates left', () => { |
| 25 | + expect(rotate([1, 2, 3, 4, 5], -2)).toEqual([3, 4, 5, 1, 2]) |
| 26 | + }) |
| 27 | + |
| 28 | + test('rotate empty array returns empty array', () => { |
| 29 | + expect(rotate([], 3)).toEqual([]) |
| 30 | + }) |
| 31 | + |
| 32 | + test('rotate single element returns same array', () => { |
| 33 | + expect(rotate([1], 5)).toEqual([1]) |
| 34 | + }) |
| 35 | + |
| 36 | + test('rotate does not mutate original array', () => { |
| 37 | + const arr = [1, 2, 3, 4, 5] |
| 38 | + rotate(arr, 2) |
| 39 | + expect(arr).toEqual([1, 2, 3, 4, 5]) |
| 40 | + }) |
| 41 | + |
| 42 | + test('throws TypeError for non-array input', () => { |
| 43 | + expect(() => rotate('123', 2)).toThrow(TypeError) |
| 44 | + }) |
| 45 | + |
| 46 | + test('throws TypeError for float k', () => { |
| 47 | + expect(() => rotate([1, 2, 3], 1.5)).toThrow(TypeError) |
| 48 | + }) |
| 49 | +}) |
0 commit comments