|
| 1 | +import { rotateRight, rotateLeft } from '../RotateArray.js' |
| 2 | + |
| 3 | +describe('rotateRight', () => { |
| 4 | + it('should rotate array to the right by k positions', () => { |
| 5 | + expect(rotateRight([1, 2, 3, 4, 5], 2)).toEqual([4, 5, 1, 2, 3]) |
| 6 | + expect(rotateRight([1, 2, 3], 1)).toEqual([3, 1, 2]) |
| 7 | + }) |
| 8 | + |
| 9 | + it('should handle rotation count larger than array length', () => { |
| 10 | + expect(rotateRight([1, 2, 3], 5)).toEqual([2, 3, 1]) |
| 11 | + expect(rotateRight([1], 10)).toEqual([1]) |
| 12 | + }) |
| 13 | + |
| 14 | + it('should return empty array for empty input', () => { |
| 15 | + expect(rotateRight([], 3)).toEqual([]) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should handle zero rotation', () => { |
| 19 | + expect(rotateRight([1, 2, 3], 0)).toEqual([1, 2, 3]) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should throw TypeError for invalid inputs', () => { |
| 23 | + expect(() => rotateRight('not an array', 1)).toThrow(TypeError) |
| 24 | + expect(() => rotateRight([1, 2, 3], -1)).toThrow(TypeError) |
| 25 | + expect(() => rotateRight([1, 2, 3], 1.5)).toThrow(TypeError) |
| 26 | + }) |
| 27 | +}) |
| 28 | + |
| 29 | +describe('rotateLeft', () => { |
| 30 | + it('should rotate array to the left by k positions', () => { |
| 31 | + expect(rotateLeft([1, 2, 3, 4, 5], 2)).toEqual([3, 4, 5, 1, 2]) |
| 32 | + expect(rotateLeft([1, 2, 3], 1)).toEqual([2, 3, 1]) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should handle rotation count larger than array length', () => { |
| 36 | + expect(rotateLeft([1, 2, 3], 5)).toEqual([3, 1, 2]) |
| 37 | + expect(rotateLeft([1], 10)).toEqual([1]) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should return empty array for empty input', () => { |
| 41 | + expect(rotateLeft([], 3)).toEqual([]) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should handle zero rotation', () => { |
| 45 | + expect(rotateLeft([1, 2, 3], 0)).toEqual([1, 2, 3]) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should throw TypeError for invalid inputs', () => { |
| 49 | + expect(() => rotateLeft('not an array', 1)).toThrow(TypeError) |
| 50 | + expect(() => rotateLeft([1, 2, 3], -1)).toThrow(TypeError) |
| 51 | + expect(() => rotateLeft([1, 2, 3], 1.5)).toThrow(TypeError) |
| 52 | + }) |
| 53 | +}) |
0 commit comments