I have come across a few instances in my testing with QuickCheck when it would have simplified things to write my own modifiers in some cases, but I'm not exactly sure how one would do this. In particular, it would be helpful to know how to write a modifier for generators of lists and of numerics (such as Int). I'm aware of NonEmptyList, and Positive and NonNegative, that are already in the library, but in some instances it would have made my tests clearer if I could have specified something like a list that is not only NonEmpty, but also NonSingleton (so, it has at least 2 elements), or an Int that is greater than 1, not just NonZero or Positive, or an Int(egral) that is even/odd, etc.
How do you write a new modifier in QuickCheck
179 views Asked by josiah At
1
There are 1 answers
Related Questions in HASKELL
- Typeclass projections as inheritance
- How to generate all possible matrices given a number n in Haskell
- Is there a way to get `cabal` to detect changes to non-Haskell source files?
- How to have fixed options using Option.Applicative in haskell?
- How can I create a thread in Haskell that will restart if it gets killed due to any reason?
- Automatic Jacobian matrix in Haskell
- Haskell writing to named pipe unexpectedly fails with `openFile: does not exist (No such device or address)`
- Why does Enum require to implement toEnum and fromEnum, if that's not enough for types larger than Int?
- Non-exhaustive patterns in function compress
- How to get terms names of GADT in Template Haskell?
- Implementing eval() function with Happy parser generator
- How to count the occurences of every element in a list in Haskell fast?
- In Haskell, what does `Con Int` mean?
- Extract a Maybe from a heterogeneous collection
- Haskell, Stack, importing module shows error "Module not found"
Related Questions in QUICKCHECK
- cabal test is inconsistent with its QuickCheck log
- Printing an expression using Dump library fails
- How can I pinpoint the specific location where property tests fail when using QuickCheck more efficiently?
- Haskell: Using quickCheckAll with a size constraint (e.g. quickCheckWith (stdArgs {maxSize = n}))
- Haskell - QuickCheck: Checking Equality in TestTree
- Model based property failed after few tests
- How is arbitrary distributed for Int? Why is it limited by so small values?
- How to write a Quickcheck property for ANSI escaped coded string parser?
- Using mocks in unit testing in Haskell?
- Issues with installing QuickCheck
- How to constraint quickcheck test argument to be non-empty in Rust
- Is there an easy way to integrate QuickCheck (Quiviq) with Erlang Eunit and if so how?
- How to test Monad instance for custom StateT?
- QuickCheck returns "0 tests" when checking Applicative homomorphism property (Binary Tree)
- Quickcheck Applicative homomorphism law for Binary Tree
Related Questions in PROPERTY-TESTING
- FsCheck: Override generator for a type, but only in the context of a single parent generator
- Are gopter property tests safe for parallel use?
- How do you write a new modifier in QuickCheck
- Python property testing with timeout
- How to report all test case input for scalacheck.Prop.forAll property test?
- How to use QuickCheck in Hspec tests?
- scalacheck: define a generator for an infinite stream with some dependence on previous elements
- How write a property test for particular list content
- Generating tuples containing Long for Vavr Property Checking
- How to share test interfaces between Go packages?
- Python Hypothesis - building strategy once for many tests?
- How I can force evaluation of property tester manually in eclipse 4?
- How are properties evaluated in eclipse?
- Create an Arbitrary instance for a case class that holds a `Numeric` in ScalaCheck?
- pure E4 usage activeWhen Expression
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
There's plenty of way in which you can do that. Here's some examples.
Combinator function
You can write a combinator as a function. Here's one that generates non-singleton lists from any
Gen a:This has the same type as the built-in
listOffunction, and can be used in the same way:Here I've taken advantage of
Gen abeing aMonadso that I could write both the function and the property withdonotation, but you can also write it using monadic combinators if you so prefer.The function simply generates two values,
x1andx2, as well as a listxsof arbitrary size (which can be empty), and creates a list of all three. Sincex1andx2are guaranteed to be single values, the resulting list will have at least those two values.Filtering
Sometimes you just want to throw away a small subset of generated values. You can to that with the built-in
==>combinator, here used directly in a property:While this property is tautological, it demonstrates that the predicate you place to the left of
==>ensures that whatever runs on the right-hand side of==>has passed the predicate.Existing monadic combinators
Since
Gen ais aMonadinstance, you can also use existingMonad,Applicative, andFunctorcombinators. Here's one that turns any number inside of anyFunctorinto an even number:Notice that this works for any
Functor f, not just forGen a. Since, however,Gen ais aFunctor, you can still useevenInt:The
arbitraryfunction call here creates an unconstrainedIntegervalue.evenIntthen makes it even by multiplying it by two.Arbitrary newtypes
You can also use
newtypeto create your own data containers, and then make themArbitraryinstances:This also enables you to implement
shrink, if you need it.You can use the
newtypein a property like this:The
Arbitraryinstance usesarbitraryfor the typeato generate an unconstrained valuei, then doubles it and adds one, thereby ensuring that the value is odd.Take a look at the QuickCheck documentation for many more built-in combinators. I particularly find
choose,elements,oneof, andsuchThatuseful for expressing additional constraints.