def test_insert_and_get_item_positive(self):
     hmap = Hashmap()
     key = 0
     obj = "zero"
     hmap.insert(key, obj)
     res = hmap.get(key)
     self.assertEqual(obj, res)
 def __init__(self):
     self.hashmap = Hashmap()
     self.messages = []
     self.key = ""
     self.value = ""
     self.operation = HashmapOperation.INSERT
     self.set_value_textbox_enabled()
     self.set_button_run_disabled()
 def test_get_keys_three_items_hashmap_positive(self):
     hmap = Hashmap()
     keys = [123, "asda", 1023]
     for key in keys:
         hmap.insert(key, "dummy")
     ret_keys = hmap.keys()
     keys.sort(key=lambda x: hash(x))
     ret_keys.sort(key=lambda x: hash(x))
     self.assertEqual(keys, ret_keys)
 def test_insert_few_items_in_hashmap_size_one_positive(self):
     hmap = Hashmap(1)
     keys = [123, "asda", 1023]
     for key in keys:
         hmap.insert(key, "dummy")
     ret_keys = hmap.keys()
     keys.sort(key=lambda x: hash(x))
     ret_keys.sort(key=lambda x: hash(x))
     self.assertEqual(keys, ret_keys)
예제 #5
0
 def __init__(self, logger=RealLogger()):
     self.logger = logger
     self.logger.log("create view")
     self.hashmap = Hashmap()
     self.messages = []
     self.key = ""
     self.value = ""
     self.operation = HashmapOperation.INSERT
     self.set_value_textbox_enabled()
     self.set_button_run_disabled()
 def test_insert_two_items_with_same_hash_positive(self):
     hmap = Hashmap(1)
     key1 = 0
     key2 = 1
     obj1 = "zero"
     obj2 = "one"
     hmap.insert(key1, obj1)
     hmap.insert(key2, obj2)
     res1 = hmap.get(key1)
     res2 = hmap.get(key2)
     self.assertEqual(obj1, res1)
     self.assertEqual(obj2, res2)
 def test_insert_few_items_and_get_items_positive(self):
     hmap = Hashmap()
     key1 = 0
     key2 = 1
     obj1 = "zero"
     obj2 = "one"
     hmap.insert(key1, obj1)
     hmap.insert(key2, obj2)
     res1 = hmap.get(key1)
     res2 = hmap.get(key2)
     self.assertEqual(obj1, res1)
     self.assertEqual(obj2, res2)
 def test_update_item_positive(self):
     hmap = Hashmap()
     key = 0
     obj = "zero"
     obj_updated = "0"
     hmap.insert(key, obj)
     hmap.update(key, obj_updated)
     res = hmap.get(key)
     self.assertEqual(obj_updated, res)
 def test_insert_two_items_with_same_key_negative(self):
     hmap = Hashmap()
     key = 0
     obj1 = "zero"
     obj2 = "one"
     hmap.insert(key, obj1)
     with self.assertRaises(KeyError):
         hmap.insert(key, obj2)
 def test_get_keys_empty_hashmap_positive(self):
     hmap = Hashmap()
     keys = []
     ret_keys = hmap.keys()
     self.assertEqual(keys, ret_keys)
 def test_update_item_not_exists_negative(self):
     hmap = Hashmap()
     key = 0
     obj = "zero"
     with self.assertRaises(KeyError):
         hmap.update(key, obj)
 def test_create_hashmap_positive(self):
     Hashmap()
 def test_delete_item_not_exists_negative(self):
     hmap = Hashmap()
     with self.assertRaises(KeyError):
         hmap.delete(0)
 def test_get_item_not_exists_negative(self):
     hmap = Hashmap()
     with self.assertRaises(KeyError):
         hmap.get(0)
 def test_key_not_hashable_negative(self):
     hmap = Hashmap()
     key = ([])
     obj = "zero"
     with self.assertRaises(TypeError):
         hmap.insert(key, obj)
예제 #16
0
class HashmapViewModel:
    MSG_TYPES = {
        "insert_msg": "Insert: key is '%s', value is '%s'",
        "delete_msg": "Delete: key is '%s'",
        "get_msg": "Get: key is '%s', found value is '%s'",
        "update_msg": "Update: key is '%s', value is '%s'",
        "key_not_exist_msg": "ERROR: key '%s' does not exist",
        "key_exist_msg": "ERROR: key '%s' already exists",
    }
    MAX_MESSAGE_NUMBER = 10

    def __init__(self, logger=RealLogger()):
        self.logger = logger
        self.logger.log("create view")
        self.hashmap = Hashmap()
        self.messages = []
        self.key = ""
        self.value = ""
        self.operation = HashmapOperation.INSERT
        self.set_value_textbox_enabled()
        self.set_button_run_disabled()

    def validate_text(self):
        if self.key and (self.value
                         or self.value_textbox_state == State.DISABLED):
            self.set_button_run_enabled()
        else:
            self.set_button_run_disabled()

    def set_button_run_enabled(self):
        self.button_run_state = State.ENABLED

    def set_button_run_disabled(self):
        self.button_run_state = State.DISABLED

    def get_button_run_state(self):
        return self.button_run_state.value

    def set_value_textbox_enabled(self):
        self.value_textbox_state = State.ENABLED

    def set_value_textbox_disabled(self):
        self.set_value("")
        self.value_textbox_state = State.DISABLED

    def get_value_textbox_state(self):
        return self.value_textbox_state.value

    def get_key(self):
        return self.key

    def set_key(self, key):
        self.key = key
        self.validate_text()

    def get_value(self):
        return self.value

    def set_value(self, value):
        self.value = value
        self.validate_text()

    def get_message_text(self):
        return "\n".join(self.messages)

    def update_messages(self, new_message):
        self.messages.insert(0, new_message)
        if len(self.messages) > self.MAX_MESSAGE_NUMBER:
            self.messages = self.messages[:self.MAX_MESSAGE_NUMBER]

    def set_operation(self, operation):
        self.operation = operation
        if operation in [HashmapOperation.DELETE, HashmapOperation.GET]:
            self.set_value_textbox_disabled()
            self.set_value("")
        else:
            self.set_value_textbox_enabled()
        self.validate_text()

    def click_run_button(self):
        if self.button_run_state == State.DISABLED:
            self.logger.log("button is disabled")
            return
        if self.operation == HashmapOperation.INSERT:
            try:
                self.hashmap.insert(self.key, self.value)
                self.update_messages(self.MSG_TYPES["insert_msg"] %
                                     (self.key, self.value))
                self.logger.log(self.MSG_TYPES["insert_msg"] %
                                (self.key, self.value))
            except KeyError:
                self.update_messages(self.MSG_TYPES["key_exist_msg"] %
                                     (self.key))
                self.logger.log(self.MSG_TYPES["key_exist_msg"] % (self.key))

        elif self.operation == HashmapOperation.DELETE:
            try:
                self.hashmap.delete(self.key)
                self.update_messages(self.MSG_TYPES["delete_msg"] % (self.key))
                self.logger.log(self.MSG_TYPES["delete_msg"] % (self.key))
            except KeyError:
                self.update_messages(self.MSG_TYPES["key_not_exist_msg"] %
                                     (self.key))
                self.logger.log(self.MSG_TYPES["key_not_exist_msg"] %
                                (self.key))

        elif self.operation == HashmapOperation.GET:
            try:
                value = self.hashmap.get(self.key)
                self.update_messages(self.MSG_TYPES["get_msg"] %
                                     (self.key, value))
                self.logger.log(self.MSG_TYPES["get_msg"] % (self.key, value))
            except KeyError:
                self.update_messages(self.MSG_TYPES["key_not_exist_msg"] %
                                     (self.key))
                self.logger.log(self.MSG_TYPES["key_not_exist_msg"] %
                                (self.key))

        elif self.operation == HashmapOperation.UPDATE:
            try:
                value = self.hashmap.update(self.key, self.value)
                self.update_messages(self.MSG_TYPES["update_msg"] %
                                     (self.key, self.value))
                self.logger.log(self.MSG_TYPES["update_msg"] %
                                (self.key, self.value))
            except KeyError:
                self.update_messages(self.MSG_TYPES["key_not_exist_msg"] %
                                     (self.key))
                self.logger.log(self.MSG_TYPES["key_not_exist_msg"] %
                                (self.key))