def setUp(self): """Set up test data for use in compressed trie unit tests""" self.data = [("ab", "0"), ("abababa", "1"), ("abab", "2"), ("baba", "3"), ("ababaa", "4"), ("a", "5"), ("abababa", "6"), ("bab", "7"), ("babba", "8")] self.empty_trie = CompressedTrie() self.trie = CompressedTrie(self.data)
def test_init(self): """Trie init should construct the right structure""" # In no pair_list is provided, it should create an empty Trie t = CompressedTrie() self.assertEqual(t._root.key, "") self.assertEqual(t._root.values, []) self.assertEqual(t._root.children, {}) # If a pair_list is provided, it should insert all the data t = CompressedTrie(self.data) self.assertEqual(t._root.key, "") self.assertEqual(t._root.values, []) self.assertEqual(set(t._root.children.keys()), set(["a", "b"]))
def test_insert(self): """Correctly inserts a new key into the trie""" t = CompressedTrie(self.data) t.insert("babc", "9") self.assertTrue("9" in t.find("babc")) exp1 = { "1": ["6", "2", "0", "5"], "9": ["7"], "3": [], "4": [], "8": [] } exp2 = { "1": ["6", "2", "0", "5"], "9": [], "3": ["7"], "4": [], "8": [] } exp3 = { "1": ["6", "2", "0", "5"], "9": [], "3": [], "4": [], "8": ["7"] } self.assertTrue(t.prefix_map in (exp1, exp2, exp3))