commit 5e05dab99d44c0a78c6bf7008183477ffbae59ae
parent 0be10b6a613880aea3fa87096f5ec2eb77426e76
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 22 Jan 2025 12:44:54 +0400
test: arithmetic tests
Diffstat:
| M | test/Main.hs | | | 43 | ++++++++++++++++++++++++++++++++++++++----- |
1 file changed, 38 insertions(+), 5 deletions(-)
diff --git a/test/Main.hs b/test/Main.hs
@@ -14,26 +14,59 @@ instance Q.Arbitrary Word256 where
w3 <- Q.arbitrary
pure (Word256 w0 w1 w2 w3)
+-- second argument is no greater than first argument
+newtype Monotonic a = Monotonic (a, a)
+ deriving Show
+
+instance (Q.Arbitrary a, Ord a) => Q.Arbitrary (Monotonic a) where
+ arbitrary = do
+ a <- Q.arbitrary
+ b <- Q.arbitrary `Q.suchThat` (\b -> b <= a)
+ pure (Monotonic (a, b))
+
+-- properties -----------------------------------------------------------------
+
to_word256_inverts_to_integer :: Word256 -> Bool
to_word256_inverts_to_integer w256 =
to_word256 (to_integer w256) == w256
-- doesn't hold for negative inputs
-to_integer_inverts_to_word256 :: (Q.NonNegative Integer) -> Bool
+to_integer_inverts_to_word256 :: Q.NonNegative Integer -> Bool
to_integer_inverts_to_word256 (Q.NonNegative n) =
to_integer (to_word256 n) == n
-conversion :: TestTree
-conversion = testGroup "conversion" [
+addition_matches :: Q.NonNegative Integer -> Q.NonNegative Integer -> Bool
+addition_matches (Q.NonNegative a) (Q.NonNegative b) =
+ to_integer (to_word256 a `add` to_word256 b) == a + b
+
+subtraction_matches :: Monotonic (Q.NonNegative Integer) -> Bool
+subtraction_matches (Monotonic (Q.NonNegative a, Q.NonNegative b)) =
+ to_integer (to_word256 a `sub` to_word256 b) == a - b
+
+
+-- main -----------------------------------------------------------------------
+
+inverses :: TestTree
+inverses = testGroup "inverses" [
Q.testProperty "to_word256 . to_integer ~ id" $
Q.withMaxSuccess 1000 to_word256_inverts_to_integer
- , Q.testProperty "to_integer . to_word256 ~ id (for nonnegative input)" $
+ , Q.testProperty "to_integer . to_word256 ~ id (nonneg input)" $
Q.withMaxSuccess 1000 to_integer_inverts_to_word256
]
+arithmetic :: TestTree
+arithmetic = testGroup "arithmetic" [
+ Q.testProperty "addition matches (nonneg input)" $
+ Q.withMaxSuccess 1000 addition_matches
+ , Q.testProperty "subtraction matches (nonneg, monotonic inputs)" $
+ Q.withMaxSuccess 1000 subtraction_matches
+ ]
+
+
main :: IO ()
main = defaultMain $ testGroup "ppad-fw" [
- conversion
+ inverses
+ , arithmetic
]