|
| 1 | +{-# OPTIONS_GHC -Wno-type-defaults #-} |
| 2 | + |
| 3 | +module Test.Chapter1 |
| 4 | + ( chapter1 |
| 5 | + ) where |
| 6 | + |
| 7 | +import Test.Hspec (Spec, describe, it, shouldBe) |
| 8 | + |
| 9 | +import Chapter1 |
| 10 | + |
| 11 | + |
| 12 | +chapter1 :: Spec |
| 13 | +chapter1 = describe "Chapter1" $ do |
| 14 | + chapter1normal |
| 15 | + chapter1advanced |
| 16 | + |
| 17 | +chapter1normal :: Spec |
| 18 | +chapter1normal = describe "Chapter1Normal" $ do |
| 19 | + describe "Task4: next" $ do |
| 20 | + it "returns next Int for 42" $ next 42 `shouldBe` 43 |
| 21 | + it "returns next Int for negative" $ next (-5) `shouldBe` (-4) |
| 22 | + it "returns next Int for 0" $ next 0 `shouldBe` 1 |
| 23 | + describe "Task5: lastDigit" $ do |
| 24 | + it "last digit of 0" $ lastDigit 0 `shouldBe` 0 |
| 25 | + it "last digit of 0 < x < 10" $ lastDigit 5 `shouldBe` 5 |
| 26 | + it "last digit of 10 < x < 100" $ lastDigit 34 `shouldBe` 4 |
| 27 | + it "last digit of 100 < x < 1000" $ lastDigit 341 `shouldBe` 1 |
| 28 | + it "last digit of big num" $ lastDigit 1234789 `shouldBe` 9 |
| 29 | + it "last digit of negative" $ lastDigit (-12) `shouldBe` 2 |
| 30 | + describe "Task6: closestToZero" $ do |
| 31 | + it "both positive, 1st wins" $ closestToZero 100 200 `shouldBe` 100 |
| 32 | + it "both positive, 2nd wins" $ closestToZero 200 100 `shouldBe` 100 |
| 33 | + it "both negative, 2nd wins" $ closestToZero (-200) (-100) `shouldBe` (-100) |
| 34 | + it "both negative, 2nd wins" $ closestToZero (-100) (-200) `shouldBe` (-100) |
| 35 | + it "with 0, 1nd wins" $ closestToZero 0 (-200) `shouldBe` 0 |
| 36 | + it "with 0, 2nd wins" $ closestToZero 10 0 `shouldBe` 0 |
| 37 | + it "equals" $ closestToZero 42 42 `shouldBe` 42 |
| 38 | + it "positive, negative, pos wins" $ closestToZero 11 (-12) `shouldBe` 11 |
| 39 | + it "positive, negative, neg wins" $ closestToZero 12 (-11) `shouldBe` (-11) |
| 40 | + describe "Task7: mid" $ do |
| 41 | + it "positives up " $ mid 10 20 30 `shouldBe` 20 |
| 42 | + it "positives down" $ mid 30 20 10 `shouldBe` 20 |
| 43 | + it "negatives down" $ mid (-10) (-20) (-30) `shouldBe` (-20) |
| 44 | + it "negatives up " $ mid (-30) (-20) (-10) `shouldBe` (-20) |
| 45 | + it "all equal" $ mid 1 1 1 `shouldBe` 1 |
| 46 | + it "all equal, except 1" $ mid 1 1 2 `shouldBe` 1 |
| 47 | + describe "Task8: isVowel" $ do |
| 48 | + it "true for vowels" $ all isVowel "aeiou" `shouldBe` True |
| 49 | + it "false for non-vowels" $ isVowel 'c' `shouldBe` False |
| 50 | + it "false for symbol" $ isVowel '+' `shouldBe` False |
| 51 | + describe "Task9: sumLast2" $ do |
| 52 | + it "sumLast2 0" $ sumLast2 0 `shouldBe` 0 |
| 53 | + it "sumLast2 0 < 10" $ sumLast2 9 `shouldBe` 9 |
| 54 | + it "sumLast2 10 < 100" $ sumLast2 56 `shouldBe` 11 |
| 55 | + it "sumLast2 100 < 1000" $ sumLast2 987 `shouldBe` 15 |
| 56 | + it "sumLast2 0 > -10" $ sumLast2 (-9) `shouldBe` 9 |
| 57 | + it "sumLast2 -10 > -100" $ sumLast2 (-56) `shouldBe` 11 |
| 58 | + it "sumLast2 -100 > -1000" $ sumLast2 (-987) `shouldBe` 15 |
| 59 | + |
| 60 | +chapter1advanced :: Spec |
| 61 | +chapter1advanced = describe "Chapter1Advanced" $ do |
| 62 | + it "first digit 0" $ firstDigit 0 `shouldBe` 0 |
| 63 | + it "first digit 0 < 10" $ firstDigit 9 `shouldBe` 9 |
| 64 | + it "first digit 10 < 100" $ firstDigit 58 `shouldBe` 5 |
| 65 | + it "first digit 100 < 1000" $ firstDigit 158 `shouldBe` 1 |
| 66 | + it "first digit big" $ firstDigit 467321 `shouldBe` 4 |
| 67 | + it "first digit 0 > -10" $ firstDigit (-9) `shouldBe` 9 |
| 68 | + it "first digit -10 > -100" $ firstDigit (-58) `shouldBe` 5 |
| 69 | + it "first digit -100 > -1000" $ firstDigit (-158) `shouldBe` 1 |
0 commit comments