diff --git a/EnigmaTests/EnigmaTests.swift b/EnigmaTests/EnigmaTests.swift index 6872f31..f4ee167 100644 --- a/EnigmaTests/EnigmaTests.swift +++ b/EnigmaTests/EnigmaTests.swift @@ -13,28 +13,37 @@ let alphaSeries = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" class MachineTests: XCTestCase { - func makeMachine() -> Machine! { + var machine: Machine! + + override func setUp() { do { let rotors = [try Rotor(.EnigmaI), try Rotor(.EnigmaII), try Rotor(.EnigmaIII)] - rotors[0].notch = 17 - rotors[1].notch = 5 - rotors[2].notch = 22 - return Machine(rotors: rotors, reflector: try Reflector(.EnigmaB), plugboard: Plugboard()) + self.machine = Machine(rotors: rotors, reflector: try Reflector(.EnigmaB), plugboard: Plugboard()) } catch let error { XCTFail("Error while creating machine: \(error)") } - // Never reached - return nil } - func testThatItTurnsOn() { - let machine = makeMachine() - print(try! machine.encode("A")) + func testThatMachineEncodesWithoutRotorStepping() { + machine.rotorAdvanceEnabled = false + let unsteppedSeries = "UEJOBTPZWCNSRKDGVMLFAQIYXH" + for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, unsteppedSeries.characters) { + do { + let encodedCharacter = try machine.encode(plainCharacter) + let decodedCharacter = try machine.encode(cipherCharacter) + XCTAssertEqual(encodedCharacter, cipherCharacter) + XCTAssertEqual(decodedCharacter, plainCharacter) + } catch let error { + XCTFail("Error doing basic encoding: \(error)") + } + } } func testThatRotorsAdvanceAtNotchPositions() { - let machine = makeMachine() let rotors = machine.rotors + rotors[0].notch = 17 + rotors[1].notch = 5 + rotors[2].notch = 22 let rotorPermutations = Int(pow(Double(Cryptor.alphabet.count), 3.0)) for _ in 0.. \(plainCharacter): \(error)") + XCTFail("Error inverse-encoding \(plainCharacter) -> \(encodedCharacter) -> \(originalCharacter): \(error)") } } }