Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow test with Ints #58

Merged
merged 4 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
Bugfixes:

Other improvements:
- Fix integer overflow error in test for Ints (#58 by @JordanMartinez)

## [v6.0.1](https://github.com/purescript-contrib/purescript-quickcheck-laws/releases/tag/v6.0.1) - 2021-05-06

Expand Down
17 changes: 16 additions & 1 deletion test/Test/Prim/Int.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ module Test.Prim.Int where
import Prelude

import Effect (Effect)
import Test.QuickCheck (class Arbitrary)
import Test.QuickCheck.Gen (chooseInt)
import Test.QuickCheck.Laws (checkLaws)
import Test.QuickCheck.Laws.Data as Data
import Type.Proxy (Proxy(..))

-- | Necessary for the EuclideanRing test
-- | so as to prevent integer overflow when multiplying large integer values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of naming this SmallInt instead, usable to avoid overflows when testing large integer values? It doesn't seem specific to EuclideanRing other than that we're using it in a test for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that #57 is in, this could be refactored to use the gen version and not even need this newtype.

newtype EuclideanRingInt = EuclideanRingInt Int
derive newtype instance eqEuclideanRingInt :: Eq EuclideanRingInt
derive newtype instance semiringEuclideanRingInt :: Semiring EuclideanRingInt
derive newtype instance ringEuclideanRingInt :: Ring EuclideanRingInt
derive newtype instance commutativeRingEuclideanRingInt :: CommutativeRing EuclideanRingInt
derive newtype instance euclideanRingEuclideanRingInt :: EuclideanRing EuclideanRingInt
instance arbitraryEuclideanRingInt :: Arbitrary EuclideanRingInt where
arbitrary = do
EuclideanRingInt <$> chooseInt (-10_000) 10_000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
instance arbitraryEuclideanRingInt :: Arbitrary EuclideanRingInt where
arbitrary = do
EuclideanRingInt <$> chooseInt (-10_000) 10_000
instance arbitraryEuclideanRingInt :: Arbitrary EuclideanRingInt where
arbitrary = EuclideanRingInt <$> chooseInt (-10_000) 10_000

Is there a reason for -10_000 to 10_000 specifically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number.MAX_SAFE_INTEGER is 9007199254740991. I figured 10k was a large enough range to test without being too large to produce the overflow.


checkInt ∷ Effect Unit
checkInt = checkLaws "Int" do
Data.checkEq prxInt
Data.checkOrd prxInt
Data.checkCommutativeRing prxInt
Data.checkSemiring prxInt
Data.checkEuclideanRing prxInt
Data.checkEuclideanRing prxEuclideanRingInt
Data.checkRing prxInt
where
prxInt = Proxy ∷ Proxy Int
prxEuclideanRingInt = Proxy ∷ Proxy EuclideanRingInt