pass return_nan tests

This commit is contained in:
Ilya Rezvov
2018-04-12 14:17:01 -07:00
parent 2955456e26
commit b2a0d2c86e
2 changed files with 46 additions and 16 deletions
+30 -16
View File
@@ -57,8 +57,7 @@ import Language.Wasm.FloatUtils (
wordToFloat, wordToFloat,
floatToWord, floatToWord,
wordToDouble, wordToDouble,
doubleToWord, doubleToWord
makeNaN
) )
data Value = data Value =
@@ -92,6 +91,7 @@ asWord64 i
nearest :: (IEEE a) => a -> a nearest :: (IEEE a) => a -> a
nearest f nearest f
| isNaN f = f
| f >= 0 && f <= 0.5 = copySign 0 f | f >= 0 && f <= 0.5 = copySign 0 f
| f < 0 && f >= -0.5 = -0 | f < 0 && f >= -0.5 = -0
| otherwise = | otherwise =
@@ -110,34 +110,48 @@ nearest f
) )
zeroAwareMin :: IEEE a => a -> a -> a zeroAwareMin :: IEEE a => a -> a -> a
zeroAwareMin a b = zeroAwareMin a b
if identicalIEEE a 0 && identicalIEEE b (-0) | identicalIEEE a 0 && identicalIEEE b (-0) = b
then b | isNaN a = a
else minNum a b | isNaN b = b
| otherwise = minNum a b
zeroAwareMax :: IEEE a => a -> a -> a zeroAwareMax :: IEEE a => a -> a -> a
zeroAwareMax a b = zeroAwareMax a b
if identicalIEEE a (-0) && identicalIEEE b 0 | identicalIEEE a (-0) && identicalIEEE b 0 = b
then b | isNaN a = a
else maxNum a b | isNaN b = b
| otherwise = maxNum a b
floatFloor :: Float -> Float floatFloor :: Float -> Float
floatFloor a = copySign (fromIntegral (floor a :: Integer)) a floatFloor a
| isNaN a = a
| otherwise = copySign (fromIntegral (floor a :: Integer)) a
doubleFloor :: Double -> Double doubleFloor :: Double -> Double
doubleFloor a = copySign (fromIntegral (floor a :: Integer)) a doubleFloor a
| isNaN a = a
| otherwise = copySign (fromIntegral (floor a :: Integer)) a
floatCeil :: Float -> Float floatCeil :: Float -> Float
floatCeil a = copySign (fromIntegral (ceiling a :: Integer)) a floatCeil a
| isNaN a = a
| otherwise = copySign (fromIntegral (ceiling a :: Integer)) a
doubleCeil :: Double -> Double doubleCeil :: Double -> Double
doubleCeil a = copySign (fromIntegral (ceiling a :: Integer)) a doubleCeil a
| isNaN a = a
| otherwise = copySign (fromIntegral (ceiling a :: Integer)) a
floatTrunc :: Float -> Float floatTrunc :: Float -> Float
floatTrunc a = copySign (fromIntegral (truncate a :: Integer)) a floatTrunc a
| isNaN a = a
| otherwise = copySign (fromIntegral (truncate a :: Integer)) a
doubleTrunc :: Double -> Double doubleTrunc :: Double -> Double
doubleTrunc a = copySign (fromIntegral (truncate a :: Integer)) a doubleTrunc a
| isNaN a = a
| otherwise = copySign (fromIntegral (truncate a :: Integer)) a
data Label = Label ResultType deriving (Show, Eq) data Label = Label ResultType deriving (Show, Eq)
+16
View File
@@ -126,12 +126,28 @@ runScript onAssertFail script = do
isValueEqual (Interpreter.VF64 v1) (Interpreter.VF64 v2) = identicalIEEE v1 v2 isValueEqual (Interpreter.VF64 v1) (Interpreter.VF64 v2) = identicalIEEE v1 v2
isValueEqual _ _ = False isValueEqual _ _ = False
isNaNReturned :: ScriptState -> Action -> Assertion -> IO ()
isNaNReturned st action assert = do
result <- runAction st action
case result of
[Interpreter.VF32 v] ->
if isNaN v
then return ()
else onAssertFail ("Expected NaN, but action returned " ++ show v) assert
[Interpreter.VF64 v] ->
if isNaN v
then return ()
else onAssertFail ("Expected NaN, but action returned " ++ show v) assert
_ -> onAssertFail ("Expected NaN, but action returned " ++ show result) assert
runAssert :: ScriptState -> Assertion -> IO () runAssert :: ScriptState -> Assertion -> IO ()
runAssert st assert@(AssertReturn action expected) = do runAssert st assert@(AssertReturn action expected) = do
result <- runAction st action result <- runAction st action
if length result == length expected && (all id $ zipWith isValueEqual result (map asArg expected)) if length result == length expected && (all id $ zipWith isValueEqual result (map asArg expected))
then return () then return ()
else onAssertFail ("Expected " ++ show (map asArg expected) ++ ", but action returned " ++ show result) assert else onAssertFail ("Expected " ++ show (map asArg expected) ++ ", but action returned " ++ show result) assert
runAssert st assert@(AssertReturnCanonicalNaN action) = isNaNReturned st action assert
runAssert st assert@(AssertReturnArithmeticNaN action) = isNaNReturned st action assert
runAssert _ _ = return () runAssert _ _ = return ()
runCommand :: ScriptState -> Command -> IO ScriptState runCommand :: ScriptState -> Command -> IO ScriptState