aboutsummaryrefslogtreecommitdiff
path: root/Haskell-book/14/morse/tests
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
committerEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
commit98329e0a3dd4f78b5d815ac3896272ec70904901 (patch)
tree80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/14/morse/tests
parent3624c712d72d246f21d4e710cec7c11e052e0326 (diff)
downloadbook-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/14/morse/tests')
-rw-r--r--Haskell-book/14/morse/tests/CoArbitrary.hs16
-rw-r--r--Haskell-book/14/morse/tests/WordNumberTest.hs24
-rw-r--r--Haskell-book/14/morse/tests/tests.hs86
3 files changed, 126 insertions, 0 deletions
diff --git a/Haskell-book/14/morse/tests/CoArbitrary.hs b/Haskell-book/14/morse/tests/CoArbitrary.hs
new file mode 100644
index 0000000..dc0da3d
--- /dev/null
+++ b/Haskell-book/14/morse/tests/CoArbitrary.hs
@@ -0,0 +1,16 @@
+{-# LANGUAGE DeriveGeneric #-}
+
+module CoArbitrary where
+
+import GHC.Generics
+import Test.QuickCheck
+
+data Bool' = True' | False' deriving (Generic)
+
+instance CoArbitrary Bool'
+
+trueGen :: Gen Int
+trueGen = coarbitrary True' arbitrary
+
+falseGen :: Gen Int
+falseGen = coarbitrary False' arbitrary \ No newline at end of file
diff --git a/Haskell-book/14/morse/tests/WordNumberTest.hs b/Haskell-book/14/morse/tests/WordNumberTest.hs
new file mode 100644
index 0000000..d9623a9
--- /dev/null
+++ b/Haskell-book/14/morse/tests/WordNumberTest.hs
@@ -0,0 +1,24 @@
+module Main where
+
+import Test.Hspec
+import WordNumber (digitToWord, digits, wordNumber)
+
+main :: IO ()
+main = hspec $ do
+ describe "digitToWord" $ do
+ it "returns zero for 0" $ do
+ digitToWord 0 `shouldBe`"zero"
+ it "returns one for 1" $ do
+ digitToWord 1 `shouldBe` "one"
+
+ describe "digits" $ do
+ it "returns [1] for 1" $ do
+ digits 1 `shouldBe` [1]
+ it "returns [1, 0, 0] for 100" $ do
+ digits 100 `shouldBe` [1, 0, 0]
+
+ describe "wordNumber" $ do
+ it "one-zero-zero given 100" $ do
+ wordNumber 100 `shouldBe` "one-zero-zero"
+ it "nine-zero-zero-one for 9001" $ do
+ wordNumber 9001 `shouldBe` "nine-zero-zero-one" \ No newline at end of file
diff --git a/Haskell-book/14/morse/tests/tests.hs b/Haskell-book/14/morse/tests/tests.hs
new file mode 100644
index 0000000..b27d3b3
--- /dev/null
+++ b/Haskell-book/14/morse/tests/tests.hs
@@ -0,0 +1,86 @@
+module Main where
+
+import qualified Data.Map as M
+import Morse
+import Test.QuickCheck
+import Test.QuickCheck.Gen (oneof)
+
+allowedChars :: [Char]
+allowedChars = M.keys letterToMorse
+
+allowedMorse :: [Morse]
+allowedMorse = M.elems letterToMorse
+
+charGen :: Gen Char
+charGen = elements allowedChars
+
+morseGen :: Gen Morse
+morseGen = elements allowedMorse
+
+prop_thereAndBackAgain :: Property
+prop_thereAndBackAgain =
+ forAll charGen (\c -> ((charToMorse c) >>= morseToChar) == Just c)
+
+main' :: IO ()
+main' = quickCheck prop_thereAndBackAgain
+
+data Trivial = Trivial deriving (Eq, Show)
+
+trivialGen :: Gen Trivial
+trivialGen = return Trivial
+
+instance Arbitrary Trivial where
+ arbitrary = trivialGen
+
+main :: IO ()
+main = do
+ sample trivialGen
+
+data Identity a = Identity a deriving (Eq, Show)
+
+identityGen :: Arbitrary a => Gen (Identity a)
+identityGen = do
+ a <- arbitrary
+ return (Identity a)
+
+instance Arbitrary a => Arbitrary (Identity a) where
+ arbitrary = identityGen
+
+identityGenInt :: Gen (Identity Int)
+identityGenInt = identityGen
+
+data Pair a b = Pair a b deriving (Eq, Show)
+
+pairGen :: (Arbitrary a, Arbitrary b) => Gen (Pair a b)
+pairGen = do
+ a <- arbitrary
+ b <- arbitrary
+ return (Pair a b)
+
+instance (Arbitrary a, Arbitrary b) => Arbitrary (Pair a b) where
+ arbitrary = pairGen
+
+pairGenIntString :: Gen (Pair Int String)
+pairGenIntString = pairGen
+
+data Sum a b = First a | Second b deriving (Eq, Show)
+
+sumGenEqual :: Gen (Sum Char Int)
+sumGenEqual = do
+ a <- arbitrary
+ b <- arbitrary
+ oneof [return $ First a,
+ return $ Second b]
+
+sumGenCharInt :: Gen (Sum Char Int)
+sumGenCharInt = sumGenEqual
+
+sumGenFirstPls :: (Arbitrary a, Arbitrary b) => Gen (Sum a b)
+sumGenFirstPls = do
+ a <- arbitrary
+ b <- arbitrary
+ frequency [(10, return $ First a),
+ (1, return $ Second b)]
+
+sumGenCharIntFirst :: Gen (Sum Char Int)
+sumGenCharIntFirst = sumGenFirstPls \ No newline at end of file