@@ -2,7 +2,7 @@ module Test.Main where
22
33import Prelude
44
5−import Data.Fixed (class KnownPrecision, Fixed, PProxy, fromNumber, toNumber, fromString, reifyPrecision, reflectPrecision, toString, P100, rescale, fromBigInt, floor)
5+import Data.Fixed (class KnownPrecision, Fixed, PProxy, fromNumber, toNumber, fromString, reifyPrecision, reflectPrecision, toString, P100, rescale, fromBigInt, floor, ceil, numerator)
66import Data.BigInt as BigInt
77import Data.Maybe (Maybe(..), fromJust, isJust)
88import Effect (Effect)
@@ -10,14 +10,17 @@ import Effect.Console (log)
1010import Math as Math
1111import Partial.Unsafe (unsafePartial)
1212import Test.QuickCheck (arbitrary, quickCheck', (<?>), (===))
13−import Test.QuickCheck.Gen (Gen, chooseInt, suchThat)
13+import Test.QuickCheck.Gen (Gen, chooseInt, uniform, suchThat)
1414
1515-- Just here to package up the safe use of `unsafePartial`.
1616trying :: forall a b. Gen a -> (a -> Maybe b) -> Gen b
1717trying gen f = map (unsafePartial fromJust) (map f gen `suchThat` isJust)
1818
1919genFixedP :: forall p. KnownPrecision p => PProxy p -> Gen (Fixed p)
20−genFixedP _ = map fromNumber arbitrary `trying` identity
20+genFixedP _ = map (fromNumber <<< stretch) uniform `trying` identity
21+ where
22+ -- maps [0,1] to [-10,10]
23+ stretch = (_ * 20.0) >>> (_ - 10.0)
2124
2225fromNumberWith
2326 :: forall precision
@@ -35,15 +38,15 @@ withPrecisionOf
3538 -> Fixed precision
3639withPrecisionOf _ x = x
3740
38−floorToPrecision
41+truncateToPrecision
3942 :: forall precision
4043 . KnownPrecision precision
4144 => Int
4245 -> Fixed precision
4346 -> Fixed precision
44−floorToPrecision p =
47+truncateToPrecision p =
4548 (_ / scaleFactor)
46− <<< floor
49+ <<< (\x -> if x > zero then floor x else ceil x)
4750 <<< (_ * scaleFactor)
4851 where
4952 scaleFactor =
@@ -123,7 +126,8 @@ main = do
123126 precision <- chooseInt 0 100
124127 unsafePartial fromJust $ reifyPrecision precision \p -> do
125128 a <- genFixedP p
126− pure $ fromString (toString a) == Just a <?> show { precision, a }
129+ let b = fromString (toString a)
130+ pure $ b == Just a <?> show { precision, a, b, numeratorA: numerator a, numeratorB: map numerator b }
127131
128132 log ""
129133 log "Conversions to/from Number"
@@ -162,7 +166,7 @@ main = do
162166 let xx = rescale y
163167 pure $ x == xx <?> show { x, xx, y }
164168
165− log "rescaling down then up should be the same as flooring"
169+ log "rescaling down then up should be the same as truncating"
166170 quickCheck' 1000 do
167171 let hi = 20
168172 lo <- chooseInt 10 20
@@ -171,5 +175,5 @@ main = do
171175 reifyPrecision hi \hi_ -> do
172176 x <- genFixedP hi_
173177 let y = withPrecisionOf lo_ $ rescale x
174− let xx = floorToPrecision lo x
178+ let xx = truncateToPrecision lo x
175179 pure $ xx == rescale y <?> show { x, xx, y }