def test_rotor_mechanism_4(self): reflector = Reflector(EnigmaSettings.get_reflectors()['B']) # A/B/C rotor_1 = Rotor(EnigmaSettings.get_rotors()['I'], 'A') # I / II / III / IV / V rotor_2 = Rotor(EnigmaSettings.get_rotors()['II'], 'A') # I / II / III / IV / V rotor_3 = Rotor(EnigmaSettings.get_rotors()['III'], 'A') # I / II / III / IV / V rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], reflector) self.assertEqual('A', chr(rotor_mechanism.process(ord('B') - ord('A') + 1) - 1 + ord('A'))) self.assertEqual('A', chr(rotor_mechanism.process(ord('D') - ord('A') + 1) - 1 + ord('A'))) self.assertEqual('A', chr(rotor_mechanism.process(ord('Z') - ord('A') + 1) - 1 + ord('A'))) self.assertEqual('A', chr(rotor_mechanism.process(ord('G') - ord('A') + 1) - 1 + ord('A'))) self.assertEqual('A', chr(rotor_mechanism.process(ord('O') - ord('A') + 1) - 1 + ord('A')))
def test_rotor_mechanism_2(self): rotor_1 = Rotor(EnigmaSettings.get_rotors()['I'], 'A') #I / II / III / IV / V rotor_2 = Rotor(EnigmaSettings.get_rotors()['II'], 'A') # I / II / III / IV / V rotor_3 = Rotor(EnigmaSettings.get_rotors()['III'], 'A') # I / II / III / IV / V rotor_mechanism = RotorMechanism([rotor_3, rotor_2, rotor_1], None) try: rotor_mechanism.process(25) self.assertFalse(True) except AttributeError as ae: self.assertEqual(str(ae), str(AttributeError('\'NoneType\' object has no attribute \'wiring\''))) try: rotor_mechanism.process(225) self.assertFalse(True) except KeyError as ke: self.assertTrue(True)
def write_text(self, text: str, rotor_mechanism: RotorMechanism) -> str: if text is None: text = '' after_text = '' text = text.upper() for character in text: valid_character(character, True) for character in text: if self.plug_board is not None: plug_board_character = self.plug_board.get_plug_char(character) else: plug_board_character = character plug_board_num = az_to_index(plug_board_character) + 1 wheel_pack_num = rotor_mechanism.process(plug_board_num) wheel_pack_character = index_to_az(wheel_pack_num - 1) if self.plug_board is not None: plug_board_character_2 = self.plug_board.get_plug_char( wheel_pack_character) else: plug_board_character_2 = wheel_pack_character after_text += plug_board_character_2 return after_text