-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
[#5] Num laws for Size #32
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
resolver: nightly-2019-11-08 | ||
resolver: nightly-2020-05-01 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's been a while 😸 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Main (main) where | ||
|
||
import Test.Hspec (hspec) | ||
|
||
import Test.Slist.Size (sizeSpec) | ||
|
||
|
||
main :: IO () | ||
main = hspec sizeSpec |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,95 @@ | ||||||
{- HLINT ignore "Redundant fromInteger" -} | ||||||
{- HLINT ignore "Reduce duplication" -} | ||||||
|
||||||
module Test.Slist.Size | ||||||
( sizeSpec | ||||||
) where | ||||||
|
||||||
import Hedgehog (Gen, PropertyT, forAll, (===)) | ||||||
import Test.Hspec (Spec, describe, it) | ||||||
import Test.Hspec.Hedgehog (hedgehog) | ||||||
|
||||||
import Slist.Size (Size (..)) | ||||||
|
||||||
import qualified Hedgehog.Gen as Gen | ||||||
import qualified Hedgehog.Range as Range | ||||||
|
||||||
|
||||||
type Property = PropertyT IO () | ||||||
|
||||||
sizeSpec :: Spec | ||||||
sizeSpec = describe "Size tests" $ | ||||||
describe "'Num' laws" $ do | ||||||
it "Neutrality of 0 over addition" zeroAdditionNeutrality | ||||||
it "Commutativity of (+)" additionCommutativity | ||||||
it "Associativity of (+)" additionAssotiavity | ||||||
it "Neutrality of 1 over multiplication" oneMultiplicationNeutrality | ||||||
it "Commutativity of (*)" multiplicationCommutavity | ||||||
it "Associativity of (*)" multiplicationAssociativity | ||||||
it "Distributivity of (*) with respect to (+)" distributivity | ||||||
it "'abs' and 'signum' correspondence" absSignum | ||||||
|
||||||
zeroAdditionNeutrality :: Property | ||||||
zeroAdditionNeutrality = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
|
||||||
fromInteger 0 + x === x | ||||||
vrom911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
x + fromInteger 0 === x | ||||||
vrom911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
additionCommutativity :: Property | ||||||
additionCommutativity = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
y <- forAll genSize | ||||||
|
||||||
x + y === y + x | ||||||
|
||||||
additionAssotiavity :: Property | ||||||
additionAssotiavity = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
vrom911 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Reduce duplication
Suggested change
|
||||||
y <- forAll genSize | ||||||
z <- forAll genSize | ||||||
|
||||||
(x + y) + z === x + (y + z) | ||||||
|
||||||
oneMultiplicationNeutrality :: Property | ||||||
oneMultiplicationNeutrality = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
|
||||||
fromInteger 1 * x === x | ||||||
vrom911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
x * fromInteger 1 === x | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Warning: Redundant fromInteger
Suggested change
|
||||||
|
||||||
multiplicationCommutavity :: Property | ||||||
multiplicationCommutavity = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
y <- forAll genSize | ||||||
|
||||||
x * y === y * x | ||||||
|
||||||
multiplicationAssociativity :: Property | ||||||
multiplicationAssociativity = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
y <- forAll genSize | ||||||
z <- forAll genSize | ||||||
|
||||||
(x * y) * z === x * (y * z) | ||||||
|
||||||
distributivity :: Property | ||||||
distributivity = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
y <- forAll genSize | ||||||
z <- forAll genSize | ||||||
|
||||||
x * (y + z) === (x * y) + (x * z) | ||||||
(y + z) * x === (y * x) + (z * x) | ||||||
|
||||||
absSignum :: Property | ||||||
absSignum = hedgehog $ do | ||||||
x <- forAll genSize | ||||||
|
||||||
abs x * signum x === x | ||||||
|
||||||
genSize :: Gen Size | ||||||
genSize = Gen.frequency | ||||||
[ (1, pure Infinity) | ||||||
, (9, Size <$> Gen.int (Range.constant 0 maxBound)) | ||||||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐺 👍