quickcheck

Find the value that failed for quickcheck

感情迁移 提交于 2019-12-10 01:48:17
问题 When a value fails a QuickCheck'd test, I'd like to use it for debugging. Is there any way I can do something like: let failValue = quickCheck' myTest in someStuff failValue If my data was read able then I could probably hack some way to get it in from IO, but it's not. 回答1: I couldn't find anything in the QuickCheck API to do this in a nice way, but here's something I hacked together using the monadic QuickCheck API. It intercepts and logs the inputs to your property in an IORef , and

How to tell QuickCheck to generate only valid list indices for a parameter?

≡放荡痞女 提交于 2019-12-10 00:58:25
问题 Say I want to write some unit tests for the (!!) function. my_prop xs n = ... I want to restrict n to only valid indexes and I know I could do something like my_prop xs n = (not.null) (drop n xs) ==> ... But this makes it so that the vast majority of the generated cases are invalid and get thrown away. Is there a way I can set things up so that QuickCheck generates the xs list first and uses its value to generate only valid cases of n ? 回答1: Using forAll, you can specify a generator for n

Haskell quickcheck - how to generate only printable strings

吃可爱长大的小学妹 提交于 2019-12-09 05:07:24
问题 I have a set of simple demo programs which encode/decode strings, and want to generate some quickCheck tests for them, but to limit the tests to printable strings only. Using a guard is too slow and fails because of too many generated and rejected test cases, so I want to create a safe generator for this domain. The references to this that I have seen say to either (1) define one's own Arbitrary instance for Char and use that to generate only printable characters for strings, or (2) to have

fscheck doesn't generate random enough data

巧了我就是萌 提交于 2019-12-08 22:13:07
问题 I'm playing with FsCheck so I have this implementation: let add a b = if a > 100 then failwith "nasty bug" else a + b ...and this FsCheck based test: fun (a:int) -> (add a 0) = a |> Check.QuickThrowOnFailure and the test never fails. My guess is that the 100 values produced by the random generator are never bigger than 100. Shouldn't the values be more "random"? 回答1: When you use Check.QuickThrowOnFailure , it uses the configuration Config.QuickThrowOnFailure , which has these values: >

Junit-Quickcheck: Generate String matching a pattern

浪尽此生 提交于 2019-12-07 07:36:44
问题 I am using pholser's port. I have to generate strings matching a given pattern like \[a-zA-Z0-9\\.\\-\\\\;\\:\\_\\@\\[\\]\\^/\\|\\}\\{]* Length 40 . I extend the Generator class as: public class InputGenerator extends Generator<TestData> {...} It overloads a function: publicTestData generate(SourceOfRandomness random, GenerationStatus status) {...} Now, random has functions like nextDouble(), nextInt() but there is nothing for strings! How can I generate random strings matching the above

Haskell: QuickCheck property fails tests using implications

ε祈祈猫儿з 提交于 2019-12-07 03:02:33
问题 I've got the following property I want to test using quickcheck: prop_zip xs ys = length xs == length ys ==> unzip (zip xs ys) == (xs,ys) Eventhough it seems to be logically right according to the definition of zip and unzip, that this property should be correct fo lists of the same length, the quickcheck ends with: *** Gave up! Passed only 49 tests. Thanks in advance for any hint or advice! 回答1: Preconditions that are difficult to meet by generating random test cases are often a bad idea in

How do I emulate Lisp (apply) or (curry) in Rust?

我怕爱的太早我们不能终老 提交于 2019-12-07 01:32:40
问题 I'm porting QuickCheck to Rust, and I've written everything except for_all as I'm not sure what the type signature should be. I know that in general, for_all will accept a property lambda and a collection of generator lambdas. It will evaluate the generators in order to create a random test case to give the property as input. It should print +++ OK, passed 100 tests. if the property returns true, otherwise, it should print *** Failed! and print the offending test case values. 回答1: In Rust,

Generically derive Arbitrary for massive algebraic data types?

房东的猫 提交于 2019-12-06 21:01:34
问题 I've got a protocol that I've typed like so: data ProtocolPacket = Packet1 Word8 Text Int8 | Packet2 Text | Packet3 Int Text Text Text Text | Packet4 Int Double Double Double Int16 Int16 Int16 ... deriving (Show,Eq) In addition, I've implemented serialization/deserialization code for each packet. Naturally, I would like to test this protocol in Quickcheck and make sure that serializing and deserializing any packet for any combination of inputs will give me back exactly what I put in. So I go

Arbitrary instance for generating unbiased graphs for quickcheck

我只是一个虾纸丫 提交于 2019-12-06 12:07:09
问题 module Main where import Test.QuickCheck import Data.Set as Set data Edge v = Edge {source :: v, target :: v} deriving (Show,Eq,Ord) data Graph v = Graph {nodes :: Set v, edges :: Set (Edge v)} deriving Show instance Arbitrary v => Int-> Arbitrary (Edge v) where arbitrary = sized aux where aux n = do s <- arbitrary t <- arbitrary `suchThat` (/= s) return $ Edge {source = s, target = t} instance (Ord v, Arbitrary v) => Arbitrary (Graph v) where arbitrary = aux `suchThat` isValid where aux = do

How do I test this applicative instance with checkers? (No instance for CoArbitrary (Validation e0 [Char]))

拟墨画扇 提交于 2019-12-06 05:23:27
问题 Checkers is a library for reusable QuickCheck properties, particularly for standard type classes How do I write a checkers instance to test whether my applicative instance of Validation is valid? import Test.QuickCheck import Test.QuickCheck.Checkers import Test.QuickCheck.Classes import Control.Applicative import Data.Monoid data Validation e a = Error e | Scss a deriving (Eq, Show) instance Functor (Validation e) where fmap _ (Error e) = Error e fmap f (Scss a) = Scss $ f a instance Monoid