Skip to content

Add test for next greater element #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/_Problems_/next-greater-element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
To keep it simple, the next greater element for the last or maximum value in the array is -1.
Input: [4, 6, 3, 2, 8, 1]
Output: {6, 8, 8, 8, -1, -1}
Output: [6, 8, 8, 8, -1, -1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 😄

*/

const Stack = require('../../_DataStructures_/Stack');
Expand Down Expand Up @@ -42,5 +42,4 @@ function nextGreaterElement(arr) {
return nextGreater;
}

// eslint-disable-next-line no-console
console.log(nextGreaterElement([4, 6, 3, 2, 8, 1]));
module.exports = { nextGreaterElement };
38 changes: 38 additions & 0 deletions src/_Problems_/next-greater-element/next-greater-element.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { nextGreaterElement } = require('.');

describe('Next greater element', () => {
it('returns next greater elements collection', () => {
const input = [4, 6, 3, 2, 8, 1]
const greaterElements = [6, 8, 8, 8, -1, -1]

expect(nextGreaterElement(input)).toEqual(greaterElements);
});

it('returns and empty collection for an empty array', () => {
expect(nextGreaterElement([])).toEqual([]);
});

it('returns an array with -1 if the input has only one element', () => {
expect(nextGreaterElement([0])).toEqual([-1]);
});

it('returns a collection of -1 if there is no greater element', () => {
const input = [90, 40, 15, 7, -1, -10]
const greaterElements = [-1, -1, -1, -1, -1, -1]

expect(nextGreaterElement(input)).toEqual(greaterElements);
});

it('uses -1 if the numbers are the same', () => {
const input = [90, 90]
const greaterElements = [-1, -1]

expect(nextGreaterElement(input)).toEqual(greaterElements);
});

it('throws an error if the input is not an array', () => {
expect(() => {
nextGreaterElement('xunda')
}).toThrowError('Invalid Argument');
});
});