Skip to content

Commit d92cea2

Browse files
test coverage update
1 parent 3450cb4 commit d92cea2

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

tests/index.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,53 @@ describe('array-compare', () => {
1414
it('should return false for arrays of different lengths', () => {
1515
expect(areEqual([1, 2], [1, 2, 3])).toBe(false);
1616
});
17+
18+
it('should handle same object reference', () => {
19+
const arr = [1, 2, 3];
20+
expect(areEqual(arr, arr)).toBe(true);
21+
});
22+
23+
it('should handle both null/undefined arrays', () => {
24+
expect(areEqual(null as any, null as any)).toBe(true);
25+
expect(areEqual(undefined as any, undefined as any)).toBe(true);
26+
});
27+
28+
it('should handle one null/undefined array', () => {
29+
expect(areEqual([1, 2], null as any)).toBe(false);
30+
expect(areEqual(null as any, [1, 2])).toBe(false);
31+
expect(areEqual([1, 2], undefined as any)).toBe(false);
32+
});
33+
34+
it('should handle empty arrays', () => {
35+
expect(areEqual([], [])).toBe(true);
36+
});
37+
38+
it('should handle arrays with undefined elements', () => {
39+
const sparseArray1 = new Array(3);
40+
sparseArray1[0] = 1;
41+
sparseArray1[2] = 3;
42+
// sparseArray1[1] is undefined
43+
44+
const sparseArray2 = new Array(3);
45+
sparseArray2[0] = 1;
46+
sparseArray2[2] = 3;
47+
// sparseArray2[1] is undefined
48+
49+
expect(areEqual(sparseArray1, sparseArray2)).toBe(true);
50+
51+
const sparseArray3 = new Array(3);
52+
sparseArray3[0] = 1;
53+
sparseArray3[1] = 2;
54+
sparseArray3[2] = 3;
55+
56+
expect(areEqual(sparseArray1, sparseArray3)).toBe(false);
57+
});
58+
59+
it('should use custom equality comparison', () => {
60+
const customEqual = (a: number, b: number) => Math.abs(a - b) <= 1;
61+
expect(areEqual([1, 2, 3], [2, 3, 4], customEqual)).toBe(true);
62+
expect(areEqual([1, 2, 3], [1, 2, 5], customEqual)).toBe(false);
63+
});
1764
});
1865

1966
describe('areAllEqual', () => {
@@ -24,6 +71,25 @@ describe('array-compare', () => {
2471
it('should return false for different arrays', () => {
2572
expect(areAllEqual([[1, 2], [1, 3], [1, 2]])).toBe(false);
2673
});
74+
75+
it('should throw for null arrays parameter', () => {
76+
expect(() => areAllEqual(null as any)).toThrow('arrays');
77+
});
78+
79+
it('should throw for arrays with less than 2 elements', () => {
80+
expect(() => areAllEqual([])).toThrow('Cannot compare a set of arrays less than 2.');
81+
expect(() => areAllEqual([[1, 2]])).toThrow('Cannot compare a set of arrays less than 2.');
82+
});
83+
84+
it('should return false for undefined arrays', () => {
85+
expect(areAllEqual([undefined as any, [1, 2]])).toBe(false);
86+
expect(areAllEqual([[1, 2], undefined as any])).toBe(false);
87+
});
88+
89+
it('should use custom equality comparison', () => {
90+
const customEqual = (a: number, b: number) => Math.abs(a - b) <= 1;
91+
expect(areAllEqual([[1, 2], [2, 3], [1, 2]], customEqual)).toBe(true);
92+
});
2793
});
2894

2995
describe('areEquivalent', () => {
@@ -34,5 +100,68 @@ describe('array-compare', () => {
34100
it('should return false for arrays with different elements', () => {
35101
expect(areEquivalent([1, 2, 3], [1, 2, 4])).toBe(false);
36102
});
103+
104+
it('should handle same object reference', () => {
105+
const arr = [3, 1, 2];
106+
expect(areEquivalent(arr, arr)).toBe(true);
107+
});
108+
109+
it('should handle both null/undefined arrays', () => {
110+
expect(areEquivalent(null as any, null as any)).toBe(true);
111+
expect(areEquivalent(undefined as any, undefined as any)).toBe(true);
112+
});
113+
114+
it('should handle one null/undefined array', () => {
115+
expect(areEquivalent([1, 2], null as any)).toBe(false);
116+
expect(areEquivalent(null as any, [1, 2])).toBe(false);
117+
});
118+
119+
it('should handle empty arrays', () => {
120+
expect(areEquivalent([], [])).toBe(true);
121+
});
122+
123+
it('should handle single element arrays', () => {
124+
expect(areEquivalent([1], [1])).toBe(true);
125+
expect(areEquivalent([1], [2])).toBe(false);
126+
});
127+
128+
it('should use custom comparison function', () => {
129+
const reverseCompare = (a: number, b: number) => b - a;
130+
expect(areEquivalent([1, 2, 3], [3, 2, 1], reverseCompare)).toBe(true);
131+
});
132+
133+
it('should handle large arrays efficiently', () => {
134+
const largeArray1 = Array.from({ length: 70000 }, (_, i) => i);
135+
const largeArray2 = Array.from({ length: 70000 }, (_, i) => 69999 - i);
136+
expect(areEquivalent(largeArray1, largeArray2)).toBe(true);
137+
});
138+
139+
it('should throw for arrays with undefined elements in sorting', () => {
140+
const sparseArray1 = new Array(3);
141+
sparseArray1[0] = 1;
142+
// sparseArray1[1] is undefined
143+
sparseArray1[2] = 3;
144+
145+
const sparseArray2 = new Array(3);
146+
sparseArray2[0] = 1;
147+
sparseArray2[1] = 2;
148+
sparseArray2[2] = 3;
149+
150+
// Both cases should throw because internalSort checks for undefined
151+
expect(() => areEquivalent(sparseArray1, sparseArray2)).toThrow('Array element at index 1 is undefined');
152+
expect(() => areEquivalent(sparseArray2, sparseArray1)).toThrow('Array element at index 1 is undefined');
153+
154+
// Test with different undefined positions
155+
const sparseArray3 = new Array(2);
156+
sparseArray3[0] = 1;
157+
// sparseArray3[1] is undefined
158+
159+
const sparseArray4 = new Array(2);
160+
// sparseArray4[0] is undefined
161+
sparseArray4[1] = 2;
162+
163+
expect(() => areEquivalent(sparseArray3, sparseArray4)).toThrow('Array element at index');
164+
expect(() => areEquivalent(sparseArray4, sparseArray3)).toThrow('Array element at index');
165+
});
37166
});
38167
});

0 commit comments

Comments
 (0)