|
| 1 | +const { ternarySearch, ternarySearchRecursive } = require('.'); |
| 2 | + |
| 3 | +describe('Ternary Search', () => { |
| 4 | + const array = [1, 2, 3, 4, 5, 6, 7, 8]; |
| 5 | + const low = 0; |
| 6 | + const high = array.length - 1; |
| 7 | + |
| 8 | + describe('When element to find is at 1st position ', () => { |
| 9 | + it('Ternary search with Loop', () => { |
| 10 | + expect(ternarySearch(array, 1)).toEqual(0); |
| 11 | + }); |
| 12 | + it('Ternary serach with recursion', () => { |
| 13 | + expect(ternarySearchRecursive(array, low, high, 1)).toEqual(0); |
| 14 | + }); |
| 15 | + }); |
| 16 | + describe('When element to find is at last position ', () => { |
| 17 | + it('Ternary search with Loop', () => { |
| 18 | + expect(ternarySearch(array, 8)).toEqual(7); |
| 19 | + }); |
| 20 | + it('Ternary serach with recursion', () => { |
| 21 | + expect(ternarySearchRecursive(array, low, high, 8)).toEqual(7); |
| 22 | + }); |
| 23 | + }); |
| 24 | + describe('When element to find is at random position ', () => { |
| 25 | + it('Ternary search with Loop', () => { |
| 26 | + expect(ternarySearch(array, 3)).toEqual(2); |
| 27 | + }); |
| 28 | + it('Ternary serach with recursion', () => { |
| 29 | + expect(ternarySearchRecursive(array, low, high, 4)).toEqual(3); |
| 30 | + }); |
| 31 | + }); |
| 32 | + describe('When element to find is no present in array ', () => { |
| 33 | + it('Ternary search with Loop', () => { |
| 34 | + expect(ternarySearch(array, 10)).toEqual(null); |
| 35 | + }); |
| 36 | + it('Ternary serach with recursion', () => { |
| 37 | + expect(ternarySearchRecursive(array, low, high, 10)).toEqual(null); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | +}); |
0 commit comments