Ejemplo n.º 1
0
class Checker():
    def __init__(self):
        self.checker = HashTable()

        self.checker.insert("Servico", False)
        self.checker.insert("Categoria", False)
        self.checker.insert("Profissional", False)
        self.checker.insert("CInvalida", False)
        self.checker.insert("SInvalida", False)

    def get(self, key):
        return self.checker.get(key)

    def checks_out(self):
        it = self.checker.values().iterator()
        while it.has_next():
            if it.next():
                return False
        return True

    def change_servico(self):
        self.checker.update('Servico', True)

    def change_categoria(self):
        self.checker.update('Categoria', True)

    def change_profissional(self):
        self.checker.update(
            'Profissional', True)

    def change_cinvalida(self):
        self.checker.update('CInvalida', True)

    def change_sinvalida(self):
        self.checker.update('SInvalida', True)
Ejemplo n.º 2
0
class Familia():
    def __init__(self, nome):
        self.nome = nome
        self.membros = HashTable()

    def get_nome(self):
        return self.nome

    def get_membros(self):
        return self.membros.values()

    def add_member(self, member):
        self.membros.insert(member.get_nome(), member)

    def remove_member(self, member):
        self.membros.remove(member.get_nome())
Ejemplo n.º 3
0
class TestHashTable(unittest.TestCase):
    def setUp(self):
        self.table = HashTable(size=11)

    def insert_items(self, quantity, shift=0):
        for i in range(quantity):
            k = f"key_{i+1+shift}"
            v = f"value_{i+1+shift}"
            self.table.insert(k, v)

    def remove_items(self, quantity, shift=0):
        for i in range(quantity):
            k = f"key_{i+1+shift}"
            self.table.remove(k)

    def test_size(self):
        self.assertEqual(self.table.size(), 0)
        self.insert_items(5)
        self.assertEqual(self.table.size(), 5)

    def test_is_full(self):
        self.table = HashTable(size=7)
        self.assertFalse(self.table.is_full())
        self.insert_items(7)
        self.assertTrue(self.table.is_full())
        self.remove_items(1)
        self.assertFalse(self.table.is_full())

    def test_get(self):
        with self.assertRaises(NoSuchElementException):
            self.table.get("missing_key")
        self.insert_items(5)
        self.assertEqual(self.table.get("key_3"), "value_3")
        with self.assertRaises(NoSuchElementException):
            self.table.get("missing_key")
        self.remove_items(1, shift=2)
        with self.assertRaises(NoSuchElementException):
            self.table.get("key_3")
        self.insert_items(1, shift=2)
        self.assertEqual(self.table.get("key_3"), "value_3")

    def test_insert(self):
        self.assertFalse(self.table.has_key("key_1"))
        self.table.insert("key_1", "value_1")
        self.assertTrue(self.table.has_key("key_1"))
        with self.assertRaises(DuplicatedKeyException):
            self.table.insert("key_1", "value_1")
        self.assertEqual(self.table.get("key_1"), "value_1")

    def test_update(self):
        with self.assertRaises(NoSuchElementException):
            self.table.update("key_1", "new_value_1")
        self.insert_items(1)
        self.assertEqual(self.table.get("key_1"), "value_1")
        self.table.update("key_1", "new_value_1")
        self.assertEqual(self.table.get("key_1"), "new_value_1")

    def test_remove(self):
        with self.assertRaises(NoSuchElementException):
            self.table.remove("missing_key")
        self.insert_items(1)
        self.assertEqual(self.table.remove("key_1"), "value_1")
        with self.assertRaises(NoSuchElementException):
            self.table.remove("key_1")
        self.insert_items(5)
        self.assertEqual(self.table.remove("key_3"), "value_3")

    def test_keys(self):
        self.assertEqual(self.table.keys().size(), 0)
        self.insert_items(5)
        self.assertEqual(self.table.keys().size(), 5)
        self.assertNotEqual(self.table.keys().find("key_1"), -1)

    def test_values(self):
        self.assertEqual(self.table.values().size(), 0)
        self.insert_items(5)
        self.assertEqual(self.table.values().size(), 5)
        self.assertNotEqual(self.table.values().find("value_1"), -1)

    def test_items(self):
        self.assertEqual(self.table.items().size(), 0)
        self.insert_items(5)
        self.assertEqual(self.table.items().size(), 5)
        it = self.table.items().iterator()
        while it.has_next():
            item = it.next()
            self.assertIn(item.get_key(), [f"key_{i+1}" for i in range(5)])
            self.assertIn(item.get_value(), [f"value_{i+1}" for i in range(5)])