|
| 1 | +import { a_chunk } from './chunk'; |
| 2 | + |
| 3 | +describe('a_chunk', () => { |
| 4 | + test('splits an evenly divisible array correctly', () => { |
| 5 | + const input = [1, 2, 3, 4, 5, 6]; |
| 6 | + const result = a_chunk(input, 2); |
| 7 | + expect(result).toEqual([ |
| 8 | + [1, 2], |
| 9 | + [3, 4], |
| 10 | + [5, 6], |
| 11 | + ]); |
| 12 | + }); |
| 13 | + |
| 14 | + test('handles chunk size of 1', () => { |
| 15 | + const input = [1, 2, 3]; |
| 16 | + const result = a_chunk(input, 1); |
| 17 | + expect(result).toEqual([[1], [2], [3]]); |
| 18 | + }); |
| 19 | + |
| 20 | + test('returns empty array when given an empty array', () => { |
| 21 | + const input: number[] = []; |
| 22 | + const result = a_chunk(input, 2); |
| 23 | + expect(result).toEqual([]); |
| 24 | + }); |
| 25 | + |
| 26 | + test('returns one chunk if chunk size equals the entire array length', () => { |
| 27 | + const input = [1, 2, 3, 4]; |
| 28 | + const result = a_chunk(input, 4); |
| 29 | + expect(result).toEqual([[1, 2, 3, 4]]); |
| 30 | + }); |
| 31 | + |
| 32 | + test('last chunk may be smaller if array length is not divisible by chunk size', () => { |
| 33 | + const input = [1, 2, 3, 4, 5]; |
| 34 | + const result = a_chunk(input, 2); |
| 35 | + expect(result).toEqual([[1, 2], [3, 4], [5]]); |
| 36 | + }); |
| 37 | + |
| 38 | + test('handles chunk size larger than the array length', () => { |
| 39 | + const input = [1, 2, 3]; |
| 40 | + const result = a_chunk(input, 10); |
| 41 | + expect(result).toEqual([[1, 2, 3]]); |
| 42 | + }); |
| 43 | + |
| 44 | + test('works with different data types (strings)', () => { |
| 45 | + const input = ['a', 'b', 'c', 'd']; |
| 46 | + const result = a_chunk(input, 2); |
| 47 | + expect(result).toEqual([ |
| 48 | + ['a', 'b'], |
| 49 | + ['c', 'd'], |
| 50 | + ]); |
| 51 | + }); |
| 52 | + |
| 53 | + test('works with mixed data types', () => { |
| 54 | + const input = [1, 'two', { three: 3 }, [4], null]; |
| 55 | + const result = a_chunk(input, 2); |
| 56 | + expect(result).toEqual([[1, 'two'], [{ three: 3 }, [4]], [null]]); |
| 57 | + }); |
| 58 | + |
| 59 | + test('handles a chunk size of 3', () => { |
| 60 | + const input = [1, 2, 3, 4, 5, 6, 7]; |
| 61 | + const result = a_chunk(input, 3); |
| 62 | + expect(result).toEqual([[1, 2, 3], [4, 5, 6], [7]]); |
| 63 | + }); |
| 64 | + |
| 65 | + test('does not modify the original array', () => { |
| 66 | + const input = [1, 2, 3, 4, 5]; |
| 67 | + const copy = [...input]; |
| 68 | + a_chunk(input, 2); |
| 69 | + expect(input).toEqual(copy); |
| 70 | + }); |
| 71 | + |
| 72 | + test('handles a large array efficiently', () => { |
| 73 | + const input = Array.from({ length: 1000 }, (_, i) => i + 1); |
| 74 | + const result = a_chunk(input, 100); |
| 75 | + expect(result.length).toBe(10); |
| 76 | + expect(result[0].length).toBe(100); |
| 77 | + }); |
| 78 | + |
| 79 | + test('handles chunk size equal to 2 with odd length input', () => { |
| 80 | + const input = [1, 2, 3]; |
| 81 | + const result = a_chunk(input, 2); |
| 82 | + expect(result).toEqual([[1, 2], [3]]); |
| 83 | + }); |
| 84 | + |
| 85 | + test('handles chunk size equal to the first half of array length', () => { |
| 86 | + const input = [1, 2, 3, 4, 5, 6]; |
| 87 | + const result = a_chunk(input, 3); |
| 88 | + expect(result).toEqual([ |
| 89 | + [1, 2, 3], |
| 90 | + [4, 5, 6], |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + test('works with a single-element array', () => { |
| 95 | + const input = [42]; |
| 96 | + const result = a_chunk(input, 5); |
| 97 | + expect(result).toEqual([[42]]); |
| 98 | + }); |
| 99 | + |
| 100 | + test('works with an array of booleans', () => { |
| 101 | + const input = [true, false, true, false, true]; |
| 102 | + const result = a_chunk(input, 2); |
| 103 | + expect(result).toEqual([[true, false], [true, false], [true]]); |
| 104 | + }); |
| 105 | + |
| 106 | + test('works with nested arrays as elements', () => { |
| 107 | + const input = [[1], [2], [3], [4]]; |
| 108 | + const result = a_chunk(input, 2); |
| 109 | + expect(result).toEqual([ |
| 110 | + [[1], [2]], |
| 111 | + [[3], [4]], |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + test('handles chunk size of 1 on large input', () => { |
| 116 | + const input = [1, 2, 3, 4, 5, 6]; |
| 117 | + const result = a_chunk(input, 1); |
| 118 | + expect(result).toEqual([[1], [2], [3], [4], [5], [6]]); |
| 119 | + }); |
| 120 | + |
| 121 | + test('returns the input if chunk size is equal to input length', () => { |
| 122 | + const input = [10, 20, 30]; |
| 123 | + const result = a_chunk(input, 3); |
| 124 | + expect(result).toEqual([[10, 20, 30]]); |
| 125 | + }); |
| 126 | + |
| 127 | + test('works with chunk size of 2 on an empty array', () => { |
| 128 | + const input: number[] = []; |
| 129 | + const result = a_chunk(input, 2); |
| 130 | + expect(result).toEqual([]); |
| 131 | + }); |
| 132 | + |
| 133 | + test('handles large chunk size with large input', () => { |
| 134 | + const input = Array.from({ length: 20 }, (_, i) => i + 1); |
| 135 | + const result = a_chunk(input, 25); |
| 136 | + expect(result).toEqual([input]); // entire array as one chunk |
| 137 | + }); |
| 138 | +}); |
0 commit comments