Skip to content

Commit c57b51d

Browse files
authored
Merge pull request knaxus#25 from marcelinol/add-test-for-next-greater-element
Add test for next greater element
2 parents 54cb23e + 19f2963 commit c57b51d

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/_Problems_/next-greater-element/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
To keep it simple, the next greater element for the last or maximum value in the array is -1.
55
66
Input: [4, 6, 3, 2, 8, 1]
7-
Output: {6, 8, 8, 8, -1, -1}
7+
Output: [6, 8, 8, 8, -1, -1]
88
*/
99

1010
const Stack = require('../../_DataStructures_/Stack');
@@ -42,5 +42,4 @@ function nextGreaterElement(arr) {
4242
return nextGreater;
4343
}
4444

45-
// eslint-disable-next-line no-console
46-
console.log(nextGreaterElement([4, 6, 3, 2, 8, 1]));
45+
module.exports = { nextGreaterElement };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { nextGreaterElement } = require('.');
2+
3+
describe('Next greater element', () => {
4+
it('returns next greater elements collection', () => {
5+
const input = [4, 6, 3, 2, 8, 1]
6+
const greaterElements = [6, 8, 8, 8, -1, -1]
7+
8+
expect(nextGreaterElement(input)).toEqual(greaterElements);
9+
});
10+
11+
it('returns and empty collection for an empty array', () => {
12+
expect(nextGreaterElement([])).toEqual([]);
13+
});
14+
15+
it('returns an array with -1 if the input has only one element', () => {
16+
expect(nextGreaterElement([0])).toEqual([-1]);
17+
});
18+
19+
it('returns a collection of -1 if there is no greater element', () => {
20+
const input = [90, 40, 15, 7, -1, -10]
21+
const greaterElements = [-1, -1, -1, -1, -1, -1]
22+
23+
expect(nextGreaterElement(input)).toEqual(greaterElements);
24+
});
25+
26+
it('uses -1 if the numbers are the same', () => {
27+
const input = [90, 90]
28+
const greaterElements = [-1, -1]
29+
30+
expect(nextGreaterElement(input)).toEqual(greaterElements);
31+
});
32+
33+
it('throws an error if the input is not an array', () => {
34+
expect(() => {
35+
nextGreaterElement('xunda')
36+
}).toThrowError('Invalid Argument');
37+
});
38+
});

0 commit comments

Comments
 (0)