tonelli shanks test

This commit is contained in:
2026-01-18 21:34:07 +01:00
parent 68f20e7623
commit 7a12d6b21a
3 changed files with 29 additions and 0 deletions

View File

@@ -59,6 +59,7 @@ test-suite haskell-math-test
main-is: Main.hs
other-modules:
GF2test
TonelliShanksTest
build-depends:
base ^>=4.18.2.1,
haskell-math,

View File

@@ -1,10 +1,12 @@
module Main (main) where
import GF2test (run)
import TonelliShanksTest (run)
main :: IO ()
main = do
putStrLn "Running all tests"
GF2test.run
TonelliShanksTest.run
putStrLn "All tests done."

26
test/TonelliShanksTest.hs Normal file
View File

@@ -0,0 +1,26 @@
module TonelliShanksTest (run) where
import Test.HUnit
import ModularSquareRoot (tonelliShanks)
assertOneOf :: (Eq a, Show a) => String -> Maybe a -> [a] -> Assertion
assertOneOf msg result expected =
case result of
Nothing -> assertFailure (msg ++ ": expected one of " ++ show expected ++ ", got Nothing")
Just x -> assertBool
(msg ++ ": got " ++ show x ++ ", expected one of " ++ show expected)
(x `elem` expected)
run :: IO ()
run = do
putStrLn "Tonelli Shanks test"
assertOneOf "sqrt(1) mod 7" (tonelliShanks 1 7) [1,6]
assertOneOf "sqrt(2) mod 7" (tonelliShanks 2 7) [3,4]
assertEqual "sqrt(3) mod 7" (tonelliShanks 3 7) Nothing
assertOneOf "sqrt(4) mod 7" (tonelliShanks 4 7) [2,5]
assertEqual "sqrt(5) mod 7" (tonelliShanks 5 7) Nothing
assertEqual "sqrt(6) mod 7" (tonelliShanks 6 7) Nothing
putStrLn "Tonelli Shanks test Done."