summaryrefslogtreecommitdiff
path: root/8puzzle/PuzzleTest.hs
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2016-03-27 23:57:50 +0200
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2016-03-28 00:48:57 +0200
commitbb67830de2f7fbaf2b95245cbb37f09d841f6790 (patch)
treeefbea50b980a6e3dcec1b1e77ae35c432128ca53 /8puzzle/PuzzleTest.hs
parent475872efcb9bc8496cedcbf4f978fab44402c046 (diff)
8 puzzle solver in haskell.
Diffstat (limited to '8puzzle/PuzzleTest.hs')
-rw-r--r--8puzzle/PuzzleTest.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/8puzzle/PuzzleTest.hs b/8puzzle/PuzzleTest.hs
new file mode 100644
index 0000000..cbd88af
--- /dev/null
+++ b/8puzzle/PuzzleTest.hs
@@ -0,0 +1,37 @@
+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 (1, 10)
+ board <- shuffle [0 .. (size*size - 1)]
+ return $ Puzzle size (V.fromList board) False
+
+tests = [
+ testGroup "Board"
+ [ testProperty "neighbors have distance of 1" prop_neighborDistance,
+ 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
+
+example = Puzzle 2 (V.fromList [1, 3, 2, 0]) False
+
+main = do
+ print example
+ putStrLn "Neighbors:"
+ mapM_ print (neighbors example)
+ putStrLn ""
+ defaultMain tests