-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spec.hs
48 lines (40 loc) · 2.05 KB
/
Spec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Test.Hspec
import Test.QuickCheck
import Control.Monad
import Machine
genReflector :: Gen Reflector
genReflector = elements [reflectorA, reflectorB, reflectorB]
genRotor :: Gen Rotor
genRotor = elements [rotorI, rotorII, rotorIII]
genRotors :: Gen Rotors
genRotors = liftM3 (,,) genRotor genRotor genRotor
plugboard :: [Char] -> [(Char, Char)]
plugboard zs = zip xs ys
where (xs, ys) = splitAt ((length zs + 1) `div` 2) zs
genPlugboard :: Gen Plugboard
genPlugboard = fmap plugboard $ shuffle alphabet
genMessage :: Gen String
genMessage = listOf $ elements alphabet
main :: IO ()
main = hspec $ do
describe "Machine" $ do
it "encodes message without plugboard" $ do
encodeMessage (rotorIII, rotorII, rotorI) reflectorB [] "HELLOWORLD" `shouldBe` "ILBDAAMTAZ"
it "encodes message with plugboard" $ do
encodeMessage (rotorIII, rotorII, rotorI) reflectorB [('A', 'B')] "HELLOWORLD" `shouldBe` "ILADBBMTBZ"
it "handles second rotor step" $ do
encodeMessage (rotorIII, rotorII, rotorI) reflectorB [] ([1..102] >> "A") `shouldBe` "BDZGOWCXLTKSBTMCDLPBMUQOFXYHCXTGYJFLINHNXSHIUNTHEORXPQPKOVHCBUBTZSZSOOSTGOTFSODBBZZLXLCYZXIFGWFDZEEQIB"
it "encoded cipher matches message" $ do
property $ forAll genReflector $ \reflector ->
forAll genRotors $ \rotors ->
forAll genPlugboard $ \plugboard ->
forAll genMessage $ \message ->
let cipher = encodeMessage rotors reflector plugboard message in
message == encodeMessage rotors reflector plugboard cipher
it "cipher is same length as message" $ do
property $ forAll genReflector $ \reflector ->
forAll genRotors $ \rotors ->
forAll genPlugboard $ \plugboard ->
forAll genMessage $ \message ->
let cipher = encodeMessage rotors reflector plugboard message in
length message == length cipher