File tree 4 files changed +48
-5
lines changed
4 files changed +48
-5
lines changed Original file line number Diff line number Diff line change @@ -28,17 +28,17 @@ library
28
28
Ch14.Addition
29
29
Ch14.Morse
30
30
Ch8.Playground
31
+ Ch11.Cipher
32
+ Ch9.Cipher
31
33
other-modules :
32
34
Ch10.Playground
33
- Ch11.Cipher
34
35
Ch11.HuttonsRazor
35
36
Ch11.Phone
36
37
Ch11.PhoneAlt
37
38
Ch11.Playground
38
39
Ch12.Playground
39
40
Ch13.Person
40
41
Ch13.Playground
41
- Ch9.Cipher
42
42
Ch9.Playground
43
43
Ch9.StdFunc
44
44
Lib
@@ -50,6 +50,7 @@ library
50
50
default-extensions :
51
51
ImportQualifiedPost
52
52
LambdaCase
53
+ TypeApplications
53
54
ghc-options : -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
54
55
build-depends :
55
56
PyF
@@ -72,6 +73,7 @@ executable hffp
72
73
default-extensions :
73
74
ImportQualifiedPost
74
75
LambdaCase
76
+ TypeApplications
75
77
ghc-options : -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
76
78
build-depends :
77
79
base >= 4.7 && < 5
@@ -82,6 +84,7 @@ test-suite hffp-test
82
84
type : exitcode-stdio-1.0
83
85
main-is : Spec.hs
84
86
other-modules :
87
+ Ch14.CipherSpec
85
88
Ch14.ExerciseSpec
86
89
Ch14.Identity
87
90
Ch14.MorseSpec
@@ -95,6 +98,7 @@ test-suite hffp-test
95
98
default-extensions :
96
99
ImportQualifiedPost
97
100
LambdaCase
101
+ TypeApplications
98
102
ghc-options : -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
99
103
build-depends :
100
104
QuickCheck
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ dependencies:
25
25
default-extensions :
26
26
- ImportQualifiedPost
27
27
- LambdaCase
28
+ - TypeApplications
28
29
29
30
ghc-options :
30
31
- -Wall
@@ -50,6 +51,8 @@ library:
50
51
- Ch14.Addition
51
52
- Ch14.Morse
52
53
- Ch8.Playground
54
+ - Ch11.Cipher
55
+ - Ch9.Cipher
53
56
54
57
executables :
55
58
hffp :
Original file line number Diff line number Diff line change 1
- module Ch11.Cipher (vigenereCipher , CaesarCipher. cipherMsg ) where
1
+ module Ch11.Cipher (vigenereCipher , CaesarCipher. cipherMsg, vigenereUncipher ) where
2
2
3
3
import Ch9.Cipher qualified as CaesarCipher
4
4
@@ -9,11 +9,11 @@ type Message = String
9
9
type CipherText = String
10
10
11
11
ceilingDivision :: Int -> Int -> Int
12
- ceilingDivision num1 num2 = ceiling . fromIntegral $ div num1 num2
12
+ ceilingDivision num1 num2 = ( ceiling @ Double ) . fromIntegral $ div num1 num2
13
13
14
14
standardizeKeywordLength :: Keyword -> Message -> String
15
15
standardizeKeywordLength keyword msg
16
- | length keyword < length msg = let replicateCount = ceilingDivision (length msg) (length keyword) in concat $ replicate replicateCount keyword
16
+ | length keyword < length msg = let replicateCount = ceilingDivision (length msg) (length keyword) in concat $ replicate ( replicateCount + 1 ) keyword
17
17
| otherwise = keyword
18
18
19
19
zipWithSpaces :: String -> String -> [(Char , Char )]
Original file line number Diff line number Diff line change
1
+ module Ch14.CipherSpec where
2
+
3
+ import Ch11.Cipher
4
+ import Ch9.Cipher
5
+
6
+ import Test.Hspec
7
+ import Test.Hspec.QuickCheck
8
+ import Test.QuickCheck
9
+
10
+ import Data.Char
11
+
12
+ newtype AlphaChar = AlphaChar { unAlphaChar :: Char } deriving (Eq , Show )
13
+
14
+ genAlphaChar :: Gen AlphaChar
15
+ genAlphaChar = do
16
+ let upperCaseAlphaRange = (65 , 90 )
17
+ let lowerCaseAlphaRange = (97 , 122 )
18
+ charCode <- oneof [chooseInt upperCaseAlphaRange, chooseInt lowerCaseAlphaRange]
19
+
20
+ pure $ AlphaChar $ chr charCode
21
+
22
+ instance Arbitrary AlphaChar where
23
+ arbitrary = genAlphaChar
24
+
25
+ spec :: Spec
26
+ spec = do
27
+ describe " Cipher Identity" $ do
28
+ prop " Vigenere Cipher" $ \ (NonEmpty keyword') (NonEmpty msg') ->
29
+ let keyword = unAlphaChar <$> (keyword' :: [AlphaChar ])
30
+ msg = unAlphaChar <$> (msg' :: [AlphaChar ])
31
+ in vigenereUncipher keyword (vigenereCipher keyword msg) == msg
32
+
33
+ prop " Caesar Cipher" $ \ (Positive delta') (NonEmpty msg') ->
34
+ let delta = (delta' :: Int )
35
+ msg = unAlphaChar <$> (msg' :: [AlphaChar ])
36
+ in unCipherMsg delta (cipherMsg delta msg) == msg
You can’t perform that action at this time.
0 commit comments