简体   繁体   English

如何编写不模仿函数实现的QuickCheck测试?

[英]How do I write QuickCheck tests that don't mimic implementation of the function?

I am using Haskell and QuickCheck to write a test for the following function: 我正在使用Haskell和QuickCheck为以下函数编写测试:

{-| Given a list of points and a direction, find the point furthest
along in that direction. -}

fn :: (Eq a, Ord a, DotProd a) => [a] -> a -> a
fn pnts dir = pnts !! index
    where index = fromJust $ elemIndex (maximum dotproducts) dotproducts
          dotproducts = map (dot dir) pnts

I believe this implementation to be correct, since it's not too complex of a function. 我相信这个实现是正确的,因为它不是太复杂的功能。 But, I want to use QuickCheck to test it for some edge cases. 但是,我想使用QuickCheck来测试一些边缘情况。

However, I run into the problem that, when I define my QuickCheck tests, they are identical to the function I am testing. 但是,我遇到的问题是,当我定义我的QuickCheck测试时,它们与我正在测试的功能相同。

How do I write a test in QuickCheck that tests the purpose of a function without repeating its implementation? 如何在QuickCheck中编写一个测试功能目的而不重复实现的测试?

How do I write a test in QuickCheck that tests the purpose of a function without repeating its implementation? 如何在QuickCheck中编写一个测试功能目的而不重复实现的测试?

First , note that sometimes, a quickcheck property that states that a function behaves according to its current implementation is not completely worthless. 首先 ,请注意,有时,一个表示函数根据其当前实现行为的quickcheck属性并非完全没有价值。 You can use it for regression tests if you ever change the implementation. 如果您更改了实现,则可以将其用于回归测试。 For example, if you optimize the definition of your fn to use clever data structures, the Quickcheck property based on the old, more straight-forward implementation might prove helpful. 例如,如果优化fn的定义以使用聪明的数据结构,则基于旧的,更直接的实现的Quickcheck属性可能会有所帮助。

Second , you often want your Quickcheck properties to check high-level and declarative properties of your functions, while the implementation is usually lower-level and more directly executable. 其次 ,您经常希望Quickcheck属性检查函数的高级和声明性属性,而实现通常是较低级别且可直接执行。 In this case, you could specify such properties as: 在这种情况下,您可以指定以下属性:

  • forall lists of points ps and directions d, the point fn ps d is in the list ps. 在点ps和方向d的forall列表中,点fn ps d在列表ps中。

  • forall lists of points ps, directions d, and forall points p in ps, the point p is not further along in the direction d than the point fn ps d . 以ps为单位的点列表ps,方向d和forall点p,点p在方向d上不比点fn ps d更远。

  • forall points p and forall directions d, fn [p, origin] d is p. forall p和forall方向d, fn [p, origin] d是p。

I'm not totally sure about the underlying geometry, so my examples might be stupid. 我不完全确定基础几何,所以我的例子可能是愚蠢的。 But I hope the examples convey the general idea: The quickcheck properties could check for properties of the specification "being further along in a direction" without mentioning the particular algorithm. 但是我希望这些例子能够传达一般的想法:快速检查属性可以检查规范的属性“在一个方向上更进一步”,而不提及特定的算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM