nick8325 / nick8325/quickcheck

Make Arbitrary instance of Integer able to generate large numbers out of the box

Open
#213 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
790
Forks
130
Avg merge
16h 41m
Merged PRs (30d)
2

Description

There is a slight problem with Arbitrary instance of Integer, namely that it won't generate large numbers (by which I mean numbers that don't fit into Int64) by default, which makes it impossible to test all the code paths out of the box as it is not uncommon for the code that deals with Integers distinguish between small and large numbers (which makes sense even given its constructors: https://hackage.haskell.org/package/integer-gmp-1.0.1.0/docs/src/GHC.Integer.Type.html#Integer).

Can current instance be replaced with something along the lines of

instance Arbitrary Integer where
    arbitrary = sized $ \sz -> do
        n <- choose (1, sz)
        sign <- arbitrary
        (if sign then negate else id) . foldr f 0
            <$> replicateM n arbitrary
      where
        f :: Word8 -> Integer -> Integer
        f w acc = (acc `shiftL` 8) + fromIntegral w

?

As of now defining a newtype LargeInteger = LargeInteger Integer with appropriate Arbitrary instance is somewhat of a workaround, but there are two problems with that:

  1. You need to remember to do that.
  2. Sometimes it's impossible/inconvenient to use it, e.g. when we're testing a data type with fields of type Integer (or better yet, where Integer is nested somewhere deep within tested data type).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the current Arbitrary Integer instance and compare its behavior with the proposed size-based generation. Add coverage demonstrating that values larger than Int64 can be generated by default, then run the relevant QuickCheck tests and confirm existing Integer generation remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.