Пример #1
0
    def test_hash(self):
        from hashtable import HashTable, HashNode, word_frequency

        table = HashTable(capacity=16)

        table.table = [
            None, None, None,
            HashNode('class_ever', 1),
            HashNode(None, None, True),
            HashNode(None, None, True), None, None, None, None,
            HashNode(None, None, True), None, None, None,
            HashNode('cse331', 100), None
        ]

        # Should insert in the first available bin
        assert (4 == table.hash("is_the", inserting=True))

        # Should search until the first None/unused bin
        assert (15 == table.hash("is_the"))

        # Should insert in the first available bin
        assert (5 == table.hash("yash", inserting=True))

        # Should search until the first None/unused bin
        assert (7 == table.hash("yash"))

        assert (3 == table.hash("class_ever"))
Пример #2
0
    def test_get(self):
        from hashtable import HashTable, HashNode, word_frequency

        table = HashTable()

        solution = [
            None, None, None,
            HashNode('class_ever', 1),
            HashNode('is_the', 3005), None, None, None, None, None,
            HashNode('best', 42), None, None, None,
            HashNode('cse331', 100), None
        ]

        table.insert("cse331", 100)
        table.insert("is_the", 3005)

        assert (table.size == 2)
        assert (table.capacity == 8)

        table['best'] = 42
        table['class_ever'] = 1

        assert (table.size == 4)
        assert (table.capacity == 16)
        print(table)
        assert (solution == table.table)

        for i in solution:
            if i:
                assert (table[i.key] == i)
Пример #3
0
    def test_getitem(self):
        table = HashTable()

        solution = [
            None, None, None,
            HashNode('class_ever', 1),
            HashNode('is_the', 3005), None, None, None, None, None,
            HashNode('best', 42), None, None, None,
            HashNode('cse331', 100), None
        ]

        table["cse331"] = 100
        table["is_the"] = 3005

        assert (table.size == 2)
        assert (table.capacity == 8)

        table['best'] = 42
        table['class_ever'] = 1
        assert (table.size == 4)
        assert (table.capacity == 16)
        print(table)
        assert (solution == table.table)

        for i in solution:
            if i:
                assert (table[i.key] == i.value)
Пример #4
0
    def test_1(self):
        from hashtable import HashTable, HashNode, word_frequency

        string = "chefallfall"
        dictionary = ['chef', 'all', 'fall', 'a']

        table = HashTable()
        table = word_frequency(string, dictionary, table)
        solution = [
            None,
            HashNode('all', 1), None, None, None, None, None, None,
            HashNode('a', 0), None, None, None, None, None,
            HashNode('chef', 1),
            HashNode('fall', 1)
        ]

        print(solution)
        assert (solution == table.table)
        print(table)
Пример #5
0
    def test_all(self):
        from hashtable import HashTable, HashNode, word_frequency

        table = HashTable()

        pre_solution = [
            None, None, None,
            HashNode('class_ever', 1),
            HashNode('is_the', 3005), None, None, None, None, None,
            HashNode('best', 42), None, None, None,
            HashNode('cse331', 100), None
        ]

        post_solution = [
            None, None, None,
            HashNode('class_ever', 1),
            HashNode(None, None), None, None, None, None, None,
            HashNode(None, None), None, None, None,
            HashNode('cse331', 100), None
        ]

        table.insert("cse331", 100)
        table.insert("is_the", 3005)

        assert (table.size == 2)
        assert (table.capacity == 8)

        table['best'] = 42
        table['class_ever'] = 1

        assert (table.size == 4)
        assert (table.capacity == 16)
        print(table)

        assert (pre_solution == table.table)

        delete = ['best', 'is_the']
        for k in delete:
            table.delete(k)

        assert (post_solution == table.table)
        print(table)

        assert (table['is_the'] == None)
        assert (table['best'] == None)
Пример #6
0
    def test_all(self):
        table = HashTable()

        pre_solution = [
            None, None, None,
            HashNode('class_ever', 1),
            HashNode('is_the', 3005), None, None, None, None, None,
            HashNode('best', 42), None, None, None,
            HashNode('cse331', 100), None
        ]

        post_solution = [
            None, None, None,
            HashNode('class_ever', 1),
            HashNode(None, None), None, None, None, None, None,
            HashNode(None, None), None, None, None,
            HashNode('cse331', 100), None
        ]

        table['cse331'] = 100
        table['is_the'] = 3005

        assert (table.size == 2)
        assert (table.capacity == 8)

        table['best'] = 42
        table['class_ever'] = 1

        assert (table.size == 4)
        assert (table.capacity == 16)
        print(table)

        assert (pre_solution == table.table)

        delete = ['best', 'is_the']
        for k in delete:
            del table[k]

        assert (post_solution == table.table)
        print(table)

        with self.assertRaises(KeyError):
            print(table['best'])
Пример #7
0
from hashtable import HashNode, HashTable

n = HashNode("k", "v")
assert (n is not None)
assert (hasattr(n, "key"))
assert (hasattr(n, "value"))
assert (hasattr(n, "next"))
assert (str(n) == "(k,v)")

t = HashTable(4)
assert (t is not None)
assert (hasattr(t, "nodes"))
assert (hasattr(t, "get"))
assert (hasattr(t, "set"))

t.set("Good", "1")
unempty_nodes = [node for node in t.nodes if node is not None]
assert (len(unempty_nodes) == 1)
assert (str(unempty_nodes[0]) == "(Good,1)")
assert (str(t) == "(Good,1)")

t.set("Bad", "3")
t_str = str(t)
assert ("(Good,1)" in t_str)
assert ("(Bad,3)" in t_str)
t_str = t_str.replace("(Good,1)", "")
t_str = t_str.replace("(Bad,3)", "")
assert (t_str == "")

t.set("Normal", "2")
t_str = str(t)