Skip to content

Commit 49f8d87

Browse files
feat: enhance test suite comprehensiveness while maintaining 100% coverage
- Expand from 1 basic test to 10 comprehensive tests covering all scenarios - Add boundary condition tests for 65536 element optimization threshold - Test both performance optimization paths (array.length vs new Array()) - Validate type safety with generic type annotations - Cover edge cases: empty arrays, sparse arrays, very large arrays - Ensure consistent behavior across small/large array boundary - Maintain 100% statement, branch, function, and line coverage - Improve test organization with descriptive grouping and documentation
1 parent 1d702ee commit 49f8d87

1 file changed

Lines changed: 104 additions & 7 deletions

File tree

tests/arrayInit.test.ts

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,113 @@
11
import { describe, it, expect } from 'vitest';
2-
import arrayInit from '../src/arrayInit';
2+
import arrayInit from '../src/arrayInit.js';
33

44
function validate (length: number): void
55
{
6-
expect(arrayInit<any>(length).length).equal(length);
6+
const array = arrayInit<any>(length);
7+
expect(array.length).equal(length);
8+
expect(Array.isArray(array)).toBe(true);
79
}
810

911
describe('arrayInit', () => {
10-
it('produces proper length arrays', () => {
11-
validate(10);
12-
validate(10000);
13-
validate(70000);
14-
validate(700000);
12+
describe('basic functionality', () => {
13+
it('produces proper length arrays', () => {
14+
validate(10);
15+
validate(10000);
16+
validate(70000);
17+
validate(700000);
18+
});
19+
20+
it('creates arrays with correct length for boundary values', () => {
21+
// Test the 65536 boundary specifically
22+
validate(65535); // Just below threshold - uses array.length = length
23+
validate(65536); // At threshold - uses new Array(length)
24+
validate(65537); // Just above threshold - uses new Array(length)
25+
});
26+
27+
it('handles edge cases', () => {
28+
validate(0); // Empty array
29+
validate(1); // Single element
30+
validate(2); // Small array
31+
});
32+
});
33+
34+
describe('performance optimization paths', () => {
35+
it('uses array.length assignment for small arrays', () => {
36+
const smallArray = arrayInit<string>(1000);
37+
expect(smallArray.length).toBe(1000);
38+
expect(Array.isArray(smallArray)).toBe(true);
39+
// All elements should be undefined (holes)
40+
expect(smallArray[0]).toBeUndefined();
41+
expect(smallArray[999]).toBeUndefined();
42+
});
43+
44+
it('uses new Array() constructor for large arrays', () => {
45+
const largeArray = arrayInit<number>(100000);
46+
expect(largeArray.length).toBe(100000);
47+
expect(Array.isArray(largeArray)).toBe(true);
48+
// All elements should be undefined (holes)
49+
expect(largeArray[0]).toBeUndefined();
50+
expect(largeArray[99999]).toBeUndefined();
51+
});
52+
});
53+
54+
describe('type safety', () => {
55+
it('works with generic type annotations', () => {
56+
const stringArray = arrayInit<string>(5);
57+
const numberArray = arrayInit<number>(3);
58+
const booleanArray = arrayInit<boolean>(2);
59+
60+
expect(stringArray.length).toBe(5);
61+
expect(numberArray.length).toBe(3);
62+
expect(booleanArray.length).toBe(2);
63+
64+
// Should be able to assign proper types
65+
stringArray[0] = 'hello';
66+
numberArray[0] = 42;
67+
booleanArray[0] = true;
68+
69+
expect(stringArray[0]).toBe('hello');
70+
expect(numberArray[0]).toBe(42);
71+
expect(booleanArray[0]).toBe(true);
72+
});
73+
74+
it('creates sparse arrays by default', () => {
75+
const sparseArray = arrayInit<string>(5);
76+
77+
// Array has length but no defined elements
78+
expect(sparseArray.length).toBe(5);
79+
expect(0 in sparseArray).toBe(false);
80+
expect(4 in sparseArray).toBe(false);
81+
expect(sparseArray.hasOwnProperty(0)).toBe(false);
82+
});
83+
});
84+
85+
describe('boundary conditions', () => {
86+
it('handles exactly 65536 elements', () => {
87+
const boundaryArray = arrayInit<number>(65536);
88+
expect(boundaryArray.length).toBe(65536);
89+
expect(Array.isArray(boundaryArray)).toBe(true);
90+
});
91+
92+
it('maintains array behavior across optimization boundary', () => {
93+
const smallArray = arrayInit<number>(65535);
94+
const largeArray = arrayInit<number>(65537);
95+
96+
// Both should behave the same way
97+
expect(smallArray.length).toBe(65535);
98+
expect(largeArray.length).toBe(65537);
99+
expect(Array.isArray(smallArray)).toBe(true);
100+
expect(Array.isArray(largeArray)).toBe(true);
101+
102+
// Both should be sparse
103+
expect(0 in smallArray).toBe(false);
104+
expect(0 in largeArray).toBe(false);
105+
});
106+
107+
it('handles very large arrays', () => {
108+
const veryLargeArray = arrayInit<any>(1000000);
109+
expect(veryLargeArray.length).toBe(1000000);
110+
expect(Array.isArray(veryLargeArray)).toBe(true);
111+
});
15112
});
16113
});

0 commit comments

Comments
 (0)