packages feed

synthesizer-core-0.4: src/Test/Utility.hs

{-# LANGUAGE NoImplicitPrelude #-}
module Test.Utility where

import Test.QuickCheck (Arbitrary(arbitrary))

import qualified Algebra.RealRing                  as RealRing
import qualified Algebra.Ring                  as Ring

import qualified Data.Char as Char

import NumericPrelude.Base
import NumericPrelude.Numeric


equalList :: Eq a => [a] -> Bool
equalList xs =
   -- 'drop 1' instead of 'take' for suppression of error
   and (zipWith (==) xs (drop 1 xs))


approxEqual :: (RealRing.C a) => a -> a -> a -> Bool
approxEqual eps x y =
   2 * abs (x-y) <= eps * (abs x + abs y)

approxEqualListRel :: (RealRing.C a) => a -> [a] -> Bool
approxEqualListRel eps xs =
   let n = fromIntegral $ length xs
   in  approxEqualListAbs (eps * n * sum (map abs xs)) xs

approxEqualListAbs :: (RealRing.C a) => a -> [a] -> Bool
approxEqualListAbs eps xs =
   let n = fromIntegral $ length xs
       s = sum xs
   in  sum (map (\x -> abs (n*x-s)) xs)  <=  eps


-- see event-list

newtype ArbChar = ArbChar Char
   deriving (Eq, Ord)

instance Show ArbChar where
   showsPrec n (ArbChar c) = showsPrec n c

instance Arbitrary ArbChar where
   arbitrary = fmap (ArbChar . Char.chr . (32+) . flip mod 96) arbitrary

unpackArbString :: [ArbChar] -> String
unpackArbString =
   map (\(ArbChar c) -> c)