-- Simple test wrapper for Cesar Cipher example -- -- Usage: ./TestCesar -- Compile like this: ghc --make -o TestCesar TestCesar.hs -- -- Reads text from the given file, splits it into lines, -- encodes each line, decodes each line and checks -- whether the decoded text matches the input text. ----------------------------------------------------------------------------- module Main where import System.Environment import System.IO import System.Random import Cesar main = do args <- getArgs -- read command line arguments :: [String] let [fn] = args -- exactly 1 argument required: filename contents <- readFile fn -- read entire contents as string let strs = lines contents -- split by lines key <- getStdRandom (randomR (1,25)) -- randomly generated key let cs = map (encode key) strs -- encode all lines let zs = map crack cs -- decode all lines putStrLn $ unlines -- check for equality and print result [if ok then "Crack successful" else ("Crack failed on string: "++str ) | (str,c,z) <- zip3 strs cs zs, let ok = crack c == str ]