|
1 |
| -import { describe, it, expect } from 'vitest'; |
| 1 | +import { expect, it } from 'vitest'; |
2 | 2 | import { splitArray } from './split-array';
|
3 | 3 |
|
4 |
| -describe('splitArray', () => { |
5 |
| - it('should split an array into chunks of the specified size', () => { |
6 |
| - const array = [1, 2, 3, 4, 5]; |
7 |
| - const size = 2; |
8 |
| - const result = splitArray(array, size); |
9 |
| - expect(result).toEqual([[1, 2], [3, 4], [5]]); |
10 |
| - }); |
11 |
| - |
12 |
| - it('should return an empty array when the input array is empty', () => { |
13 |
| - const array: number[] = []; |
14 |
| - const size = 2; |
15 |
| - const result = splitArray(array, size); |
16 |
| - expect(result).toEqual([]); |
17 |
| - }); |
18 |
| - |
19 |
| - it('should return the original array when the chunk size is greater than the array length', () => { |
20 |
| - const array = [1, 2, 3]; |
21 |
| - const size = 5; |
22 |
| - const result = splitArray(array, size); |
23 |
| - expect(result).toEqual([[1, 2, 3]]); |
24 |
| - }); |
25 |
| - |
26 |
| - it('should return the original array when the chunk size is equal to the array length', () => { |
27 |
| - const array = [1, 2, 3]; |
28 |
| - const size = 3; |
29 |
| - const result = splitArray(array, size); |
30 |
| - expect(result).toEqual([[1, 2, 3]]); |
31 |
| - }); |
32 |
| - |
33 |
| - it('should handle chunk size of 1 correctly', () => { |
34 |
| - const array = [1, 2, 3]; |
35 |
| - const size = 1; |
36 |
| - const result = splitArray(array, size); |
37 |
| - expect(result).toEqual([[1], [2], [3]]); |
38 |
| - }); |
39 |
| - |
40 |
| - it('should throw an error for chunk size of 0', () => { |
41 |
| - const array = [1, 2, 3]; |
42 |
| - const size = 0; |
43 |
| - expect(() => splitArray(array, size)).toThrow( |
44 |
| - 'chunkSize must be greater than 0', |
45 |
| - ); |
46 |
| - }); |
47 |
| - |
48 |
| - it('should throw an error for negative chunk size', () => { |
49 |
| - const array = [1, 2, 3]; |
50 |
| - const size = -1; |
51 |
| - expect(() => splitArray(array, size)).toThrow( |
52 |
| - 'chunkSize must be greater than 0', |
53 |
| - ); |
54 |
| - }); |
55 |
| - |
56 |
| - it('should handle non-integer chunk size by flooring the size', () => { |
57 |
| - const array = [1, 2, 3, 4, 5]; |
58 |
| - const size = 2.5; |
59 |
| - const result = splitArray(array, Math.floor(size)); |
60 |
| - expect(result).toEqual([[1, 2], [3, 4], [5]]); |
61 |
| - }); |
| 4 | +it('should split an array into chunks of the specified size', () => { |
| 5 | + const array = [1, 2, 3, 4, 5]; |
| 6 | + const size = 2; |
| 7 | + const result = splitArray(array, size); |
| 8 | + expect(result).toEqual([[1, 2], [3, 4], [5]]); |
| 9 | +}); |
| 10 | + |
| 11 | +it('should return an empty array when the input array is empty', () => { |
| 12 | + const array: number[] = []; |
| 13 | + const size = 2; |
| 14 | + const result = splitArray(array, size); |
| 15 | + expect(result).toEqual([]); |
| 16 | +}); |
| 17 | + |
| 18 | +it('should return the original array when the chunk size is greater than the array length', () => { |
| 19 | + const array = [1, 2, 3]; |
| 20 | + const size = 5; |
| 21 | + const result = splitArray(array, size); |
| 22 | + expect(result).toEqual([[1, 2, 3]]); |
| 23 | +}); |
| 24 | + |
| 25 | +it('should return the original array when the chunk size is equal to the array length', () => { |
| 26 | + const array = [1, 2, 3]; |
| 27 | + const size = 3; |
| 28 | + const result = splitArray(array, size); |
| 29 | + expect(result).toEqual([[1, 2, 3]]); |
| 30 | +}); |
| 31 | + |
| 32 | +it('should handle chunk size of 1 correctly', () => { |
| 33 | + const array = [1, 2, 3]; |
| 34 | + const size = 1; |
| 35 | + const result = splitArray(array, size); |
| 36 | + expect(result).toEqual([[1], [2], [3]]); |
| 37 | +}); |
| 38 | + |
| 39 | +it('should throw an error for chunk size of 0', () => { |
| 40 | + const array = [1, 2, 3]; |
| 41 | + const size = 0; |
| 42 | + expect(() => splitArray(array, size)).toThrow( |
| 43 | + 'chunkSize must be greater than 0', |
| 44 | + ); |
| 45 | +}); |
| 46 | + |
| 47 | +it('should throw an error for negative chunk size', () => { |
| 48 | + const array = [1, 2, 3]; |
| 49 | + const size = -1; |
| 50 | + expect(() => splitArray(array, size)).toThrow( |
| 51 | + 'chunkSize must be greater than 0', |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +it('should handle non-integer chunk size by flooring the size', () => { |
| 56 | + const array = [1, 2, 3, 4, 5]; |
| 57 | + const size = 2.5; |
| 58 | + const result = splitArray(array, Math.floor(size)); |
| 59 | + expect(result).toEqual([[1, 2], [3, 4], [5]]); |
62 | 60 | });
|
0 commit comments