You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.7 KiB

9 years ago
9 years ago
  1. import Datatypes
  2. import VM
  3. import qualified Datatypes.Unit as Unit
  4. import Data.Hashable (hash, Hashable(..))
  5. import Data.Set (Set)
  6. import qualified Data.Set as Set
  7. import Test.QuickCheck
  8. test :: (Cell -> Cell) -> [Bool]
  9. test f = [f (0,3) == (2,5), f (1,3) == (3, 6), f (2,3) == (3,7), f (3, 3) == (4,8), f (2, 4) == (2,7)]
  10. test2 :: (Cell -> Cell) -> [Bool]
  11. test2 f = [f (2,5) == (3,6), f (3,6) == (2, 7), f (2,7) == (1,7), f (1, 7) == (1,6), f (1, 6) == (1,5), f (1, 5) == (2, 5)]
  12. deepCheck p = quickCheckWith (stdArgs { maxSuccess = 100000 }) p
  13. testTutto = do
  14. deepCheck (\pivot cell -> let r2 = rotateCell pivot in (r2 . r2 . r2 . r2 . r2 . r2) cell == cell)
  15. deepCheck (\pivot cell -> cell == counterRotateCell pivot (rotateCell pivot cell))
  16. deepCheck (\pivot cell -> cell == counterRotateCell2 pivot (rotateCell pivot cell))
  17. checkForNothing::[Maybe(Cell, Fallimento)] -> Maybe(Cell, Fallimento)
  18. checkForNothing (Nothing:xs) = checkForNothing xs
  19. checkForNothing ((Just v):xs) = Just v
  20. checkForNothing [] = Nothing
  21. testTutteRotazioni = checkForNothing [
  22. testDoppieRotazioni (2,3) [(2,0), (4,1), (5,4), (3,6), (0,5), (0,2), (2,0)],
  23. testDoppieRotazioni (2,4) [(2,1), (4,3), (4,6), (1,7), (-1,5), (0,2), (2,1)],
  24. testDoppieRotazioni (1,5) [(1,3), (3,4), (3,6), (1,7), (0,6), (0,4), (1,3)],
  25. testControRotazioni (5, 5) [(6,1), (8,4), (7, 8), (4,9), (1,6), (2,2), (5,5)]
  26. ]
  27. testCentering = deepCheck (\up um um1 -> let b = Board 5 10 (Set.fromList [])
  28. u = Unit up (Set.fromList (um1:um))
  29. c = Unit.centeredIn u b
  30. bw = 5
  31. xCoords = Set.map (\(x, y) -> x) (Unit.members c)
  32. yCoords = Set.map (\(x, y) -> y) (Unit.members c)
  33. unitLeft = Set.findMin xCoords
  34. unitRight = Set.findMax xCoords
  35. unitTop = Set.findMin yCoords
  36. deltaLeft = unitLeft
  37. deltaRight = bw - unitRight - 1
  38. in (deltaLeft == deltaRight || (deltaRight - deltaLeft) == 1) && unitTop == 0)
  39. testGame = let board = Board 5 10 (Set.fromList [])
  40. unit = Unit (2, 8) (Set.fromList [(2, 8)])
  41. game = Game board [unit] (Set.fromList [hash unit])
  42. in game
  43. data Fallimento = Rotazione | ControRotazione deriving Show
  44. testDoppieRotazioni :: Cell -> [Cell] -> Maybe (Cell, Fallimento)
  45. testDoppieRotazioni pivot cells =
  46. case testRotazioni pivot cells of
  47. Just v -> Just (v, Rotazione)
  48. Nothing -> case testControRotazioni pivot (reverse cells) of
  49. Just w -> Just (w, ControRotazione)
  50. Nothing -> Nothing
  51. -- restituisce eventualmente il primo valore per cui fallisce
  52. testRotazioni :: Cell -> [Cell] -> Maybe Cell
  53. testRotazioni _ [] = Nothing
  54. testRotazioni _ [x] = Nothing
  55. testRotazioni pivot (x:y:xs) = if rotateCell pivot x == y
  56. then testRotazioni pivot (y:xs)
  57. else Just x
  58. -- restituisce eventualmente il primo valore per cui fallisce
  59. testControRotazioni :: Cell -> [Cell] -> Maybe Cell
  60. testControRotazioni _ [] = Nothing
  61. testControRotazioni _ [x] = Nothing
  62. testControRotazioni pivot (x:y:xs) = if counterRotateCell pivot x == y
  63. then testControRotazioni pivot (y:xs)
  64. else Just x