|
| 1 | +const getUniqueWords = require('./index'); |
| 2 | +const Trie = require('../index'); |
| 3 | + |
| 4 | +describe('Data Structure : Trie : Get unique words', () => { |
| 5 | + it('Should returns unique words (no duplicates), sorted alphabetically', () => { |
| 6 | + const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; |
| 7 | + const trie = new Trie(); |
| 8 | + |
| 9 | + words.forEach(word => trie.insert(word)); |
| 10 | + |
| 11 | + const result = getUniqueWords(trie.root); |
| 12 | + |
| 13 | + const expected = ['apple', 'ball', 'bed', 'java', 'javascript']; |
| 14 | + expect(result).toEqual(expected); |
| 15 | + }); |
| 16 | + |
| 17 | + it('removes duplicates', () => { |
| 18 | + const words = ['bed', 'bed', 'bed']; |
| 19 | + const trie = new Trie(); |
| 20 | + |
| 21 | + words.forEach(word => trie.insert(word)); |
| 22 | + |
| 23 | + const result = getUniqueWords(trie.root); |
| 24 | + expect(result.length).toBe(1); |
| 25 | + }); |
| 26 | + |
| 27 | + it('passing an empty array of words returns an empty array', () => { |
| 28 | + const words = []; |
| 29 | + const trie = new Trie(); |
| 30 | + |
| 31 | + words.forEach(word => trie.insert(word)); |
| 32 | + |
| 33 | + const result = getUniqueWords(trie.root); |
| 34 | + expect(result).toEqual([]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('passing an empty Trie will throw an error ', () => { |
| 38 | + const trie = new Trie(); |
| 39 | + expect(() => { |
| 40 | + getUniqueWords(trie); |
| 41 | + }).toThrow('Invalid argument: Root of Trie is required'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('passing an empty array will throw an error ', () => { |
| 45 | + expect(() => { |
| 46 | + getUniqueWords([]); |
| 47 | + }).toThrow('Invalid argument: Root of Trie is required'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('passing null will throw an error ', () => { |
| 51 | + expect(() => { |
| 52 | + getUniqueWords([]); |
| 53 | + }).toThrow('Invalid argument: Root of Trie is required'); |
| 54 | + }); |
| 55 | + |
| 56 | + |
| 57 | + it('passing an array not in a Trie will throw an error ', () => { |
| 58 | + expect(() => { |
| 59 | + getUniqueWords(['bed', 'ball', 'apple']); |
| 60 | + }).toThrow('Invalid argument: Root of Trie is required'); |
| 61 | + }); |
| 62 | +}); |
0 commit comments