summaryrefslogtreecommitdiff
path: root/8puzzle/PuzzleTest.hs
blob: 0044608567a0fdb287c33f3dc89716bc76802499 (plain)
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
import Test.Framework (defaultMain, testGroup)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.Framework.Providers.HUnit
import Test.HUnit
import Test.QuickCheck
import Test.QuickCheck.Gen
import qualified Data.Vector as V

import Puzzle hiding (main)

instance Arbitrary Puzzle where
    arbitrary = do
      size <- choose (2, 4)
      board <- shuffle [0 .. (size*size - 1)]
      return $ Puzzle size (V.fromList board) False

tests = [
  testGroup "Board"
    [ testProperty "neighbors have distance of 1" prop_neighborDistance,
      testProperty "can solve puzzle" prop_canSolvePuzzle,
      testCase "basic distance" test_distance
    ]
  ]

test_distance = distance puzzle @?= 4
    where puzzle = Puzzle 2 (V.fromList [1, 3, 2, 0]) False

prop_neighborDistance p = all validNeighbor (neighbors p)
    where validNeighbor n = abs (distance p - distance n) == 1

prop_canSolvePuzzle = not . null . solve

example = Puzzle 2 (V.fromList [1, 3, 2, 0]) False

main = do
  print example
  putStrLn "Neighbors:"
  mapM_ print (neighbors example)
  putStrLn ""
  defaultMain tests