|
| 1 | +import { describe, expect, it } from '@rstest/core'; |
| 2 | +import { |
| 3 | + chunk, |
| 4 | + findMax, |
| 5 | + flatten, |
| 6 | + removeDuplicates, |
| 7 | + shuffle, |
| 8 | +} from '../src/array'; |
| 9 | + |
| 10 | +describe('Array Utils', () => { |
| 11 | + describe('removeDuplicates', () => { |
| 12 | + it('should remove duplicate numbers', () => { |
| 13 | + expect(removeDuplicates([1, 2, 2, 3, 3, 4])).toEqual([1, 2, 3, 4]); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should remove duplicate strings', () => { |
| 17 | + expect(removeDuplicates(['a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should handle empty array', () => { |
| 21 | + expect(removeDuplicates([])).toEqual([]); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('chunk', () => { |
| 26 | + it('should chunk array into specified size', () => { |
| 27 | + expect(chunk([1, 2, 3, 4, 5, 6], 2)).toEqual([ |
| 28 | + [1, 2], |
| 29 | + [3, 4], |
| 30 | + [5, 6], |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle remainder elements', () => { |
| 35 | + expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should throw error for invalid chunk size', () => { |
| 39 | + expect(() => chunk([1, 2, 3], 0)).toThrow( |
| 40 | + 'Chunk size must be greater than 0', |
| 41 | + ); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('flatten', () => { |
| 46 | + it('should flatten nested arrays', () => { |
| 47 | + expect(flatten([1, [2, 3], [4, [5, 6]]])).toEqual([1, 2, 3, 4, 5, 6]); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle empty arrays', () => { |
| 51 | + expect(flatten([])).toEqual([]); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('findMax', () => { |
| 56 | + it('should find maximum number', () => { |
| 57 | + expect(findMax([1, 5, 3, 9, 2])).toBe(9); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw error for empty array', () => { |
| 61 | + expect(() => findMax([])).toThrow('Array cannot be empty'); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('shuffle', () => { |
| 66 | + it('should return array with same length', () => { |
| 67 | + const original = [1, 2, 3, 4, 5]; |
| 68 | + const shuffled = shuffle(original); |
| 69 | + expect(shuffled).toHaveLength(original.length); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should contain all original elements', () => { |
| 73 | + const original = [1, 2, 3, 4, 5]; |
| 74 | + const shuffled = shuffle(original); |
| 75 | + expect(shuffled.sort()).toEqual(original.sort()); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments