-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathSubsetSum.test.js
67 lines (59 loc) · 1.72 KB
/
SubsetSum.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { subsetSum } from '../SubsetSum'
const tests = [
{
test: {
arr: [10, 5, 2, 3, 6],
sum: 8
},
expectedValue: 2
},
{
test: {
arr: [-1, -1, -1],
sum: -3
},
expectedValue: 1
},
{
test: {
arr: [40, 9, 77],
sum: 3
},
expectedValue: 0
}
]
describe('SubsetSum', () => {
test.each(tests)(
'should return $expectedValue when input is $test.arr and sum is $test.sum',
({ test, expectedValue }) => {
expect(subsetSum(test.arr, test.sum)).toBe(expectedValue)
}
)
//Empty array cases
it('should return 1 when input is an empty array and sum is 0', () => {
const result = subsetSum([], 0)
expect(result).toBe(1) // Empty subset ([]) sums to 0
})
it('should return 0 when input is an empty array and sum is not 0', () => {
const result = subsetSum([], 5)
expect(result).toBe(0) // No subsets available to sum to 5
})
// Test invalid cases for errors
describe('Invalid input cases', () => {
it('should throw a TypeError when arr is not an array', () => {
expect(() => subsetSum('invalid array', 5)).toThrow(TypeError)
})
it('should throw a TypeError when arr contains non-number elements', () => {
expect(() => subsetSum([1, 2, 'three', 4], 5)).toThrow(TypeError)
})
it('should throw a TypeError when sum is not a number', () => {
expect(() => subsetSum([1, 2, 3], 'five')).toThrow(TypeError)
})
})
// Edge case
it('should handle large arrays correctly', () => {
const largeArray = Array.from({ length: 20 }, (_, i) => i + 1) // [1, 2, ..., 20]
const result = subsetSum(largeArray, 10)
expect(result).toBeGreaterThan(0) // Ensure this works for large inputs
})
})