File: Main.hs

package info (click to toggle)
haskell-reinterpret-cast 0.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68 kB
  • sloc: haskell: 130; makefile: 3
file content (51 lines) | stat: -rw-r--r-- 1,764 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# LANGUAGE BangPatterns #-}

import           Control.Loop (numLoop)
import           Control.Monad
import           Data.Word
import           Test.Hspec

import qualified Data.ReinterpretCast as Current
import qualified Data.ReinterpretCast.Internal.ImplArray as Array
import qualified Data.ReinterpretCast.Internal.ImplFFI as FFI
-- Keep comparing against the data-binary-ieee754 package in case that changes.
import qualified Data.Binary.IEEE754 as IEEE


main :: IO ()
main = hspec $ do

  describe "Current" $ do

    it "wordToFloat" $ do
      Current.wordToFloat 1 `shouldBe` 1.0e-45


  describe "exhaustive testing (~ 2 minutes per test)" $ do

    it "Word32 / float" $ do
      numLoop 0 (maxBound :: Word32) $ \w ->
        when (Current.floatToWord (Current.wordToFloat w) /= w) $
          expectationFailure $ "Failed: " ++ show w


  describe "Comparing implementations" $ do

    let n32 = 1000000 :: Word32

    it ("[0.." ++ show n32 ++ "] Word32 / float") $ do
      numLoop 0 n32 $ \w -> do
        let f = IEEE.wordToFloat w
        when (f /= Current.wordToFloat w) $ expectationFailure $ "Current failed: " ++ show w
        when (f /= Array.wordToFloat w)   $ expectationFailure $ "Array failed: " ++ show w
        when (f /= FFI.wordToFloat w)     $ expectationFailure $ "FFI failed: " ++ show w


    let n64 = 1000000 :: Word64

    it ("[0.." ++ show n64 ++ "] Word64 / double") $ do
      numLoop 0 n64 $ \w -> do
        let f = IEEE.wordToDouble w
        when (f /= Current.wordToDouble w) $ expectationFailure $ "Current failed: " ++ show w
        when (f /= Array.wordToDouble w)   $ expectationFailure $ "Array failed: " ++ show w
        when (f /= FFI.wordToDouble w)     $ expectationFailure $ "FFI failed: " ++ show w