Extend Trie and TrieNode tests.

This commit is contained in:
Oleksii Trekhleb 2018-08-27 15:38:50 +03:00
parent d25eff49e6
commit a7ffba157c
2 changed files with 48 additions and 30 deletions

View File

@ -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', () => {

View File

@ -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);
});
});