Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
*.swp
dist
dist-*
cabal-dev
*.o
*.hi
*.chi
*.chs.h
*.dyn_o
*.dyn_hi
.hpc
.hsenv
.cabal-sandbox/
cabal.sandbox.config
*.prof
*.aux
*.hp
*.eventlog
.stack-work/
2 changes: 1 addition & 1 deletion homework01/basis.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test-suite examples
build-depends:
base
, basis
, doctest >= 0.10
, hspec
default-language: Haskell2010


Expand Down
2 changes: 1 addition & 1 deletion homework01/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.6
resolver: lts-7.14
72 changes: 61 additions & 11 deletions homework01/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
module Main
( main
)
where

-- doctest
import qualified Test.DocTest as DocTest
( main )
where

import Basis
import Test.Hspec

main :: IO ()
main =
DocTest.doctest
[ "-isrc"
, "src/Basis.hs"
]
main = hspec $ do

describe "toDigits" $ do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's important, but what about the case for numbers starting with 0 like 01234

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature of toDigits function is toDigits :: Integer -> [Integer]. Haskell don't use leading zeros, so toDigits 01234 == toDigits 1234.

context "when the number is greater than 0" $
it "returns a list of digits" $
toDigits 1234 `shouldBe` [1, 2, 3, 4]

context "when the number is 0" $
it "returns an empty list" $
toDigits 0 `shouldBe` []

context "when the number is less than 0" $
it "returns an empty list" $
toDigits (-17) `shouldBe` []

describe "toDigitsRev" $ do
context "when the number is greater than 0" $
it "returns a list of digits reversed" $
toDigitsRev 9876 `shouldBe` [6, 7, 8, 9]

context "when the number is 0" $
it "returns an empty list" $
toDigitsRev 0 `shouldBe` []

context "when the number is less than 0" $
it "returns an empty list" $
toDigitsRev (-1) `shouldBe` []

describe "doubleEveryOther" $ do
context "when the list has an even number of elements" $
it "doubles every other number beginning from the right" $
doubleEveryOther [8, 7, 6, 5] `shouldBe` [16, 7, 12, 5]

context "when the list has an odd number of elements" $
it "doubles every other number beginning from the right" $
doubleEveryOther [1, 2, 3] `shouldBe` [1, 4, 3]

describe "sumDigits" $
it "calculates the sum of sums of the digits of each element in a list" $
sumDigits [16, 7, 12, 5] `shouldBe` 22

describe "validate" $ do
context "when the credit card number is valid" $
it "returns True" $
validate 4012888888881881 `shouldBe` True

context "when the credit card number is invalid" $
it "returns False" $
validate 4012888888881882 `shouldBe` False

describe "hanoi" $
context "when the stack discs moves from first peg to second" $ do
it "gets a list of moves whit 2 discs" $
hanoi 2 "a" "b" "c" `shouldBe` [("a", "c"), ("a", "b"), ("c", "b")]

it "gets a list of moves whit 3 discs" $
hanoi 3 "a" "b" "c" `shouldBe` [("a","b"),("a","c"),("b","c"),("a","b"),("c","a"),("c","b"),("a","b")]
2 changes: 1 addition & 1 deletion homework02/data-types.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test-suite examples
build-depends:
base
, data-types
, doctest >= 0.10
, hspec
default-language: Haskell2010

test-suite suggestions
Expand Down
2 changes: 1 addition & 1 deletion homework02/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.6
resolver: lts-7.14
52 changes: 41 additions & 11 deletions homework02/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
module Main
( main
)
where

-- doctest
import qualified Test.DocTest as DocTest
( main )
where

import Log
import LogAnalysis
import Test.Hspec

main :: IO ()
main =
DocTest.doctest
[ "-isrc"
, "src/LogAnalysis.hs"
]
main = hspec $ do

describe "parseMessage" $ do
it "parses an error message" $
parseMessage "E 2 562 help help" `shouldBe` LogMessage (Error 2) 562 "help help"

it "parses an info message" $
parseMessage "I 29 la la la" `shouldBe` LogMessage Info 29 "la la la"

it "parses an Unknown message" $
parseMessage "This is not in the right format" `shouldBe` Unknown "This is not in the right format"

describe "insert" $
it "returns a tree with LogMessage inserted" $ do
let tree = Node Leaf (LogMessage (Error 2) 562 "help help") Leaf
insert (LogMessage Info 29 "la") tree
`shouldBe` Node (Node Leaf (LogMessage Info 29 "la") Leaf) (LogMessage (Error 2) 562 "help help") Leaf

describe "build" $
it "builds up a MessageTree containing the messages in a list" $ do
let messageTreeSet = [LogMessage (Error 2) 562 "help help", LogMessage Info 29 "la"]
build messageTreeSet
`shouldBe` Node (Node Leaf (LogMessage Info 29 "la") Leaf) (LogMessage (Error 2) 562 "help help") Leaf

describe "inOrder" $
it "produces a list of all LogMessages it contains, sorted by timestamp from smallest to biggest" $ do
let messageTreeSet = [LogMessage (Error 2) 562 "help help",LogMessage Info 29 "la"]
inOrder (build messageTreeSet)
`shouldBe` [LogMessage Info 29 "la",LogMessage (Error 2) 562 "help help"]

describe "whatWentWrong" $
context "with a severity of 50 or greater" $
it "returns a list of the messages corresponding to any errors sorted by timestamp" $ do
parseTest <- testWhatWentWrong parse whatWentWrong "src/sample.log"
parseTest
`shouldBe` ["Way too many pickles","Bad pickle-flange interaction detected","Flange failed!"]
2 changes: 1 addition & 1 deletion homework03/rec-poly.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test-suite examples
build-depends:
base
, rec-poly
, doctest >= 0.10
, hspec
default-language: Haskell2010

test-suite suggestions
Expand Down
2 changes: 1 addition & 1 deletion homework03/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.6
resolver: lts-7.14
48 changes: 37 additions & 11 deletions homework03/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
module Main
( main
)
where

-- doctest
import qualified Test.DocTest as DocTest
( main )
where

import Golf
import Test.Hspec

main :: IO ()
main =
DocTest.doctest
[ "-isrc"
, "src/Golf.hs"
]
main = hspec $ do

describe "skips" $ do
it "skips list of Chars (length 4)" $
skips "ABCD" `shouldBe` ["ABCD", "BD", "C", "D"]

it "skips list of Chars (length 6)" $
skips "hello!" `shouldBe` ["hello!", "el!", "l!", "l", "o", "!"]

it "skips list of one element" $
skips [1] `shouldBe` [[1]]

it "skips list of Booleans" $
skips [True,False] `shouldBe` [[True,False], [False]]

it "skips empty list" $
skips ([] :: [String]) `shouldBe` ([] :: [[String]])

describe "localMaxima" $ do
context "when at least one element of list is greater than both elements immediately before and after it" $ do
it "returns two local maximums" $
localMaxima [2,9,5,6,1] `shouldBe` [9,6]

it "returns one local maximum" $
localMaxima [2,3,4,1,5] `shouldBe` [4]

context "when no element of list is greater than both elements immediately before and after it" $
it "returns zero local maximum" $
localMaxima [1,2,3,4,5] `shouldBe` []

describe "histogram" $
it "outputs a vertical histogram" $
histogram [1,1,1,5] `shouldBe` " * \n * \n * * \n==========\n0123456789\n"
2 changes: 1 addition & 1 deletion homework04/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.10
resolver: lts-7.14
52 changes: 41 additions & 11 deletions homework04/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
module Main
( main
)
where

-- doctest
import qualified Test.DocTest as DocTest
( main )
where

import Wholemeal
import Test.Hspec

main :: IO ()
main =
DocTest.doctest
[ "-isrc"
, "src/Wholemeal.hs"
]
main = hspec $ do

describe "fun1'" $
it "returns same output of fun1" $
fun1' [1,3,5,7] `shouldBe` fun1 [1,3,5,7]

describe "fun2'" $
it "returns same output of fun2" $
fun2' 10 `shouldBe` fun2 10

describe "foldTree" $
-- Because you can have many implementations of foldTree, you will only test the height of the tree
it "returns heigh of a balanced binary tree" $ do
let height (Node x _ _ _) = x
height (foldTree "ABCDEFGHIJ") `shouldBe` 3

describe "xor" $ do
context "when list has an odd number of True values" $
it "returns True" $
xor [False, True, False] `shouldBe` True

context "when list has an even number of True values" $
it "returns False" $
xor [False, True, False, False, True] `shouldBe` False

describe "map'" $ do
context "when list is not empty" $
it "returns same output of standard map function" $
map' (+1) [1,2,3] `shouldBe` map (+1) [1,2,3]

context "when list is empty" $
it "returns same output of standard map function" $
map' (*2) [] `shouldBe` map (*2) []

describe "sieveSundaram" $
it "returns a list of prime numbers less than 2*n + 2" $
sieveSundaram 20 `shouldBe` [3,5,7,11,13,17,19,23,29,31,37,41]
2 changes: 1 addition & 1 deletion homework04/wholemeal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test-suite examples
build-depends:
base
, wholemeal
, doctest >= 0.10
, hspec
default-language: Haskell2010


Expand Down
2 changes: 1 addition & 1 deletion homework05/data-types.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test-suite examples
base
, data-types
, mtl
, doctest >= 0.10
, hspec
default-language: Haskell2010

test-suite suggestions
Expand Down
2 changes: 1 addition & 1 deletion homework05/src/StackVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data StackExp = PushI Integer
| Mul
| And
| Or
deriving Show
deriving (Show, Eq)

type Stack = [StackVal]
type Program = [StackExp]
Expand Down
2 changes: 1 addition & 1 deletion homework05/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.10
resolver: lts-7.14
Loading