1
exercism fetch haskell binary-search-tree

test/Tests.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Test.Hspec        (Spec, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import BST
  ( bstLeft
  , bstRight
  , bstValue
  , empty
  , fromList
  , insert
  , singleton
  , toList
  )

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = do

    let int4   = 4  ::  Int
    let noInts = [] :: [Int]

    it "data is retained" $
      bstValue (singleton int4) `shouldBe` Just 4

    it "inserting less" $ do
      let t = insert 2 (singleton int4)
      bstValue t `shouldBe` Just 4
      (bstLeft t >>= bstValue) `shouldBe` Just 2

    it "inserting same" $ do
      let t = insert 4 (singleton int4)
      bstValue t `shouldBe` Just 4
      (bstLeft t >>= bstValue) `shouldBe` Just 4

    it "inserting right" $ do
      let t = insert 5 (singleton int4)
      bstValue t `shouldBe` Just 4
      (bstRight t >>= bstValue) `shouldBe` Just 5

    it "empty list to tree" $
      fromList noInts `shouldBe` empty

    it "empty list has no value" $
      bstValue (fromList noInts) `shouldBe` Nothing

    it "inserting into empty" $ do
      let t = insert int4 empty
      bstValue t `shouldBe` Just 4

    it "complex tree" $ do
      let t = fromList [int4, 2, 6, 1, 3, 7, 5]
      bstValue  t                            `shouldBe` Just 4
      (bstLeft  t >>= bstValue             ) `shouldBe` Just 2
      (bstLeft  t >>= bstLeft  >>= bstValue) `shouldBe` Just 1
      (bstLeft  t >>= bstRight >>= bstValue) `shouldBe` Just 3
      (bstRight t >>= bstValue             ) `shouldBe` Just 6
      (bstRight t >>= bstLeft  >>= bstValue) `shouldBe` Just 5
      (bstRight t >>= bstRight >>= bstValue) `shouldBe` Just 7

    it "empty tree to list" $
      length (toList empty) `shouldBe` 0

    it "iterating one element" $
      toList (singleton int4) `shouldBe` [4]

    it "iterating over smaller element" $
      toList (fromList [int4, 2]) `shouldBe` [2, 4]

    it "iterating over larger element" $
      toList (fromList [int4, 5]) `shouldBe` [4, 5]

    it "iterating over complex tree" $
      toList (fromList [int4, 2, 1, 3, 6, 7, 5]) `shouldBe` [1..7]