class TestTriePrefixExists(unittest.TestCase): def test_trie_node_prefix_exists(self): self.trie = Trie() self.trie.add_all(['ash', 'ashley']) self.assertIsInstance(self.trie, Trie, "Object should be of type `lexpy.trie.Trie`") self.assertTrue('ash' in self.trie, "Word should be in trie") self.assertTrue('ashley' in self.trie, "Word should be in trie") self.assertEqual(2, self.trie.get_word_count(), "Word count not equal") self.assertTrue(self.trie.contains_prefix('ash'), "Prefix should be present in Trie") self.assertTrue(self.trie.contains_prefix('as'), "Prefix should be present in Trie") self.assertTrue(self.trie.contains_prefix('a'), "Prefix should be present in Trie") def test_trie_node_prefix_not_exists(self): self.trie = Trie() self.trie.add_all(['ash', 'ashley']) self.assertIsInstance(self.trie, Trie, "Object should be of type `lexpy.trie.Trie`") self.assertTrue('ash' in self.trie, "Word should be in trie") self.assertTrue('ashley' in self.trie, "Word should be in trie") self.assertEqual(2, self.trie.get_word_count(), "Word count not equal") self.assertFalse(self.trie.contains_prefix('xmas'), "Prefix should be present in Trie") self.assertFalse(self.trie.contains_prefix('xor'), "Prefix should be present in Trie") self.assertFalse(self.trie.contains_prefix('sh'), "Prefix should be present in Trie")
class TestTriePrefixSearch(unittest.TestCase): def test_trie_prefix_search(self): self.trie = Trie() self.trie.add_all(['ashlame', 'ashley', 'askoiu', 'ashlo']) self.assertIsInstance(self.trie, Trie, "Object should be of type `lexpy.trie.Trie`") self.assertFalse('ash' in self.trie, "Word should not be in trie") self.assertTrue('ashley' in self.trie, "Word should be in trie") self.assertEqual(4, self.trie.get_word_count(), "Word count not equal") self.assertTrue(self.trie.contains_prefix('ash'), "Prefix should be present in Trie") self.assertEqual(sorted(self.trie.search_with_prefix('ash')), sorted(['ashlame', 'ashley', 'ashlo']), 'The lists should be equal')