packages feed

bishbosh-0.0.0.1: src-test/BishBosh/Test/QuickCheck/Component/Turn.hs

{-# OPTIONS_GHC -fno-warn-orphans #-}
{-
	Copyright (C) 2018 Dr. Alistair Ward

	This file is part of BishBosh.

	BishBosh is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	BishBosh is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Implements 'Test.QuickCheck.Arbitrary' & defines /QuickCheck/-properties.
-}

module BishBosh.Test.QuickCheck.Component.Turn(
-- * Types
-- ** Type-synonyms
	Turn,
-- * Constants
--	rankValues,
	results
) where

import			BishBosh.Test.QuickCheck.Attribute.LogicalColour()
import			BishBosh.Test.QuickCheck.Attribute.MoveType()
import			BishBosh.Test.QuickCheck.Component.Move()
import			BishBosh.Test.QuickCheck.Component.Piece()
import			Control.Arrow((&&&))
import			Data.Array.IArray((!))
import qualified	BishBosh.Attribute.MoveType			as Attribute.MoveType
import qualified	BishBosh.Attribute.Rank				as Attribute.Rank
import qualified	BishBosh.Attribute.RankValues			as Attribute.RankValues
import qualified	BishBosh.Component.Move				as Component.Move
import qualified	BishBosh.Component.Piece			as Component.Piece
import qualified	BishBosh.Component.QualifiedMove		as Component.QualifiedMove
import qualified	BishBosh.Component.Turn				as Component.Turn
import qualified	BishBosh.Property.Reflectable			as Property.Reflectable
import qualified	BishBosh.Test.QuickCheck.Attribute.RankValues	as Test.QuickCheck.Attribute.RankValues
import qualified	BishBosh.Types					as T
import qualified	Control.Arrow
import qualified	Data.List
import qualified	Data.Maybe
import qualified	Test.QuickCheck
import qualified	ToolShed.Data.List
import qualified	ToolShed.Data.Triple
import qualified	ToolShed.Test.ReversibleIO

-- | Defines a concrete type for testing.
type Turn	= Component.Turn.Turn T.X T.Y

instance (
	Enum	x,
	Enum	y,
	Ord	x,
	Ord	y
 ) => Test.QuickCheck.Arbitrary (Component.Turn.Turn x y) where
--	{-# SPECIALISE instance Test.QuickCheck.Arbitrary Turn #-}
	arbitrary	= do
		moveType	<- Test.QuickCheck.arbitrary
		(move, piece)	<- case moveType of
			Attribute.MoveType.Castle _	-> fmap (
				ToolShed.Data.Triple.getSecond . Data.Maybe.fromJust . Data.List.find (
					(== moveType) . ToolShed.Data.Triple.getFirst
				) . (Component.Move.castlingMovesByLogicalColour !) &&& Component.Piece.mkKing
			 ) Test.QuickCheck.arbitrary {-logicalColour-}
			Attribute.MoveType.EnPassant	-> (,) <$> Test.QuickCheck.arbitrary {-piece. CAVEAT: very lax-} <*> fmap Component.Piece.mkPawn Test.QuickCheck.arbitrary {-logicalColour-}
			_				-> Test.QuickCheck.arbitrary	-- CAVEAT: the move-vector isn't tailored to the rank of piece.

		return {-to Gen-monad-} . Component.Turn.mkTurn (Component.QualifiedMove.mkQualifiedMove move moveType) $ Component.Piece.getRank piece

-- | Distinct rank-values designed for a predictable sort-order.
rankValues :: Test.QuickCheck.Attribute.RankValues.RankValues
rankValues	= Attribute.RankValues.fromAssocs . zip Attribute.Rank.range $ map (/ 10) [1, 5, 3, 4, 9, 0]

-- | The constant test-results for this data-type.
results :: IO [Test.QuickCheck.Result]
results	= sequence [
	let
		f :: Turn -> Test.QuickCheck.Property
		f	= Test.QuickCheck.label "Turn.prop_readPrependedWhiteSpace" . ToolShed.Test.ReversibleIO.readPrependedWhiteSpace
	in Test.QuickCheck.quickCheckWithResult Test.QuickCheck.stdArgs { Test.QuickCheck.maxSuccess = 256 } f,
	let
		f :: String -> Test.QuickCheck.Property
		f garbage	= Test.QuickCheck.label "Turn.prop_read" $ case (reads garbage :: [(Turn, String)]) of
			[_]	-> True
			_	-> True	-- Unless the read-implementation throws an exception.
	in Test.QuickCheck.quickCheckWithResult Test.QuickCheck.stdArgs { Test.QuickCheck.maxSuccess = 256 } f,
	let
		f :: Turn -> String -> Test.QuickCheck.Property
		f turn	= Test.QuickCheck.label "Turn.prop_readTrailingGarbage" . ToolShed.Test.ReversibleIO.readTrailingGarbage (const False) turn
	in Test.QuickCheck.quickCheckWithResult Test.QuickCheck.stdArgs { Test.QuickCheck.maxSuccess = 256 } f,
	let
		f :: Turn -> Test.QuickCheck.Property
		f	= Test.QuickCheck.label "Turn.prop_reflectOnX" . uncurry (==) . (id &&& Property.Reflectable.reflectOnX . Property.Reflectable.reflectOnX)
	in Test.QuickCheck.quickCheckWithResult Test.QuickCheck.stdArgs { Test.QuickCheck.maxSuccess = 64 } f,
	let
		f :: Test.QuickCheck.Attribute.RankValues.RankValues -> [Turn] -> Test.QuickCheck.Property
		f rankValues'	= Test.QuickCheck.label "Turn.prop_compareByMVVLVA/quiet" . uncurry (==) . (
			dropWhile Component.Turn.isCapture . Data.List.sortBy (
				Component.Turn.compareByMVVLVA (`Attribute.RankValues.findRankValue` rankValues')
			) &&& filter (
				not . Component.Turn.isCapture
			)
		 )
	in Test.QuickCheck.quickCheckWithResult Test.QuickCheck.stdArgs { Test.QuickCheck.maxSuccess = 256 } f,
	let
		f :: [Turn] -> Test.QuickCheck.Property
		f = Test.QuickCheck.label "Turn.prop_compareByMVVLVA/MVV" . (
			\ranks -> Data.List.sortOn (
				negate . (`Attribute.RankValues.findRankValue` rankValues) -- Most valuable victim should be first.
			) ranks == ranks
		 ) . map head . Data.List.group . Data.Maybe.mapMaybe (
			Attribute.MoveType.getMaybeImplicitlyTakenRank . Component.QualifiedMove.getMoveType . Component.Turn.getQualifiedMove
		 ) . Data.List.sortBy (
			Component.Turn.compareByMVVLVA (`Attribute.RankValues.findRankValue` rankValues)
		 )
	in Test.QuickCheck.quickCheckWithResult Test.QuickCheck.stdArgs { Test.QuickCheck.maxSuccess = 256 } f,
	let
		f :: [Turn] -> Test.QuickCheck.Property
		f = Test.QuickCheck.label "Turn.prop_compareByMVVLVA/LVA" . all (
			(
				\ranks -> uncurry (++) (
					Control.Arrow.first (
						Data.List.sortOn (`Attribute.RankValues.findRankValue` rankValues)	-- Least valuable aggressor should be first.
					) $ Data.List.partition (/= Attribute.Rank.King) ranks
				) == ranks
			) . map head . Data.List.group . map Component.Turn.getRank {-aggressor's rank-}
		 ) . Data.List.groupBy (
			ToolShed.Data.List.equalityBy $ Attribute.MoveType.getMaybeImplicitlyTakenRank . Component.QualifiedMove.getMoveType . Component.Turn.getQualifiedMove
		 ) . takeWhile Component.Turn.isCapture . Data.List.sortBy (
			Component.Turn.compareByMVVLVA (`Attribute.RankValues.findRankValue` rankValues)
		 )
	in Test.QuickCheck.quickCheckWithResult Test.QuickCheck.stdArgs { Test.QuickCheck.maxSuccess = 512 } f
 ]