Skip to content

Commit

Permalink
Tests: clean up warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Oct 15, 2023
1 parent 3cd64a2 commit 5d64fd8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
17 changes: 9 additions & 8 deletions test-suite/Math/NumberTheory/ArithmeticFunctionsTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,15 @@ nFreedomProperty2 (Power n) (NonNegative m) =
in take m (filter (isNFree n') [1 ..]) == take m (nFrees n' :: [Integer])

nFreedomProperty3 :: Power Word -> Positive Int -> Bool
nFreedomProperty3 (Power n) (Positive m) =
let n' | n == maxBound = n
| otherwise = n + 1
zet = 1 / zetas 1e-14 Inf.!! n' :: Double
m' = 100 * m
nfree = fromIntegral m' /
fromIntegral (head (drop (m' - 1) $ nFrees n' :: [Integer]))
in 1 / fromIntegral m >= abs (zet - nfree)
nFreedomProperty3 (Power n) (Positive m) = case drop (m' - 1) $ nFrees n :: [Integer] of
[] -> True
x : _ -> 1 / fromIntegral m >= abs (zet - fromIntegral m' / fromIntegral x)
where
zet :: Double
zet = 1 / zetas 1e-14 Inf.!! n

m' :: Int
m' = 100 * m

-- |
-- * Using a bounded integer type like @Int@ instead of @Integer@ here means
Expand Down
2 changes: 1 addition & 1 deletion test-suite/Math/NumberTheory/EisensteinIntegersTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ primesProperty1 (Positive index) = all (isJust . isPrime . (unPrime :: Prime E.E
primesProperty2 :: Positive Int -> Bool
primesProperty2 (Positive index) =
let isOrdered :: [Prime E.EisensteinInteger] -> Bool
isOrdered xs = all (\(x, y) -> E.norm (unPrime x) <= E.norm (unPrime y)) . zip xs $ tail xs
isOrdered xs = all (\(x, y) -> E.norm (unPrime x) <= E.norm (unPrime y)) . zip xs $ drop 1 xs
in isOrdered $ Inf.take index E.primes

-- | Checks that the numbers produced by @primes@ are all in the first
Expand Down
4 changes: 2 additions & 2 deletions test-suite/Math/NumberTheory/GaussianIntegersTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ findPrimeReference p =
k = integerSquareRoot (unPrime p)
bs = [1 .. k]
asbs = map (\b' -> ((b' * c) `mod` unPrime p, b')) bs
(a, b) = head [ (a', b') | (a', b') <- asbs, a' <= k]
(a, b) : _ = [ (a', b') | (a', b') <- asbs, a' <= k]
in a :+ b

findPrimeProperty1 :: Prime Integer -> Bool
Expand Down Expand Up @@ -113,7 +113,7 @@ primesGeneratesPrimesProperty i = case isPrime (unPrime (primes Inf.!! i) :: Gau

-- | Check that primes generates the primes in order.
orderingPrimes :: Assertion
orderingPrimes = assertBool "primes are ordered" (and $ zipWith (<=) xs (tail xs))
orderingPrimes = assertBool "primes are ordered" (and $ zipWith (<=) xs (drop 1 xs))
where xs = map (norm . unPrime) $ Inf.take 1000 primes

numberOfPrimes :: Assertion
Expand Down
13 changes: 7 additions & 6 deletions test-suite/Math/NumberTheory/Moduli/SqrtTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import Test.Tasty
import Test.Tasty.HUnit

import Control.Arrow
import Data.List (group, sort)
import Data.Maybe (fromJust)
import Data.List (sort)
import qualified Data.List.NonEmpty as NE
import Data.Maybe (fromJust, listToMaybe)
import Numeric.Natural

import Math.NumberTheory.Moduli hiding (invertMod)
Expand All @@ -32,7 +33,7 @@ unwrapPP :: (Prime Integer, Power Word) -> (Prime Integer, Word)
unwrapPP (p, Power e) = (p, e `mod` 5)

nubOrd :: Ord a => [a] -> [a]
nubOrd = map head . group . sort
nubOrd = map NE.head . NE.group . sort

-- | Check that 'sqrtMod' is defined iff a quadratic residue exists.
-- Also check that the result is a solution of input modular equation.
Expand Down Expand Up @@ -60,7 +61,7 @@ tonelliShanksProperty1 (Positive n) p'@(unPrime -> p) = p `mod` 4 /= 1 || jacobi
tonelliShanksProperty2 :: Prime Integer -> Bool
tonelliShanksProperty2 p'@(unPrime -> p) = p `mod` 4 /= 1 || (rt ^ 2 - n) `rem` p == 0
where
n = head $ filter (\s -> jacobi s p == One) [2..p-1]
n : _ = filter (\s -> jacobi s p == One) [2..p-1]
rt : _ = sqrtsModPrime n p'

tonelliShanksProperty3 :: Prime Integer -> Bool
Expand All @@ -72,11 +73,11 @@ tonelliShanksProperty3 p'@(unPrime -> p)

tonelliShanksSpecialCases :: Assertion
tonelliShanksSpecialCases =
assertEqual "OEIS A002224" [6, 32, 219, 439, 1526, 2987, 22193, 11740, 13854, 91168, 326277, 232059, 3230839, 4379725, 11754394, 32020334, 151024619, 345641931, 373671108, 1857111865, 8110112775, 4184367042] rts
assertEqual "OEIS A002224" (map Just [6, 32, 219, 439, 1526, 2987, 22193, 11740, 13854, 91168, 326277, 232059, 3230839, 4379725, 11754394, 32020334, 151024619, 345641931, 373671108, 1857111865, 8110112775, 4184367042]) rts
where
ps :: [Integer]
ps = [17, 73, 241, 1009, 2689, 8089, 33049, 53881, 87481, 483289, 515761, 1083289, 3818929, 9257329, 22000801, 48473881, 175244281, 427733329, 898716289, 8114538721, 9176747449, 23616331489]
rts = map (head . sqrtsModPrime 2 . fromJust . isPrime) ps
rts = map (listToMaybe . sqrtsModPrime 2 . fromJust . isPrime) ps

sqrtsModPrimePowerProperty1 :: AnySign Integer -> (Prime Integer, Power Word) -> Bool
sqrtsModPrimePowerProperty1 (AnySign n) (p'@(unPrime -> p), Power e) = gcd n p > 1
Expand Down
21 changes: 12 additions & 9 deletions test-suite/Math/NumberTheory/Recurrences/BilinearTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ binomialProperty1 :: Word -> Bool
binomialProperty1 i = length (binomial @Integer Inf.!! i) == fromIntegral i + 1

binomialProperty2 :: Word -> Bool
binomialProperty2 i = head (binomial @Integer Inf.!! i) == 1
binomialProperty2 i = take 1 (binomial @Integer Inf.!! i) == [1]

binomialProperty3 :: Word -> Bool
binomialProperty3 i = binomial @Integer Inf.!! i !! fromIntegral i == 1
Expand Down Expand Up @@ -91,8 +91,8 @@ stirling1Property1 i = length (stirling1 Inf.!! i) == fromIntegral i + 1

stirling1Property2 :: Word -> Bool
stirling1Property2 i
= head (stirling1 Inf.!! i)
== if i == 0 then 1 else 0
= take 1 (stirling1 Inf.!! i)
== [if i == 0 then 1 else 0]

stirling1Property3 :: Word -> Bool
stirling1Property3 i = stirling1 Inf.!! i !! fromIntegral i == 1
Expand All @@ -109,8 +109,8 @@ stirling2Property1 i = length (stirling2 Inf.!! i) == fromIntegral i + 1

stirling2Property2 :: Word -> Bool
stirling2Property2 i
= head (stirling2 Inf.!! i)
== if i == 0 then 1 else 0
= take 1 (stirling2 Inf.!! i)
== [if i == 0 then 1 else 0]

stirling2Property3 :: Word -> Bool
stirling2Property3 i = stirling2 Inf.!! i !! fromIntegral i == 1
Expand All @@ -127,8 +127,8 @@ lahProperty1 i = length (lah Inf.!! i) == fromIntegral i + 1

lahProperty2 :: Word -> Bool
lahProperty2 i
= head (lah Inf.!! i)
== product [1 .. i+1]
= take 1 (lah Inf.!! i)
== [product [1 .. i+1]]

lahProperty3 :: Word -> Bool
lahProperty3 i = lah Inf.!! i !! fromIntegral i == 1
Expand All @@ -143,7 +143,9 @@ eulerian1Property1 :: Word -> Bool
eulerian1Property1 i = length (eulerian1 Inf.!! i) == fromIntegral i

eulerian1Property2 :: Positive Int -> Bool
eulerian1Property2 (Positive i) = head (eulerian1 Inf.!! fromIntegral i) == 1
eulerian1Property2 (Positive i)
= take 1 (eulerian1 Inf.!! fromIntegral i)
== [1]

eulerian1Property3 :: Positive Int -> Bool
eulerian1Property3 (Positive i) = eulerian1 Inf.!! fromIntegral i !! (i - 1) == 1
Expand All @@ -160,7 +162,8 @@ eulerian2Property1 i = length (eulerian2 Inf.!! i) == fromIntegral i

eulerian2Property2 :: Positive Int -> Bool
eulerian2Property2 (Positive i)
= head (eulerian2 Inf.!! fromIntegral i) == 1
= take 1 (eulerian2 Inf.!! fromIntegral i)
== [1]

eulerian2Property3 :: Positive Int -> Bool
eulerian2Property3 (Positive i)
Expand Down
24 changes: 11 additions & 13 deletions test-suite/Math/NumberTheory/Recurrences/PentagonalTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-- Tests for Math.NumberTheory.Recurrences.Pentagonal
--

{-# LANGUAGE PostfixOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}

Expand All @@ -16,7 +17,7 @@ module Math.NumberTheory.Recurrences.PentagonalTests
( testSuite
) where

import Data.List.Infinite (Infinite(..))
import Data.List.Infinite (Infinite(..), (...))
import qualified Data.List.Infinite as Inf
import Data.Proxy (Proxy (..))
import GHC.Natural (Natural)
Expand All @@ -39,9 +40,9 @@ partition' = (partition Inf.!!) . fromIntegral
-- while @2@ is the @2 * 2 - 1 == 3@-rd, and so on.
pentagonalNumbersProperty1 :: AnySign Int -> Bool
pentagonalNumbersProperty1 (AnySign n)
| n == 0 = head pents == 0
| n > 0 = pents !! (2 * n - 1) == pent n
| otherwise = pents !! (2 * abs n) == pent n
| n == 0 = Inf.head pents == 0
| n > 0 = pents Inf.!! (2 * fromIntegral n - 1) == pent n
| otherwise = pents Inf.!! (2 * fromIntegral (- n)) == pent n
where
pent m = div (3 * (m * m) - m) 2

Expand All @@ -59,13 +60,10 @@ pentagonalSigns = zipWith (*) (cycle [1, 1, -1, -1])

-- | Copied from @Math.NumberTheory.Recurrences.Pentagonal@ to test the
-- reference implementation of @partition@.
pents :: (Enum a, Num a) => [a]
pents = interleave (scanl (\acc n -> acc + 3 * n - 1) 0 [1..])
(scanl (\acc n -> acc + 3 * n - 2) 1 [2..])
where
interleave :: [a] -> [a] -> [a]
interleave (n : ns) (m : ms) = n : m : interleave ns ms
interleave _ _ = []
pents :: (Enum a, Num a) => Infinite a
pents = Inf.interleave
(Inf.scanl (\acc n -> acc + 3 * n - 1) 0 (1...))
(Inf.scanl (\acc n -> acc + 3 * n - 2) 1 (2...))

-- | Check that @p(n) = p(n-1) + p(n-2) - p(n-5) - p(n-7) + p(n-11) + ...@,
-- where @p(x) = 0@ for any negative integer and @p(0) = 1@.
Expand All @@ -74,8 +72,8 @@ partitionProperty1 (Positive n) =
partition' n == (sum .
pentagonalSigns .
map (\m -> partition' (n - m)) .
takeWhile (\m -> n - m >= 0) .
tail $ pents)
Inf.takeWhile (\m -> n - m >= 0) .
Inf.tail $ pents)

-- | Check that
-- @partition :: [Math.NumberTheory.Moduli.Mod n] == map (`mod` n) partition@.
Expand Down

0 comments on commit 5d64fd8

Please sign in to comment.