Skip to content

Commit f268fc9

Browse files
Relax some tests that are satisfied only up to some floating point error. (#59)
1 parent 971bae0 commit f268fc9

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

arrayfire.cabal

+4-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ test-suite test
144144
exitcode-stdio-1.0
145145
main-is:
146146
Main.hs
147+
other-modules:
148+
Test.Hspec.ApproxExpect
147149
hs-source-dirs:
148150
test
149151
build-depends:
@@ -154,7 +156,8 @@ test-suite test
154156
HUnit,
155157
QuickCheck,
156158
quickcheck-classes,
157-
vector
159+
vector,
160+
call-stack >=0.4 && <0.5
158161
if !flag(disable-build-tool-depends)
159162
build-tool-depends:
160163
hspec-discover:hspec-discover

test/ArrayFire/LAPACKSpec.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module ArrayFire.LAPACKSpec where
44
import qualified ArrayFire as A
55
import Prelude
66
import Test.Hspec
7+
import Test.Hspec.ApproxExpect
78

89
spec :: Spec
910
spec =
@@ -33,9 +34,9 @@ spec =
3334
it "Should get determinant of Double" $ do
3435
let eles = [[3 A.:+ 1, 8 A.:+ 1], [4 A.:+ 1, 6 A.:+ 1]]
3536
(x,y) = A.det (A.matrix @(A.Complex Double) (2,2) eles)
36-
x `shouldBe` (-14)
37+
x `shouldBeApprox` (-14)
3738
let (x,y) = A.det $ A.matrix @Double (2,2) [[3,8],[4,6]]
38-
x `shouldBe` (-14)
39+
x `shouldBeApprox` (-14)
3940
-- it "Should calculate inverse" $ do
4041
-- let x = flip A.inverse A.None $ A.matrix @Double (2,2) [[4.0,7.0],[2.0,6.0]]
4142
-- x `shouldBe` A.matrix (2,2) [[0.6,-0.7],[-0.2,0.4]]

test/ArrayFire/StatisticsSpec.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ArrayFire hiding (not)
55

66
import Data.Complex
77
import Test.Hspec
8+
import Test.Hspec.ApproxExpect
89

910
spec :: Spec
1011
spec =
@@ -15,7 +16,7 @@ spec =
1516
5.5
1617
it "Should find the weighted-mean" $ do
1718
meanWeighted (vector @Double 10 [1..]) (vector @Double 10 [1..]) 0
18-
`shouldBe`
19+
`shouldBeApprox`
1920
7.0
2021
it "Should find the variance" $ do
2122
var (vector @Double 8 [1..8]) False 0

test/Test/Hspec/ApproxExpect.hs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
module Test.Hspec.ApproxExpect where
4+
5+
import Data.CallStack (HasCallStack)
6+
7+
import Test.Hspec (shouldSatisfy, Expectation)
8+
9+
infix 1 `shouldBeApprox`
10+
11+
shouldBeApprox :: (HasCallStack, Show a, Fractional a, Eq a)
12+
=> a -> a -> Expectation
13+
shouldBeApprox actual tgt
14+
-- This is a hackish way of checking, without requiring a specific
15+
-- type or an 'Ord' instance, whether two floating-point values
16+
-- are only some epsilons apart: when the difference is small enough
17+
-- so scaling it down some more makes it a no-op for addition.
18+
= actual `shouldSatisfy` \x -> (x-tgt) * 1e-4 + tgt == tgt
19+

0 commit comments

Comments
 (0)