{-# LANGUAGE TemplateHaskell #-} -- vim: sw=2 ts=2 et ai: module Test where import Data.Array import Data.Attoparsec.ByteString.Char8 import Data.ByteString.Char8 (pack) import Data.Either import Data.List import Control.Monad import Test.QuickCheck import Chess import Chess.FEN import Chess.PGN import ArbitraryMove atAnyState :: (Board -> Bool) -> PGN -> Bool atAnyState ok pgn = and [ok b | Right b <- seqList [moveSAN m | m <- moves pgn] $ Right defaultBoard] where seqList :: [r -> Either e r] -> Either e r -> [Either e r] seqList (f:fs) (Right io) = f io:seqList fs (f io) seqList _ (Left _) = [] seqList [] _ = [] atAnyTwoStates :: (Board -> Board -> Bool) -> PGN -> Bool atAnyTwoStates ok pgn = and [ok b1 b2 | Right b1 <- seqList [moveSAN m | m <- moves pgn] | Right b2 <- seqList [moveSAN m | m <- moves pgn] $ Right defaultBoard] where seqList :: [r -> Either e r] -> Either e r -> [Either e r] seqList (f:fs) (Right io) = f io:seqList fs (f io) seqList _ (Left _) = [] seqList [] _ = [] prop_checkPGN :: PGN -> Bool prop_checkPGN pgn | isLeft parsed = False | length parsed' /= 1 = False | otherwise = pgn == ((head parsed') {initialPosition=Just defaultBoard}) where parsed = parseOnly pgnParser (pack $ pgnToString pgn) (Right parsed') = parsed pgnToString :: PGN -> String pgnToString pgn = makePGN (event pgn) (site pgn) (date pgn) (Chess.PGN.round pgn) (whitePlayer pgn) (blackPlayer pgn) (resultString $ result pgn) ("1. " ++ intercalate " 1. " (moves pgn) ++ " " ++ resultString (result pgn)) where resultString :: Maybe GameResult -> String resultString Nothing = "*" resultString (Just WhiteWon) = "1-0" resultString (Just BlackWon) = "0-1" resultString (Just Draw) = "1/2-1/2" makePGN :: String -> String -> String -> String -> String -> String -> String -> String -> String makePGN event site date round white black result moves = concatMap (uncurry tagPair) [ ("Event", event) , ("Site", site) , ("Date", date) , ("Round", round) , ("White", white) , ("Black", black) , ("Result", result) ] ++ "\r\n" ++ moves where tagPair :: String -> String -> String tagPair tag val = "[" ++ tag ++ " \"" ++ val ++ "\"]\r\n" prop_only_2_kings :: PGN -> Bool prop_only_2_kings = atAnyState (\b -> length (pieceCoords Black b King) == 1 && length (pieceCoords White b King) == 1) where pieceCoords clr brd piece = [i | (i, pc) <- (assocs $ board brd), pc == Just (Piece clr piece)] prop_number_of_pieces :: PGN -> Bool prop_number_of_pieces = atAnyTwoStates (\b1 b2 -> length (pieceCoords Nothing Nothing b1) == length (pieceCoords Nothing Nothing b2) || length (pieceCoords Nothing Nothing b1) - 1 == length (pieceCoords Nothing Nothing b2) ) return [] main = $forAllProperties (quickCheckWithResult (stdArgs {maxSuccess=10000}))