コード例 #1
0
ファイル: test_hash.py プロジェクト: kylepw/hashtable
 def setUp(self):
     self.ht = Hashtable()
     self.keys = tuple(k for k in range(1000))
     self.values = tuple(v for v in range(1000, 0, -1))
     data = list(zip(self.keys, self.values))
     for k, v in data:
         self.ht.set(k, v)
コード例 #2
0
ファイル: test_hash.py プロジェクト: kylepw/hashtable
    def test_get(self):
        self.assertIsNone(self.ht.get('ninjaturtles343'))
        self.assertEqual(self.ht.get('geee'), 'bahhh')
        self.assertEqual(self.ht.get('monkey'), 565.98)
        self.assertEqual(self.ht.get('iphone'), 'I do not know.')

        values = self.ht.get('geee', 'monkey', 'iphone')
        self.assertEqual(len(values), 3)
        self.assertEqual(values, ('bahhh', 565.98, 'I do not know.'))

        a_wrong_value = self.ht.get('geee', 'shit3433', 'iphone')
        self.assertEqual(len(a_wrong_value), 2)
        self.assertEqual(a_wrong_value, ('bahhh', 'I do not know.'))

        t = Hashtable()
        self.assertIsNone(t.get('hey'))
コード例 #3
0
ファイル: test_hash.py プロジェクト: kylepw/hashtable
    def test_keys(self):
        keys = self.ht.keys()
        self.assertEqual(keys, ('key', 'geee', 'mah', 'veloce', 'man',
                                'abacus', 'iphone', 'monkey'))
        self.assertEqual(len(keys), 8)

        self.assertEqual(Hashtable().keys(), ())
コード例 #4
0
ファイル: test_hash.py プロジェクト: kylepw/hashtable
    def setUp(self):
        self.ht = Hashtable()

        lst1 = LinkedList(head=Node('key', 'value'))
        lst1.head.next = Node('geee', 'bahhh')
        lst1.head.next.next = Node('mah', 'yo')
        lst1.head.next.next.next = Node('veloce', 123)
        self.ht._buckets[0] = lst1
        self.ht._hash_map.update([(to_hash(key), 0)
                                  for key in ('key', 'geee', 'mah', 'veloce')])

        lst2 = LinkedList(head=Node('man', {1, 2, 3}))
        lst2.head.next = Node('abacus', 'YES')
        lst2.head.next.next = Node('iphone', 'I do not know.')
        lst2.head.next.next.next = Node('monkey', 565.98)
        self.ht._buckets[1] = lst2
        self.ht._hash_map.update([
            (to_hash(key), 1) for key in ('man', 'abacus', 'iphone', 'monkey')
        ])
コード例 #5
0
ファイル: test_hash.py プロジェクト: kylepw/hashtable
    def test_set(self):
        self.assertIsNone(self.ht.set())

        self.assertEqual(self.ht.get('geee'), 'bahhh')
        self.ht.set('geee', 'wiz!')
        self.assertEqual(self.ht.get('geee'), 'wiz!')

        self.assertEqual(self.ht.get('monkey'), 565.98)
        self.ht.set('monkey', 'mammal')
        self.assertEqual(self.ht.get('monkey'), 'mammal')

        t = Hashtable()
        self.assertIsNone(t.get('hey'))
        t.set('hey', 'you')
        self.assertIsNotNone(t.get('hey'))
コード例 #6
0
ファイル: test_hash.py プロジェクト: kylepw/hashtable
class TestHashTable(unittest.TestCase):
    def setUp(self):
        self.ht = Hashtable()

        lst1 = LinkedList(head=Node('key', 'value'))
        lst1.head.next = Node('geee', 'bahhh')
        lst1.head.next.next = Node('mah', 'yo')
        lst1.head.next.next.next = Node('veloce', 123)
        self.ht._buckets[0] = lst1
        self.ht._hash_map.update([(to_hash(key), 0)
                                  for key in ('key', 'geee', 'mah', 'veloce')])

        lst2 = LinkedList(head=Node('man', {1, 2, 3}))
        lst2.head.next = Node('abacus', 'YES')
        lst2.head.next.next = Node('iphone', 'I do not know.')
        lst2.head.next.next.next = Node('monkey', 565.98)
        self.ht._buckets[1] = lst2
        self.ht._hash_map.update([
            (to_hash(key), 1) for key in ('man', 'abacus', 'iphone', 'monkey')
        ])

    def test_set(self):
        self.assertIsNone(self.ht.set())

        self.assertEqual(self.ht.get('geee'), 'bahhh')
        self.ht.set('geee', 'wiz!')
        self.assertEqual(self.ht.get('geee'), 'wiz!')

        self.assertEqual(self.ht.get('monkey'), 565.98)
        self.ht.set('monkey', 'mammal')
        self.assertEqual(self.ht.get('monkey'), 'mammal')

        t = Hashtable()
        self.assertIsNone(t.get('hey'))
        t.set('hey', 'you')
        self.assertIsNotNone(t.get('hey'))

    def test_get(self):
        self.assertIsNone(self.ht.get('ninjaturtles343'))
        self.assertEqual(self.ht.get('geee'), 'bahhh')
        self.assertEqual(self.ht.get('monkey'), 565.98)
        self.assertEqual(self.ht.get('iphone'), 'I do not know.')

        values = self.ht.get('geee', 'monkey', 'iphone')
        self.assertEqual(len(values), 3)
        self.assertEqual(values, ('bahhh', 565.98, 'I do not know.'))

        a_wrong_value = self.ht.get('geee', 'shit3433', 'iphone')
        self.assertEqual(len(a_wrong_value), 2)
        self.assertEqual(a_wrong_value, ('bahhh', 'I do not know.'))

        t = Hashtable()
        self.assertIsNone(t.get('hey'))

    def test_keys(self):
        keys = self.ht.keys()
        self.assertEqual(keys, ('key', 'geee', 'mah', 'veloce', 'man',
                                'abacus', 'iphone', 'monkey'))
        self.assertEqual(len(keys), 8)

        self.assertEqual(Hashtable().keys(), ())
コード例 #7
0
ファイル: test_hash.py プロジェクト: kylepw/hashtable
class TestHashTableWithBigData(unittest.TestCase):
    def setUp(self):
        self.ht = Hashtable()
        self.keys = tuple(k for k in range(1000))
        self.values = tuple(v for v in range(1000, 0, -1))
        data = list(zip(self.keys, self.values))
        for k, v in data:
            self.ht.set(k, v)

    def test_key_values(self):
        self.assertEqual(len(self.ht.keys()), len(self.keys))
        self.assertEqual(sorted(self.ht.keys()), sorted(self.keys))

    def test_get(self):
        for key in self.ht.keys():
            self.assertIsNotNone(self.ht.get(key))

    def test_set(self):
        keys = random.sample(self.ht.keys(), 10)
        new_values = tuple(random.sample(range(500, 1000), 10))
        pairs = list(zip(keys, new_values))

        self.assertNotEqual(self.ht.get(*keys), new_values)
        for k, v in pairs:
            self.ht.set(k, v)
        self.assertEqual(self.ht.get(*keys), new_values)