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 best solutions below
Related Questions in HASKELL
- Cabal sandbox is using a global dependency. Could not resolve
- Haskell lens: let binding of Traversal'
- How can I parse fixed-length, non-delimited integers with attoparsec?
- Pipeline-like operation using TChan
- compile-time vs. run-time cost of Hamlet templates
- Date-time package in haskell - error in the current one, can't find an analog
- How does one debug infinite recursion in Haskell?
- Force GHC using local files
- List with random numbers in Haskell
- Changes in other elements based on listbox selections in threepenny-gui
- Multithreading and gtk2hs
- Operator section for applicative with <$> and <*>
- Unable to create a custom header to use it in "withManager"
- How do I reuse an intermediate value in chain of Haskell Either binds?
- Haskell, Tree problems
Related Questions in QUICKCHECK
- Uniqueness and other restrictions for Arbitrary in QuickCheck
- How can I use multi-line input with QuickCheck in doctest?
- Test several functions with the same list of value with quickCheck
- Defining a property in haskel locally
- State machine based testing in Haskell QuickCheck
- How to test Semigroup law for this data type?
- Why does my implementation of SVG arc conversion not pass QuickCheck?
- How can I specialize a type in a Frege QuickCheck?
- Quickcheck, defining Arbitrary instances using a function whose result depends on its arguments
- QuickCheck Tests for Custom Type
- How can I constraint QuickCheck parameters, e.g. only use non-negative ints?
- Haskell quickCheck property for half Evens
- QuickCheck giving up investigating a recursive data structure (rose tree.)
- test-framework/quickcheck/cabal: Passing options to testfunction with 'cabal test'
- test isolation between pytest-hypothesis runs
Related Questions in PROPERTY-TESTING
- Python property testing with timeout
- FsCheck: Override generator for a type, but only in the context of a single parent generator
- How to share test interfaces between Go packages?
- How to use QuickCheck in Hspec tests?
- How to report all test case input for scalacheck.Prop.forAll property test?
- Generating tuples containing Long for Vavr Property Checking
- Are gopter property tests safe for parallel use?
- Should property tests run with unit tests when using the RGR methodology?
- How would I perform property-based testing on a card game's Deal function?
- Create an Arbitrary instance for a case class that holds a `Numeric` in ScalaCheck?
- scalacheck: define a generator for an infinite stream with some dependence on previous elements
- How I can force evaluation of property tester manually in eclipse 4?
- Python Hypothesis - building strategy once for many tests?
- How are properties evaluated in eclipse?
- How write a property test for particular list content
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)
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?
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.