{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}
module Evaluation.Builtins.Integer.QuotRemProperties
( test_integer_quot_rem_properties
, integer_quot_rem_property_names
, generate_integer_quot_rem_properties
)
where
import Prelude hiding (abs)
import Evaluation.Builtins.Common
import Evaluation.Builtins.Integer.Common
import Evaluation.Builtins.Integer.Conformance
import Evaluation.Builtins.Conformance (CheckMode, OutputFormat, PropertyFilter, PropertyId (..))
import Test.Cardano.Base.QuickCheck qualified as BaseQC
import Test.QuickCheck.Random (QCGen)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck
numberOfTests :: Int
numberOfTests :: Int
numberOfTests = Int
200
prop_quot_0_fails :: BigInteger -> Check
prop_quot_0_fails :: BigInteger -> Check
prop_quot_0_fails (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) =
PlcTerm -> Check
CheckFails (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> PlcTerm
quotientInteger PlcTerm
a PlcTerm
zero
prop_rem_0_fails :: BigInteger -> Check
prop_rem_0_fails :: BigInteger -> Check
prop_rem_0_fails (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) =
PlcTerm -> Check
CheckFails (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
zero
prop_quot_rem_compatible :: BigInteger -> NonZero BigInteger -> Check
prop_quot_rem_compatible :: BigInteger -> NonZero BigInteger -> Check
prop_quot_rem_compatible (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (NonZero (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
let t :: PlcTerm
t = PlcTerm -> PlcTerm -> PlcTerm
addInteger (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
b (PlcTerm -> PlcTerm -> PlcTerm
quotientInteger PlcTerm
a PlcTerm
b)) (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b)
in PlcTerm -> PlcTerm -> Check
CheckEq PlcTerm
t PlcTerm
a
prop_quot_rem_multiple :: BigInteger -> NonZero BigInteger -> Check
prop_quot_rem_multiple :: BigInteger -> NonZero BigInteger -> Check
prop_quot_rem_multiple (BigInteger -> PlcTerm
biginteger -> PlcTerm
k) (NonZero (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
let t1 :: PlcTerm
t1 = PlcTerm -> PlcTerm -> PlcTerm
quotientInteger (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
k PlcTerm
b) PlcTerm
b
t2 :: PlcTerm
t2 = PlcTerm -> PlcTerm -> PlcTerm
remainderInteger (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
k PlcTerm
b) PlcTerm
b
in Check -> Check -> Check
CheckBoth (PlcTerm -> PlcTerm -> Check
CheckEq PlcTerm
t1 PlcTerm
k) (PlcTerm -> PlcTerm -> Check
CheckEq PlcTerm
t2 PlcTerm
zero)
prop_rem_additive_pos
:: NonNegative BigInteger -> NonNegative BigInteger -> NonZero BigInteger -> Check
prop_rem_additive_pos :: NonNegative BigInteger
-> NonNegative BigInteger -> NonZero BigInteger -> Check
prop_rem_additive_pos (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a')) (NonZero (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
let t1 :: PlcTerm
t1 = PlcTerm -> PlcTerm -> PlcTerm
remainderInteger (PlcTerm -> PlcTerm -> PlcTerm
addInteger PlcTerm
a PlcTerm
a') PlcTerm
b
t2 :: PlcTerm
t2 = PlcTerm -> PlcTerm -> PlcTerm
remainderInteger (PlcTerm -> PlcTerm -> PlcTerm
addInteger (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b) (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a' PlcTerm
b)) PlcTerm
b
in PlcTerm -> PlcTerm -> Check
CheckEq PlcTerm
t1 PlcTerm
t2
prop_rem_additive_neg
:: NonPositive BigInteger -> NonPositive BigInteger -> NonZero BigInteger -> Check
prop_rem_additive_neg :: NonPositive BigInteger
-> NonPositive BigInteger -> NonZero BigInteger -> Check
prop_rem_additive_neg (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a')) (NonZero (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
let t1 :: PlcTerm
t1 = PlcTerm -> PlcTerm -> PlcTerm
remainderInteger (PlcTerm -> PlcTerm -> PlcTerm
addInteger PlcTerm
a PlcTerm
a') PlcTerm
b
t2 :: PlcTerm
t2 = PlcTerm -> PlcTerm -> PlcTerm
remainderInteger (PlcTerm -> PlcTerm -> PlcTerm
addInteger (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b) (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a' PlcTerm
b)) PlcTerm
b
in PlcTerm -> PlcTerm -> Check
CheckEq PlcTerm
t1 PlcTerm
t2
prop_rem_multiplicative :: BigInteger -> BigInteger -> NonZero BigInteger -> Check
prop_rem_multiplicative :: BigInteger -> BigInteger -> NonZero BigInteger -> Check
prop_rem_multiplicative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (BigInteger -> PlcTerm
biginteger -> PlcTerm
a') (NonZero (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
let t1 :: PlcTerm
t1 = PlcTerm -> PlcTerm -> PlcTerm
remainderInteger (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
a PlcTerm
a') PlcTerm
b
t2 :: PlcTerm
t2 = PlcTerm -> PlcTerm -> PlcTerm
remainderInteger (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b) (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a' PlcTerm
b)) PlcTerm
b
in PlcTerm -> PlcTerm -> Check
CheckEq PlcTerm
t1 PlcTerm
t2
prop_rem_size :: BigInteger -> NonZero BigInteger -> Check
prop_rem_size :: BigInteger -> NonZero BigInteger -> Check
prop_rem_size (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (NonZero (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
let r :: PlcTerm
r = PlcTerm -> PlcTerm
abs (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b)
t1 :: PlcTerm
t1 = PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
zero PlcTerm
r
t2 :: PlcTerm
t2 = PlcTerm -> PlcTerm -> PlcTerm
lessThanInteger PlcTerm
r (PlcTerm -> PlcTerm
abs PlcTerm
b)
in Check -> Check -> Check
CheckBoth (PlcTerm -> Check
CheckTrue PlcTerm
t1) (PlcTerm -> Check
CheckTrue PlcTerm
t2)
prop_quot_pos_pos :: NonNegative BigInteger -> Positive BigInteger -> Check
prop_quot_pos_pos :: NonNegative BigInteger -> Positive BigInteger -> Check
prop_quot_pos_pos (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Positive (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
ge0 (PlcTerm -> PlcTerm -> PlcTerm
quotientInteger PlcTerm
a PlcTerm
b)
prop_quot_neg_pos :: NonPositive BigInteger -> Positive BigInteger -> Check
prop_quot_neg_pos :: NonPositive BigInteger -> Positive BigInteger -> Check
prop_quot_neg_pos (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Positive (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
le0 (PlcTerm -> PlcTerm -> PlcTerm
quotientInteger PlcTerm
a PlcTerm
b)
prop_quot_pos_neg :: NonNegative BigInteger -> Negative BigInteger -> Check
prop_quot_pos_neg :: NonNegative BigInteger -> Negative BigInteger -> Check
prop_quot_pos_neg (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Negative (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
le0 (PlcTerm -> PlcTerm -> PlcTerm
quotientInteger PlcTerm
a PlcTerm
b)
prop_quot_neg_neg :: NonPositive BigInteger -> Negative BigInteger -> Check
prop_quot_neg_neg :: NonPositive BigInteger -> Negative BigInteger -> Check
prop_quot_neg_neg (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Negative (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
ge0 (PlcTerm -> PlcTerm -> PlcTerm
quotientInteger PlcTerm
a PlcTerm
b)
prop_rem_pos_pos :: (NonNegative BigInteger) -> (Positive BigInteger) -> Check
prop_rem_pos_pos :: NonNegative BigInteger -> Positive BigInteger -> Check
prop_rem_pos_pos (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Positive (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
ge0 (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b)
prop_rem_neg_pos :: (NonPositive BigInteger) -> (Positive BigInteger) -> Check
prop_rem_neg_pos :: NonPositive BigInteger -> Positive BigInteger -> Check
prop_rem_neg_pos (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Positive (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
le0 (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b)
prop_rem_pos_neg :: (NonNegative BigInteger) -> (Negative BigInteger) -> Check
prop_rem_pos_neg :: NonNegative BigInteger -> Negative BigInteger -> Check
prop_rem_pos_neg (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Negative (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
ge0 (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b)
prop_rem_neg_neg :: (NonPositive BigInteger) -> (Negative BigInteger) -> Check
prop_rem_neg_neg :: NonPositive BigInteger -> Negative BigInteger -> Check
prop_rem_neg_neg (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (Negative (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm
le0 (PlcTerm -> PlcTerm -> PlcTerm
remainderInteger PlcTerm
a PlcTerm
b)
test_integer_quot_rem_properties :: TestTree
test_integer_quot_rem_properties :: TestTree
test_integer_quot_rem_properties =
TestName -> [TestTree] -> TestTree
testGroup TestName
"Property tests for quotientInteger and remainderInteger" ([TestTree] -> TestTree) -> [TestTree] -> TestTree
forall a b. (a -> b) -> a -> b
$
(Property -> Property) -> [PropertySpec (Gen Check)] -> [TestTree]
testPropertySpecs (Int -> Property -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
numberOfTests) [PropertySpec (Gen Check)]
integerQuotRemProperties
integerQuotRemProperties :: [PropertySpec (Gen Check)]
integerQuotRemProperties :: [PropertySpec (Gen Check)]
integerQuotRemProperties =
[ TestName
-> TestName -> (BigInteger -> Check) -> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"quot_0_fails" TestName
"quotientInteger _ 0 always fails" BigInteger -> Check
prop_quot_0_fails
, TestName
-> TestName -> (BigInteger -> Check) -> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"rem_0_fails" TestName
"remainderInteger _ 0 always fails" BigInteger -> Check
prop_rem_0_fails
, TestName
-> TestName
-> (BigInteger -> NonZero BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec
TestName
"quot_rem_compatible"
TestName
"quotientInteger and remainderInteger are compatible"
BigInteger -> NonZero BigInteger -> Check
prop_quot_rem_compatible
, TestName
-> TestName
-> (BigInteger -> NonZero BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec
TestName
"quot_rem_multiple"
TestName
"quotientInteger and remainderInteger behave sensibly on multiples of the divisor"
BigInteger -> NonZero BigInteger -> Check
prop_quot_rem_multiple
, TestName
-> TestName
-> (NonNegative BigInteger
-> NonNegative BigInteger -> NonZero BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec
TestName
"rem_additive_pos"
TestName
"remainderInteger _ b is additive on non-negative inputs"
NonNegative BigInteger
-> NonNegative BigInteger -> NonZero BigInteger -> Check
prop_rem_additive_pos
, TestName
-> TestName
-> (NonPositive BigInteger
-> NonPositive BigInteger -> NonZero BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec
TestName
"rem_additive_neg"
TestName
"remainderInteger _ b is additive on non-positive inputs"
NonPositive BigInteger
-> NonPositive BigInteger -> NonZero BigInteger -> Check
prop_rem_additive_neg
, TestName
-> TestName
-> (BigInteger -> BigInteger -> NonZero BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec
TestName
"rem_multiplicative"
TestName
"remainderInteger is a multiplicative homomorphism"
BigInteger -> BigInteger -> NonZero BigInteger -> Check
prop_rem_multiplicative
, TestName
-> TestName
-> (BigInteger -> NonZero BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"rem_size" TestName
"remainderInteger size correct" BigInteger -> NonZero BigInteger -> Check
prop_rem_size
, TestName
-> TestName
-> (NonNegative BigInteger -> Positive BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"quot_pos_pos" TestName
"quotientInteger (>= 0) (> 0) >= 0" NonNegative BigInteger -> Positive BigInteger -> Check
prop_quot_pos_pos
, TestName
-> TestName
-> (NonPositive BigInteger -> Positive BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"quot_neg_pos" TestName
"quotientInteger (<= 0) (> 0) <= 0" NonPositive BigInteger -> Positive BigInteger -> Check
prop_quot_neg_pos
, TestName
-> TestName
-> (NonNegative BigInteger -> Negative BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"quot_pos_neg" TestName
"quotientInteger (>= 0) (< 0) <= 0" NonNegative BigInteger -> Negative BigInteger -> Check
prop_quot_pos_neg
, TestName
-> TestName
-> (NonPositive BigInteger -> Negative BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"quot_neg_neg" TestName
"quotientInteger (<= 0) (< 0) >= 0" NonPositive BigInteger -> Negative BigInteger -> Check
prop_quot_neg_neg
, TestName
-> TestName
-> (NonNegative BigInteger -> Positive BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"rem_pos_pos" TestName
"remainderInteger (>= 0) (> 0) >= 0" NonNegative BigInteger -> Positive BigInteger -> Check
prop_rem_pos_pos
, TestName
-> TestName
-> (NonPositive BigInteger -> Positive BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"rem_neg_pos" TestName
"remainderInteger (<= 0) (> 0) <= 0" NonPositive BigInteger -> Positive BigInteger -> Check
prop_rem_neg_pos
, TestName
-> TestName
-> (NonNegative BigInteger -> Negative BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"rem_pos_neg" TestName
"remainderInteger (>= 0) (< 0) >= 0" NonNegative BigInteger -> Negative BigInteger -> Check
prop_rem_pos_neg
, TestName
-> TestName
-> (NonPositive BigInteger -> Negative BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"rem_neg_neg" TestName
"remainderInteger (<= 0) (< 0) <= 0" NonPositive BigInteger -> Negative BigInteger -> Check
prop_rem_neg_neg
]
generate_integer_quot_rem_properties
:: OutputFormat -> CheckMode -> PropertyFilter -> FilePath -> Int -> QCGen -> IO [PropertyReport]
generate_integer_quot_rem_properties :: OutputFormat
-> CheckMode
-> PropertyFilter
-> TestName
-> Int
-> QCGen
-> IO [PropertyReport]
generate_integer_quot_rem_properties OutputFormat
format CheckMode
mode PropertyFilter
include TestName
outputDir Int
numFiles QCGen
seed =
OutputFormat
-> CheckMode
-> PropertyFilter
-> TestName
-> Int
-> QCGen
-> [TestName]
-> [PropertySpec (Gen Check)]
-> IO [PropertyReport]
generatePropertySpecs
OutputFormat
format
CheckMode
mode
PropertyFilter
include
TestName
outputDir
Int
numFiles
QCGen
seed
[TestName]
conformanceSuiteDirectory
[PropertySpec (Gen Check)]
integerQuotRemProperties
integer_quot_rem_property_names :: [PropertyId]
integer_quot_rem_property_names :: [PropertyId]
integer_quot_rem_property_names =
[TestName] -> [PropertySpec (Gen Check)] -> [PropertyId]
forall gen. [TestName] -> [PropertySpec gen] -> [PropertyId]
propertySpecPropertyIds [TestName]
conformanceSuiteDirectory [PropertySpec (Gen Check)]
integerQuotRemProperties
conformanceSuiteDirectory :: [String]
conformanceSuiteDirectory :: [TestName]
conformanceSuiteDirectory = [TestName
"integer", TestName
"QuotRemProperties"]