Skip to content

Kwxm/test/extra integer property tests #7134

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

kwxm
Copy link
Contributor

@kwxm kwxm commented Jun 5, 2025

Fixes 7068.

This GHC bug and the ones revealed by our property tests for expModInteger made me a bit nervous about the integer arthimetic and comparison builtins, which are some of the earliest ones and were a bit lacking in tests. I've added some property tests that might help to make us a bit more confident.

@kwxm kwxm added Test Builtins No Changelog Required Add this to skip the Changelog Check labels Jun 5, 2025
@kwxm kwxm temporarily deployed to github-pages June 5, 2025 19:10 — with GitHub Actions Inactive
@kwxm kwxm requested review from Unisay and effectfully June 5, 2025 19:10
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is ExpModInteger.hs, but I think I used mv instead of git mv and GitHub doesn't realise that it's the same file (tidied up and reformatted a bit)..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests here need to be quite careful about thier inputs, so I've used arbitrary explicitly and filtered its values. The other tests use more standard generators.

Copy link
Contributor

github-actions bot commented Jun 5, 2025

PR Preview Action v1.6.1

🚀 View preview at
https://IntersectMBO.github.io/plutus/pr-preview/pr-7134/

Built to branch gh-pages at 2025-06-20 22:03 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

import PlutusCore.MkPlc (builtin, mkIterAppNoAnn, mkTyBuiltin, tyInst)

import Evaluation.Builtins.Common

Copy link
Contributor Author

Choose a reason for hiding this comment

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

More utility functions for creating PLC terms. I'm probably reinventing the wheel yet again here.

test_integer_div_mod_properties :: TestTree
test_integer_div_mod_properties =
testGroup "Property tests for divideInteger and modInteger"
[ testProp "divideInteger _ 0 always fails" prop_div_0_fails
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure whether to put the test descriptions here or at the top of the tests themselves. This seems a bit more compact, but maybe putting the descriptions in the tests would have been better from a documentation point of view.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say it's ok either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests here are very similar to those for divideInteger and modInteger, but not identical because there are significant differences in behaviour for negative inputs.

@kwxm kwxm temporarily deployed to github-pages June 5, 2025 19:21 — with GitHub Actions Inactive
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}

{- | Property tests for the `addInteger`, `subtractInteger`, and `multiplyInteger` builtins -}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests aren't very exciting, but I felt obliged to add something.

Copy link
Contributor

@effectfully effectfully left a comment

Choose a reason for hiding this comment

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

Sorry for the overly late review.

Great tests, but I think coverage is unfortunate due to QuickCheck being silly.

test_integer_div_mod_properties :: TestTree
test_integer_div_mod_properties =
testGroup "Property tests for divideInteger and modInteger"
[ testProp "divideInteger _ 0 always fails" prop_div_0_fails
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say it's ok either way.

testProp s p = testProperty s $ withMaxSuccess numberOfTests p

-- `divideInteger _ 0` always fails.
prop_div_0_fails :: Integer -> Property
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you aware that QuickCheck only generates Integers within the [-size, size] (or something) range with size typically being at most 100? E.g. sample sets the size to 20 or 30 or something:

-- >>> import Test.QuickCheck
-- >>> sample (arbitrary :: Gen [[Integer]])
-- []
-- []
-- [[-1],[],[-1]]
-- [[]]
-- [[-5],[4]]
-- [[7,5,8,4],[3,-6,-10,9,2,-3,-10,7,-5],[-8,8,0],[0,2,-2,-10],[-6,-8,-1,-3,9,-5,-9,2,-1,-1],[-4,1,-2,9,-6,9,-9,8,-8,6]]
-- [[0],[-6,-1,12,9,-6,-5,-9,-5,-12,7],[5,12,-6,4],[8,-6,3,-3,12,-10,-12,12,-2],[-1,12],[-5],[5,-9,0],[2,-7,11,-12,12],[-10,-3,-7,12,11,11,10,5,0,2],[11,-5,2,-5,12,-8,-6,0,8],[-7],[4,3,9,-12,6,11,-8,1,8,-10]]
-- [[-13,-5,-2,11,2,2,13],[11,10,-5,-6,-12,-1,12,-7,9],[-8,-9,-12,-8,6,14,5,-14,4,-4,-9],[13,10,-10,-14,9,-2,-10,-9,11,10,5,14],[14,1],[4,-11,13,-7,9,-9,4,11,-14],[-8,-3,-12,6],[12,-14,0,2,0,10,9,-10,-12,-1,5,7,2],[4,3,2,8,-13],[-7,-5,-4,-10,-1,-6,-2,-9,-1],[8,8,-5,-10,0,7,-8,7,-1],[14,-7,-10,8,2,10,3,-9,-1,-7,1,-11]]
-- [[-3,3],[-3,15,-1,-7,-13,14,-1,-13,15,0,-12,11],[13,-8,-13],[-9,-1,5,-10,-3,7,13,7,-12,13,0,11,8,-7,-15,11],[-16],[10,3,15,3,-12,-2,11,8,7,-11],[-3,-9,-12,-16],[],[-13,4,0,-9],[7,12,4,-15,-15,-9,-10,5,-7],[-13,14,12,-6,13,-10,2,7,-12,10,3,12,-10],[-12,-11,-12,6,6,-14,-11],[-10,16,-10,-5,-8,13,2,11],[-15,11]]
-- [[3,-3,-5,-10],[],[7,5,7,1,18,-6,1,-12,-2,13],[16,8,-5,-9,2,-10,-1,15,-3,14,-4,-11],[12,7,-9,-14,14,15,-15,-5,4,11,-4,12,-2,6,18,4],[16,-12,0,-11,-15,17,-6,6,3,-7]]
-- [[20,14,-7,7,-6,17,6,-20,-14,5,-9,3],[2,-6,-10,14,0,17,-6,-2],[-15,-15,-20,9,-20,3,-4,7,9,-7,-8,-7,-11,11,18,14,6,-12,-6,17],[12,-14,7,14,-6,-5],[13,5,-7,0,0,-17,8,14,15,4],[19,-20,10,1,-2,17,2,10,18,-3,17,-10,-18,1],[9,-9,8,-11,12,-19,-5],[14,11,19,14,14,-7,-11,-18,1,0,-16,-7],[-13,-20],[13,-13,-8,-15,18],[],[2,17,-15,-10,-12,-18,9],[4,-18,-16,3,14,-11,9,-20,-16,0,16],[5,3,0,4,5,6,0,-9,-15,-6,-7,8,12,-9]]

I'd suggest using AsArbitraryBuiltin Integer (feel free to alias it to something more palatable).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you aware that QuickCheck only generates Integers within the [-size, size] (or something) range with size typically being at most 100?

Ah, I'd forgotten about that. I seem to remember that what sample does is very misleading because it produces values from a much more restricted range than you usually get. I'll go away and remind myself about this stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@effectfully I've updated the tests to use the generator for AsArbitraryBuiltin Integer (which meant that I had to add some extra deriving clauses for AsArbitraryBuiltin). Thanks for the suggestion.

, test_integer_div_mod_properties
, test_integer_quot_rem_properties
, test_integer_order_properties
, test_integer_ring_properties
Copy link
Contributor

Choose a reason for hiding this comment

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

makes sense to rename the test below this one for its name to be consistent:
test_expModInteger_properties -> test_integer_exp_mod_properties

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea.

@kwxm kwxm temporarily deployed to github-pages June 20, 2025 22:00 — with GitHub Actions Inactive
@@ -278,7 +278,7 @@ instance ArbitraryBuiltin BLS12_381.Pairing.MlResult where
-- the 'Arbitrary' class and the logic for handling elements from 'ArbitraryBuiltin'.
newtype AsArbitraryBuiltin a = AsArbitraryBuiltin
{ unAsArbitraryBuiltin :: a
} deriving newtype (Show)
} deriving newtype (Show, Eq, Ord, Num)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are needed to make things like Positive and NonZero work.

-- for Integers because that only produces values in [-99..99]. Here are some
-- utilities to make it easier to use the better generator for
-- AsArbitraryBuiltin Integer.
type BigInteger = AsArbitraryBuiltin Integer
Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're not always big, but I couldn't think of a better name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Builtins No Changelog Required Add this to skip the Changelog Check Test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants