def test_get_good_key(self):
        b = Bucket()
        b._vals = [('a', 12), ('x', 'asdf'), (4, False), (-1, True)]

        self.assertEqual(b['a'], 12)
        self.assertEqual(b[-1], True)
        self.assertEqual(b[4], False)
        self.assertEqual(b['x'], 'asdf')
    def test_get_good_key(self):
        b = Bucket()
        b._vals = [('a', 12), ('x', 'asdf'), (4, False), (-1, True)]

        self.assertEqual(b['a'], 12)
        self.assertEqual(b[-1], True)
        self.assertEqual(b[4], False)
        self.assertEqual(b['x'], 'asdf')
 def test_set_overwrite(self):
     b = Bucket()
     b._vals = [('z', 4), (4, 38), (12, '17')]
     b[4] = 'z'
     self.assertEqual(b._vals, [('z', 4), (4, 'z'), (12, '17')])
     b['z'] = 1009
     self.assertEqual(b._vals, [('z', 1009), (4, 'z'), (12, '17')])
     b[12] = False
     self.assertEqual(b._vals, [('z', 1009), (4, 'z'), (12, False)])
 def test_set_overwrite(self):
     b = Bucket()
     b._vals = [('z', 4), (4, 38), (12, '17')]
     b[4] = 'z'
     self.assertEqual(b._vals, [('z', 4), (4, 'z'), (12, '17')])
     b['z'] = 1009
     self.assertEqual(b._vals, [('z', 1009), (4, 'z'), (12, '17')])
     b[12] = False
     self.assertEqual(b._vals, [('z', 1009), (4, 'z'), (12, False)])
 def test_del(self):
     b = Bucket()
     b._vals = [('z', 4), (4, 38), (12, '17'), (18, 2)]
     del b[4]
     self.assertEqual(b._vals, [('z', 4), (12, '17'), (18, 2)])
     del b[18]
     self.assertEqual(b._vals, [('z', 4), (12, '17')])
     del b['z']
     self.assertEqual(b._vals, [(12, '17')])
     del b[12]
     self.assertEqual(b._vals, [])
 def test_del(self):
     b = Bucket()
     b._vals = [('z', 4), (4, 38), (12, '17'), (18, 2)]
     del b[4]
     self.assertEqual(b._vals, [('z', 4), (12, '17'), (18, 2)])
     del b[18]
     self.assertEqual(b._vals, [('z', 4), (12, '17')])
     del b['z']
     self.assertEqual(b._vals, [(12, '17')])
     del b[12]
     self.assertEqual(b._vals, [])
    def test_store_stuff(self):
        b = Bucket()
        b['a'] = 4
        b['9'] = 7
        b[12] = None
        b[3] = 'asdf'

        self.assertEqual(b['a'], 4)
        self.assertEqual(b['9'], 7)
        self.assertEqual(b[3], 'asdf')
        self.assertEqual(b[12], None)

        thrown = False
        try:
            b['f']
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)

        b['f'] = 123092
        self.assertEqual(b['f'], 123092)

        del b[12]
        thrown = False
        try:
            b[12]
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)

        for _ in range(100):
            key, val = self.rand_string(), random.randint(-7777, 77777)
            b[key] = val
            self.assertEqual(b[key], val)
    def test_get_bad_key(self):
        b = Bucket()

        thrown = False
        try:
            b['']
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)

        b._vals = [('b', 1)]
        thrown = False
        try:
            b['a']
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)
    def test_get_bad_key(self):
        b = Bucket()

        thrown = False
        try:
            b['']
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)

        b._vals = [('b', 1)]
        thrown = False
        try:
            b['a']
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)
    def test_del_non_exist(self):
        b = Bucket()
        b._vals = [('z', 4), (4, 38), (12, '17'), (18, 2)]

        thrown = False
        try:
            b['f']
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)

        b._vals = []
        thrown = False
        try:
            b[4]
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)
 def test_set_new(self):
     b = Bucket()
     self.assertEqual(b._vals, [])
     b['z'] = 4
     self.assertEqual(b._vals, [('z', 4)])
     b[4] = 38
     self.assertEqual(b._vals, [('z', 4), (4, 38)])
     b[12] = '17'
     self.assertEqual(b._vals, [('z', 4), (4, 38), (12, '17')])
    def test_del_non_exist(self):
        b = Bucket()
        b._vals = [('z', 4), (4, 38), (12, '17'), (18, 2)]

        thrown = False
        try:
            b['f']
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)

        b._vals = []
        thrown = False
        try:
            b[4]
        except KeyError as e:
            thrown = True
        self.assertTrue(thrown)
def test_collision_at_index_fourty():
    ht = HashTable()
    ht._hash_table[40] = Bucket()
    node_one = Node('apple', 43)
    node_two = Node('mango', 21)
    ht._hash_table[40].add(node_one)
    ht._hash_table[40].add(node_two)

    assert ht._hash_table[40].head.key == 'apple'  # first node
    assert ht._hash_table[40].head.next.key == 'mango'  # second node added
    assert ht._hash_table[40].head.next.next == None  # no other nodes added
def test_value_at_index_fourty():
    ht = HashTable()
    ht._hash_table[40] = Bucket()

    node_one = Node('apple', 43)
    node_two = Node('mango', 21)
    node_three = Node('pear', 65)
    node_four = Node('tomato', 'potato')

    ht._hash_table[40].add(node_one)
    ht._hash_table[40].add(node_two)
    ht._hash_table[40].add(node_three)
    ht._hash_table[40].add(node_four)

    assert ht._hash_table[40].return_value('apple') == 43
    assert ht._hash_table[40].return_value('mango') == 21
    assert ht._hash_table[40].return_value('tomato') == 'potato'
    assert ht._hash_table[40].return_value('pear') == 65
    assert ht._hash_table[40].return_value('banana') == None
Exemplo n.º 15
0
def test_bucket():
    """Should add stuff & give it back based on keys"""
    bucket = Bucket()
    bucket.add("key1", "value1")
    assert bucket.retrieve("key1") == "value1"
    bucket.add("key2", "value2")
    assert bucket.retrieve("key1") == "value1"
    assert bucket.retrieve("key2") == "value2"
    # Resets stuff, too
    bucket.add("key1", "value3")
    assert bucket.retrieve("key1") == "value3"