File: ShuffleSpec.hs

package info (click to toggle)
haskell-hspec-core 2.11.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: haskell: 8,945; makefile: 5
file content (47 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download | duplicates (2)
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
module Test.Hspec.Core.ShuffleSpec (spec) where

import           Prelude ()
import           Helper

import qualified Test.Hspec.Core.Shuffle as H
import           Test.Hspec.Core.Tree

import           Data.Array.ST
import           Control.Monad.ST
import           Data.STRef
import           System.Random

spec :: Spec
spec = do
  describe "shuffleForest" $ do
    let
      shuffleForest :: Int -> [Tree () Int] -> [Tree () Int]
      shuffleForest seed xs = runST $ do
        gen <- newSTRef (mkStdGen seed)
        H.shuffleForest gen xs

    it "shuffles a forest" $ do
      shuffleForest 2
        [Leaf 1, Leaf 2, Leaf 3] `shouldBe`
        [Leaf 3, Leaf 1, Leaf 2]

    it "recurses into Node" $ do
      shuffleForest 1
        [Node "foo" [Node "bar" [Leaf 1, Leaf 2, Leaf 3]]] `shouldBe`
        [Node "foo" [Node "bar" [Leaf 2, Leaf 3, Leaf 1]]]

    it "recurses into NodeWithCleanup" $ do
      shuffleForest 1
        [NodeWithCleanup Nothing () [NodeWithCleanup Nothing () [Leaf 1, Leaf 2, Leaf 3]]] `shouldBe`
        [NodeWithCleanup Nothing () [NodeWithCleanup Nothing () [Leaf 2, Leaf 3, Leaf 1]]]

  describe "shuffle" $ do
    it "shuffles a list" $ do
      runST $ do
        gen <- newSTRef (mkStdGen 2)
        H.shuffle gen [1, 2, 3 :: Int]
      `shouldBe` [3, 1, 2]

  describe "mkArray" $ do
    it "creates an STArray from a list" $ do
      runST (H.mkArray [1, 2, 3 :: Int] >>= getElems) `shouldBe` [1, 2, 3]