Browse Source

Price: more tests

master
Denis Tereshkin 8 years ago
parent
commit
b4ead56366
  1. 13
      src/ATrade/Price.hs
  2. 5
      src/ATrade/Types.hs
  3. 15
      test/TestTypes.hs

13
src/ATrade/Price.hs

@ -16,27 +16,30 @@ data Price = Price {
giga :: Int64 giga :: Int64
giga = 1000000000 giga = 1000000000
mega :: Int64
mega = 1000000
instance Num Price where instance Num Price where
a + b = Price { a + b = Price {
priceQuants = priceQuants a + priceQuants b } priceQuants = priceQuants a + priceQuants b }
a * b = Price { a * b = Price {
priceQuants = (priceQuants a * priceQuants b) `div` giga } priceQuants = (priceQuants a * priceQuants b) `div` mega }
abs a = a { priceQuants = abs (priceQuants a) } abs a = a { priceQuants = abs (priceQuants a) }
signum a = a { priceQuants = signum (priceQuants a)} signum a = a { priceQuants = signum (priceQuants a)}
fromInteger int = Price { priceQuants = giga * fromInteger int} fromInteger int = Price { priceQuants = mega * fromInteger int}
negate a = a { priceQuants = negate (priceQuants a) } negate a = a { priceQuants = negate (priceQuants a) }
toDouble :: Price -> Double toDouble :: Price -> Double
toDouble p = fromIntegral (priceQuants p) / fromIntegral giga toDouble p = fromIntegral (priceQuants p) / fromIntegral mega
fromDouble :: Double -> Price fromDouble :: Double -> Price
fromDouble d = Price { priceQuants = truncate (d * fromIntegral giga) } fromDouble d = Price { priceQuants = truncate (d * fromIntegral mega) }
decompose :: Price -> (Int64, Int32) decompose :: Price -> (Int64, Int32)
decompose Price{priceQuants = p} = (p `div` giga, (fromInteger . toInteger) $ p `mod` giga) decompose Price{priceQuants = p} = (p `div` mega, (fromInteger . toInteger) $ p `mod` mega)

5
src/ATrade/Types.hs

@ -21,11 +21,14 @@ module ATrade.Types (
ServerSecurityParams(..), ServerSecurityParams(..),
defaultServerSecurityParams, defaultServerSecurityParams,
ClientSecurityParams(..), ClientSecurityParams(..),
defaultClientSecurityParams defaultClientSecurityParams,
module ATrade.Price
) where ) where
import GHC.Generics import GHC.Generics
import ATrade.Price
import Control.Monad import Control.Monad
import Data.Aeson import Data.Aeson
import Data.Aeson.Types import Data.Aeson.Types

15
test/TestTypes.hs

@ -36,6 +36,9 @@ properties = testGroup "Types" [
, testPrice1 , testPrice1
, testPrice2 , testPrice2
, testPriceDecompose , testPriceDecompose
, testPriceAddition
, testPriceMultiplication
, testPriceSubtraction
] ]
testTickSerialization = QC.testProperty "Deserialize serialized tick" testTickSerialization = QC.testProperty "Deserialize serialized tick"
@ -95,5 +98,15 @@ testPrice2 = QC.testProperty "toDouble . fromDouble $ Price" $
testPriceDecompose = QC.testProperty "Price decompose" testPriceDecompose = QC.testProperty "Price decompose"
(\p -> let (i, f) = decompose p in (\p -> let (i, f) = decompose p in
i * 1000000000 + (fromInteger . fromIntegral) f == priceQuants p) i * 1000000 + (fromInteger . fromIntegral) f == priceQuants p)
testPriceAddition = QC.testProperty "Price addition"
(\(p1, p2) -> abs (toDouble p1 + toDouble p2 - toDouble (p1 + p2)) < 0.00001)
testPriceMultiplication = QC.testProperty "Price multiplication" $
QC.forAll (arbitrary `suchThat` (\(p1, p2) -> p1 < 100000 && p2 < 100000))
(\(p1, p2) -> abs (toDouble p1 + toDouble p2 - toDouble (p1 + p2)) < 0.00001)
testPriceSubtraction = QC.testProperty "Price subtraction"
(\(p1, p2) -> abs (toDouble p1 - toDouble p2 - toDouble (p1 - p2)) < 0.00001)

Loading…
Cancel
Save