Skip to content

Commit 1c828f2

Browse files
committed
Add O(n) maximum subarray test
1 parent 53256ef commit 1c828f2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var maxSubArray = require('../../src/searching/maximum-subarray').maxSubarray;
2+
3+
describe('Maximum subarray', function() {
4+
'use strict';
5+
6+
it('should work with empty arrays', function() {
7+
expect(maxSubArray([])).toBeUndefined();
8+
});
9+
10+
it('should return the only element when an array with single element is passed', function() {
11+
expect(maxSubArray([42])).toBe(42);
12+
});
13+
14+
it('should return the only negative element when an array with single element is passed', function() {
15+
expect(maxSubArray([-42])).toBe(-42);
16+
});
17+
18+
it('should return the zero when an array with single element, which is zero is passed', function() {
19+
expect(maxSubArray([0])).toBe(0);
20+
});
21+
22+
it('should return the max sum of a subarray', function() {
23+
expect(maxSubArray([1, -1, 2, 3, -1])).toBe(5);
24+
});
25+
26+
it('should return the max negative number when array with negative numbers is provided', function() {
27+
expect(maxSubArray([-10, -1, -2, -3, -1])).toBe(-1);
28+
});
29+
});

0 commit comments

Comments
 (0)