Test fixes
This commit is contained in:
parent
700ad655c2
commit
c3a4f085f1
1 changed files with 39 additions and 27 deletions
|
@ -13,28 +13,37 @@ let alphaSeries = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
|
|
||||||
class MachineTests: XCTestCase {
|
class MachineTests: XCTestCase {
|
||||||
func makeMachine() -> Machine! {
|
var machine: Machine!
|
||||||
|
|
||||||
|
override func setUp() {
|
||||||
do {
|
do {
|
||||||
let rotors = [try Rotor(.EnigmaI), try Rotor(.EnigmaII), try Rotor(.EnigmaIII)]
|
let rotors = [try Rotor(.EnigmaI), try Rotor(.EnigmaII), try Rotor(.EnigmaIII)]
|
||||||
rotors[0].notch = 17
|
self.machine = Machine(rotors: rotors, reflector: try Reflector(.EnigmaB), plugboard: Plugboard())
|
||||||
rotors[1].notch = 5
|
|
||||||
rotors[2].notch = 22
|
|
||||||
return Machine(rotors: rotors, reflector: try Reflector(.EnigmaB), plugboard: Plugboard())
|
|
||||||
} catch let error {
|
} catch let error {
|
||||||
XCTFail("Error while creating machine: \(error)")
|
XCTFail("Error while creating machine: \(error)")
|
||||||
}
|
}
|
||||||
// Never reached
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testThatItTurnsOn() {
|
func testThatMachineEncodesWithoutRotorStepping() {
|
||||||
let machine = makeMachine()
|
machine.rotorAdvanceEnabled = false
|
||||||
print(try! machine.encode("A"))
|
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() {
|
func testThatRotorsAdvanceAtNotchPositions() {
|
||||||
let machine = makeMachine()
|
|
||||||
let rotors = machine.rotors
|
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))
|
let rotorPermutations = Int(pow(Double(Cryptor.alphabet.count), 3.0))
|
||||||
for _ in 0..<rotorPermutations {
|
for _ in 0..<rotorPermutations {
|
||||||
let rotorIPosition = rotors[0].position
|
let rotorIPosition = rotors[0].position
|
||||||
|
@ -53,6 +62,15 @@ class MachineTests: XCTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testThatEncodingWithRotorAdvanceWorks() {
|
||||||
|
let rotors = machine.rotors
|
||||||
|
rotors[0].notch = 17
|
||||||
|
rotors[1].notch = 5
|
||||||
|
rotors[2].notch = 22
|
||||||
|
let encoded = try! machine.encode("A")
|
||||||
|
print("encoded: \(encoded)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,30 +94,24 @@ class RotorTests: XCTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testThatRotorCanAdvanceToRot13() {
|
func testThatIdentityRotorReturnsSameCharacters() {
|
||||||
let rotor = makeRotorWithSeries(alphaSeries)
|
let rotor = makeRotorWithSeries(alphaSeries)
|
||||||
rotor.advance(13)
|
for c in alphaSeries.characters {
|
||||||
for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, rot13Series.characters) {
|
XCTAssertEqual(try! rotor.encode(c), c)
|
||||||
XCTAssertEqual(try! rotor.encode(plainCharacter), cipherCharacter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testThatRotorCanSetToRot13() {
|
|
||||||
let rotor = makeRotorWithSeries(alphaSeries)
|
|
||||||
rotor.position = 13
|
|
||||||
for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, rot13Series.characters) {
|
|
||||||
XCTAssertEqual(try! rotor.encode(plainCharacter), cipherCharacter)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testThatRotorCanDoInverseEncoding() {
|
func testThatRotorCanDoInverseEncoding() {
|
||||||
let rotor = makeRotorWithSeries(rotorSeries)
|
let rotor = makeRotorWithSeries(rotorSeries)
|
||||||
for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, rotorSeries.characters) {
|
var encodedCharacter: Character!
|
||||||
|
var originalCharacter: Character!
|
||||||
|
for plainCharacter in alphaSeries.characters {
|
||||||
do {
|
do {
|
||||||
let encodedCharacter = try rotor.inverseEncode(cipherCharacter)
|
encodedCharacter = try rotor.encode(plainCharacter)
|
||||||
XCTAssertEqual(encodedCharacter, plainCharacter)
|
originalCharacter = try rotor.inverseEncode(encodedCharacter)
|
||||||
|
XCTAssertEqual(originalCharacter, plainCharacter)
|
||||||
} catch let error {
|
} catch let error {
|
||||||
XCTFail("Error inverse-encoding \(cipherCharacter) -> \(plainCharacter): \(error)")
|
XCTFail("Error inverse-encoding \(plainCharacter) -> \(encodedCharacter) -> \(originalCharacter): \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue