使用类型类约束快速检查并报告生成的值?

问题描述 投票:2回答:1

我正在尝试为国际象棋游戏进行基于属性的测试。我已经设置了以下类型类

class Monad m => HasCheck m where                                                   
    isCollision :: Coord -> m Bool                                                  

检查给定坐标是否包含碰撞或越界。

现在我有一个函数可以为骑士生成允许操作的移动集,如下所示

collisionKnightRule :: HasCheck m => Coord -> m (Set Coord)                      
collisionKnightRule =                                                            
    Set.filterM isCollision . knightMoveSet                                      


-- | Set of all moves, legal or not                                              
knightMoveSet :: Coord -> Set Coord                                              
knightMoveSet (x,y) =                                                            
    Set.fromList                                                                 
        [ (x+2,y-1),(x+2,y+1),(x-2,y-1),(x-2,y+1)                                
        , (x+1,y-2),(x+1,y+2),(x-1,y-2),(x-1,y+2)                                
        ]                                                                        



knightMoves :: HasCheck m => Coord -> m (Set Coord)                              
knightMoves pos =                                                                
    do  let moveSet =                                                            
                knightMoveSet pos                                                
        invalidMoves <- collisionKnightRule pos                                        
        return $ Set.difference moveSet invalidMoves                             

以及任意坐标的HasCheck类的实例

instance HasCheck Gen where                                                      
    isCollision _ =                                                              
         Quickcheck.arbitrary                                                    

然后测试这个我想确保生成的移动集是所有可能移动的适当子集。

knightSetProperty :: Piece.HasCheck Gen                                          
    => (Int,Int)                                                                 
    -> Gen Bool                                                                  
knightSetProperty position =                                                     
    do  moves <- Piece.knightMoves position                                      
        return $ moves `Set.isProperSubsetOf` (Piece.knightMoveSet position)

-- ... later on

it "Knight ruleset is subset" $                                          
            quickCheck knightSetProperty

当然这会失败,因为它可能是骑士无法移动到任何地方,这意味着它不是一个合适的子集,而是相同的集合。但是,报告的错误并不是特别有用

*** Failed! Falsifiable (after 14 tests and 3 shrinks):  
(0,0)

这是因为quickcheck不会报告isCollision的生成值。因此我想知道,我怎样才能使quickCheck报告生成的isCollision值?

haskell testing typeclass quickcheck
1个回答
0
投票

好吧所以我觉得这应该可以用另一种方式解决。然而,我做了以下解决方案,其工作灵感来自handler pattern

我将HasCheck类型类更改为记录,如下所示:

data Handle = MakeHandle                                                                   
    { isCollision   :: Coord -> Bool                                                   
    }      

然后重构所有代码以使用句柄而不是HasCheck。

collisionKnightRule :: Handle -> Coord -> (Set Coord)                            
collisionKnightRule handle =                                                     
    Set.filter (isCollision handle) . knightMoveSet                              


-- | Set of all moves, legal or not                                              
knightMoveSet :: Coord -> Set Coord                                              
knightMoveSet (x,y) =                                                            
    Set.fromList                                                                 
        [ (x+2,y-1),(x+2,y+1),(x-2,y-1),(x-2,y+1)                                
        , (x+1,y-2),(x+1,y+2),(x-1,y-2),(x-1,y+2)                                
        ]                                                                        


-- | Set of illegal moves                                                        
knightRuleSet :: Handle -> Coord -> (Set Coord)                                  
knightRuleSet =                                                                  
    collisionKnightRule                                                          


knightMoves :: Handle -> Coord -> (Set Coord)                                    
knightMoves handle pos =                                                         
    let                                                                          
        moveSet =                                                                
            knightMoveSet pos                                                    

        invalidMoves =                                                           
            knightRuleSet handle pos                                             
    in                                                                           
        Set.difference moveSet invalidMoves

这样做的缺点是我担心对于有状态代码,在传递过时的句柄时,很容易引入错误,I.E。拥有多种真理来源。一个优点是,对于刚接触Haskell的人来说,这可能更容易理解。我们现在可以使用Quickcheck的Function类型类来模拟函数,并将它们作为参数传递给makeHandler:

knightSetProperty ::                                                                 
    Fun (Int,Int) Bool                                                               
    -> (Int,Int)                                                                     
    -> Gen Bool                                                                      
knightSetProperty (Fun _ isCollision) position =                
    let                                                                              
        handler = 
            Piece.MakeHandle isCollision                           
        moveSet =                                                                      
            Piece.knightMoves handler position                                       
    in                                                                               
        return $ moveSet `Set.isProperSubsetOf` (Piece.knightMoveSet position)

现在这种情况正确地失败,反例如下:

*** Failed! Falsifiable (after 53 tests and 74 shrinks):     
{_->False}
(0,0)
© www.soinside.com 2019 - 2024. All rights reserved.