File: MoebiusInversionTests.hs

package info (click to toggle)
haskell-arithmoi 0.13.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: haskell: 10,379; makefile: 3
file content (54 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- |
-- Module:      Math.NumberTheory.MoebiusInversionTests
-- Copyright:   (c) 2016 Andrew Lelechenko
-- Licence:     MIT
-- Maintainer:  Andrew Lelechenko <andrew.lelechenko@gmail.com>
--
-- Tests for Math.NumberTheory.MoebiusInversion
--

{-# OPTIONS_GHC -fno-warn-type-defaults #-}

module Math.NumberTheory.MoebiusInversionTests
  ( testSuite
  ) where

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC hiding (Positive)

import Data.Proxy
import Data.Vector.Unboxed (Vector)

import Math.NumberTheory.MoebiusInversion
import Math.NumberTheory.ArithmeticFunctions
import Math.NumberTheory.TestUtils

proxy :: Proxy Vector
proxy = Proxy

totientSumProperty :: AnySign Word -> Bool
totientSumProperty (AnySign n) = (totientSum proxy n :: Word) == sum (map totient [1..n])

totientSumSpecialCase1 :: Assertion
totientSumSpecialCase1 = assertEqual "totientSum" 4496 (totientSum proxy 121 :: Word)

totientSumZero :: Assertion
totientSumZero = assertEqual "totientSum" 0 (totientSum proxy 0 :: Word)

generalInversionProperty :: (Word -> Word) -> Positive Word -> Bool
generalInversionProperty g (Positive n)
  =  g n == sum [f (n `quot` k) | k <- [1 .. n]]
  && f n == sum [runMoebius (moebius k) * g (n `quot` k) | k <- [1 .. n]]
  where
    f = generalInversion proxy g

testSuite :: TestTree
testSuite = testGroup "MoebiusInversion"
  [ testGroup "totientSum"
    [ testSmallAndQuick "matches definitions" totientSumProperty
    , testCase          "special case 1"      totientSumSpecialCase1
    , testCase          "zero"                totientSumZero
    ]
  , QC.testProperty "generalInversion" generalInversionProperty
  ]