From a7ffba157c54d9db205973e268ff34ca628f2de6 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 27 Aug 2018 15:38:50 +0300 Subject: [PATCH] Extend Trie and TrieNode tests. --- .../trie/__test__/Trie.test.js | 20 +++++++ .../trie/__test__/TrieNode.test.js | 58 +++++++++---------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/data-structures/trie/__test__/Trie.test.js b/src/data-structures/trie/__test__/Trie.test.js index cb4b5515..92b32f41 100644 --- a/src/data-structures/trie/__test__/Trie.test.js +++ b/src/data-structures/trie/__test__/Trie.test.js @@ -28,11 +28,31 @@ describe('Trie', () => { trie.addWord('carpet'); trie.addWord('car'); + trie.addWord('cat'); + trie.addWord('cart'); expect(trie.doesWordExist('carpet')).toBe(true); + expect(trie.doesWordExist('car')).toBe(true); + expect(trie.doesWordExist('cart')).toBe(true); + expect(trie.doesWordExist('cat')).toBe(true); trie.deleteWord('carpet'); expect(trie.doesWordExist('carpet')).toEqual(false); expect(trie.doesWordExist('car')).toEqual(true); + expect(trie.doesWordExist('cart')).toBe(true); + expect(trie.doesWordExist('cat')).toBe(true); + + trie.deleteWord('cat'); + expect(trie.doesWordExist('car')).toEqual(true); + expect(trie.doesWordExist('cart')).toBe(true); + expect(trie.doesWordExist('cat')).toBe(false); + + trie.deleteWord('car'); + expect(trie.doesWordExist('car')).toEqual(false); + expect(trie.doesWordExist('cart')).toBe(true); + + trie.deleteWord('cart'); + expect(trie.doesWordExist('car')).toEqual(false); + expect(trie.doesWordExist('cart')).toBe(false); }); it('should suggests next characters', () => { diff --git a/src/data-structures/trie/__test__/TrieNode.test.js b/src/data-structures/trie/__test__/TrieNode.test.js index dade02ae..ddcd47d2 100644 --- a/src/data-structures/trie/__test__/TrieNode.test.js +++ b/src/data-structures/trie/__test__/TrieNode.test.js @@ -18,36 +18,6 @@ describe('TrieNode', () => { expect(trieNode.toString()).toBe('c:a,o'); }); - describe('removing child nodes', () => { - it('should delete child node if the child node has NO children', () => { - const trieNode = new TrieNode('c'); - trieNode.addChild('a'); - expect(trieNode.hasChild('a')).toBe(true); - - trieNode.removeChild('a'); - expect(trieNode.hasChild('a')).toBe(false); - }); - - it('should NOT delete child node if the child node has children', () => { - const trieNode = new TrieNode('c'); - trieNode.addChild('a'); - const childNode = trieNode.getChild('a'); - childNode.addChild('r'); - - trieNode.removeChild('a'); - expect(trieNode.hasChild('a')).toEqual(true); - }); - - it('should NOT delete child node if the child node completes a word', () => { - const trieNode = new TrieNode('c'); - const IS_COMPLETE_WORD = true; - trieNode.addChild('a', IS_COMPLETE_WORD); - - trieNode.removeChild('a'); - expect(trieNode.hasChild('a')).toEqual(true); - }); - }); - it('should get child nodes', () => { const trieNode = new TrieNode('c'); @@ -79,4 +49,32 @@ describe('TrieNode', () => { expect(trieNode.suggestChildren()).toEqual(['a', 'o']); }); + + it('should delete child node if the child node has NO children', () => { + const trieNode = new TrieNode('c'); + trieNode.addChild('a'); + expect(trieNode.hasChild('a')).toBe(true); + + trieNode.removeChild('a'); + expect(trieNode.hasChild('a')).toBe(false); + }); + + it('should NOT delete child node if the child node has children', () => { + const trieNode = new TrieNode('c'); + trieNode.addChild('a'); + const childNode = trieNode.getChild('a'); + childNode.addChild('r'); + + trieNode.removeChild('a'); + expect(trieNode.hasChild('a')).toEqual(true); + }); + + it('should NOT delete child node if the child node completes a word', () => { + const trieNode = new TrieNode('c'); + const IS_COMPLETE_WORD = true; + trieNode.addChild('a', IS_COMPLETE_WORD); + + trieNode.removeChild('a'); + expect(trieNode.hasChild('a')).toEqual(true); + }); });