@@ -2,7 +2,7 @@ module Test.Main where
3import Prelude
3import Prelude
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)
6import Data.BigInt as BigInt
6import Data.BigInt as BigInt
7import Data.Maybe (Maybe(..), fromJust, isJust)
7import Data.Maybe (Maybe(..), fromJust, isJust)
8import Effect (Effect)
8import Effect (Effect)
@@ -10,14 +10,17 @@ import Effect.Console (log)
10import Math as Math
10import Math as Math
11import Partial.Unsafe (unsafePartial)
11import Partial.Unsafe (unsafePartial)
12import Test.QuickCheck (arbitrary, quickCheck', (<?>), (===))
12import Test.QuickCheck (arbitrary, quickCheck', (<?>), (===))
13−import Test.QuickCheck.Gen (Gen, chooseInt, suchThat)
13+import Test.QuickCheck.Gen (Gen, chooseInt, uniform, suchThat)
15-- Just here to package up the safe use of `unsafePartial`.
15-- Just here to package up the safe use of `unsafePartial`.
16trying :: forall a b. Gen a -> (a -> Maybe b) -> Gen b
16trying :: forall a b. Gen a -> (a -> Maybe b) -> Gen b
17trying gen f = map (unsafePartial fromJust) (map f gen `suchThat` isJust)
17trying gen f = map (unsafePartial fromJust) (map f gen `suchThat` isJust)
19genFixedP :: forall p. KnownPrecision p => PProxy p -> Gen (Fixed p)
19genFixedP :: forall p. KnownPrecision p => PProxy p -> Gen (Fixed p)
20−genFixedP _ = map fromNumber arbitrary `trying` identity
20+genFixedP _ = map (fromNumber <<< stretch) uniform `trying` identity
22+ -- maps [0,1] to [-10,10]
23+ stretch = (_ * 20.0) >>> (_ - 10.0)
22fromNumberWith
25fromNumberWith
23 :: forall precision
26 :: forall precision
@@ -35,15 +38,15 @@ withPrecisionOf
35 -> Fixed precision
38 -> Fixed precision
36withPrecisionOf _ x = x
39withPrecisionOf _ x = x
38−floorToPrecision
41+truncateToPrecision
39 :: forall precision
42 :: forall precision
40 . KnownPrecision precision
43 . KnownPrecision precision
42 -> Fixed precision
45 -> Fixed precision
43 -> Fixed precision
46 -> Fixed precision
44−floorToPrecision p =
47+truncateToPrecision p =
45 (_ / scaleFactor)
48 (_ / scaleFactor)
46− <<< floor
49+ <<< (\x -> if x > zero then floor x else ceil x)
47 <<< (_ * scaleFactor)
50 <<< (_ * scaleFactor)
49 scaleFactor =
52 scaleFactor =
@@ -123,7 +126,8 @@ main = do
123 precision <- chooseInt 0 100
126 precision <- chooseInt 0 100
124 unsafePartial fromJust $ reifyPrecision precision \p -> do
127 unsafePartial fromJust $ reifyPrecision precision \p -> do
125 a <- genFixedP p
128 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 }
129 log "Conversions to/from Number"
133 log "Conversions to/from Number"
@@ -162,7 +166,7 @@ main = do
162 let xx = rescale y
166 let xx = rescale y
163 pure $ x == xx <?> show { x, xx, y }
167 pure $ x == xx <?> show { x, xx, y }
165− log "rescaling down then up should be the same as flooring"
169+ log "rescaling down then up should be the same as truncating"
166 quickCheck' 1000 do
170 quickCheck' 1000 do
167 let hi = 20
171 let hi = 20
168 lo <- chooseInt 10 20
172 lo <- chooseInt 10 20
@@ -171,5 +175,5 @@ main = do
171 reifyPrecision hi \hi_ -> do
175 reifyPrecision hi \hi_ -> do
172 x <- genFixedP hi_
176 x <- genFixedP hi_
173 let y = withPrecisionOf lo_ $ rescale x
177 let y = withPrecisionOf lo_ $ rescale x
174− let xx = floorToPrecision lo x
178+ let xx = truncateToPrecision lo x
175 pure $ xx == rescale y <?> show { x, xx, y }
179 pure $ xx == rescale y <?> show { x, xx, y }