Beispiel #1
0
    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))
Beispiel #2
0
    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))
Beispiel #3
0
class CompressedTrieTests(TestCase):
    """Tests for the CompressedTrie class"""
    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_non_zero(self):
        """Non zero should check for any data on the trie"""
        self.assertFalse(self.empty_trie)
        self.assertTrue(self.trie)

    def test_len(self):
        """Should return the number of values attached to the trie"""
        self.assertEqual(len(self.empty_trie), 0)
        self.assertEqual(len(self.trie), 9)

    def test_size(self):
        """Should return the number of nodes attached to the trie"""
        self.assertEqual(self.empty_trie.size, 1)
        self.assertEqual(self.trie.size, 10)

    def test_prefix_map(self):
        """Should map prefix to values"""
        exp1 = {"1": ["6", "2", "0", "5"], "8": ["7"], "3": [], "4": []}
        exp2 = {"1": ["6", "2", "0", "5"], "8": [], "3": ["7"], "4": []}
        self.assertTrue(self.trie.prefix_map in (exp1, exp2))

    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))

    def test_find(self):
        """Correctly founds the values present on the trie"""
        for key, value in self.data:
            self.assertTrue(value in self.trie.find(key))
        self.assertEqual(self.trie.find("cac"), [])
        self.assertEqual(self.trie.find("abababa"), ["1", "6"])
Beispiel #4
0
class CompressedTrieTests(TestCase):
    """Tests for the CompressedTrie class"""

    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_non_zero(self):
        """Non zero should check for any data on the trie"""
        self.assertFalse(self.empty_trie)
        self.assertTrue(self.trie)

    def test_len(self):
        """Should return the number of values attached to the trie"""
        self.assertEqual(len(self.empty_trie), 0)
        self.assertEqual(len(self.trie), 9)

    def test_size(self):
        """Should return the number of nodes attached to the trie"""
        self.assertEqual(self.empty_trie.size, 1)
        self.assertEqual(self.trie.size, 10)

    def test_prefix_map(self):
        """Should map prefix to values"""
        exp1 = {"1": ["6", "2", "0", "5"],
                "8": ["7"],
                "3": [],
                "4": []}
        exp2 = {"1": ["6", "2", "0", "5"],
                "8": [],
                "3": ["7"],
                "4": []}
        self.assertTrue(self.trie.prefix_map in (exp1, exp2))

    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))

    def test_find(self):
        """Correctly founds the values present on the trie"""
        for key, value in self.data:
            self.assertTrue(value in self.trie.find(key))
        self.assertEqual(self.trie.find("cac"), [])
        self.assertEqual(self.trie.find("abababa"), ["1", "6"])