|
| 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