Permalink
Browse files

tests: import ghc-7.6 numeric testsuite

  • Loading branch information...
1 parent 43fdb0c commit afbbd6c2b2bcc107d95c1bc7129a019dba69e617 @kfish committed Oct 12, 2012
@@ -0,0 +1,7 @@
+module Main where
+
+main = print (syn [-1])
+
+syn :: [Int] -> [Int]
+syn (d:ds) = rem d 0x40000000 : syn ds
+syn [] = []
View
@@ -0,0 +1,32 @@
+-- test conversion of funny numbers through Rational (#3676)
+
+main = mapM_ putStrLn $ concat $
+ [map (show.d2d) doubles1,
+ map (show.d2f) doubles1,
+ map (show.d2r) doubles1,
+ map (show.f2d) floats1,
+ map (show.f2f) floats1,
+ map (show.f2r) floats1,
+ map (show.d2d) doubles2,
+ map (show.d2f) doubles2,
+ map (show.d2r) doubles2,
+ map (show.f2d) floats2,
+ map (show.f2f) floats2,
+ map (show.f2r) floats2
+ ]
+
+d2d = realToFrac :: Double -> Double
+d2f = realToFrac :: Double -> Float
+d2r = realToFrac :: Double -> Rational
+f2d = realToFrac :: Float -> Double
+f2f = realToFrac :: Float -> Float
+f2r = realToFrac :: Float -> Rational
+
+doubles1 = [0/0, 1/0, -1/0, 0/(-1)] :: [Double]
+floats1 = [0/0, 1/0, -1/0, 0/(-1)] :: [Float]
+
+doubles2 = names :: [Double]
+floats2 = names :: [Float]
+
+names :: Read a => [a]
+names = map read ["NaN", "Infinity", "-Infinity", "-0"]
@@ -0,0 +1,5 @@
+module Main where
+
+main = do
+ print (scaleFloat 30000 1)
+ print (scaleFloat (maxBound :: Int) 1)
@@ -0,0 +1 @@
+main = print (0.5 ^ 1030)
@@ -0,0 +1,9 @@
+
+ERROR_TESTS= $(wildcard *.hs)
+
+include ../cmpoutput.mak
+
+all :: $(patsubst %, check.%, $(ERROR_TESTS))
+
+clean:
+ rm -f *.o *.hi *.exe *.stdout *.stderr
@@ -0,0 +1,28 @@
+-- !!! conversions: Double <=> Rational/Integer things
+--
+import Data.Ratio
+
+main = putStr (show r42 ++ "\n" ++
+ show nu42 ++ ", " ++
+ show de42 ++ "\n" ++
+ show nu42d ++ ", " ++
+ show de42d ++ "\n" ++
+ show s2 ++ ", " ++
+ show e2 ++ "\n" ++
+ show s ++ ", " ++
+ show e ++ "\n" )
+ where
+ d42 :: Double
+ r42 :: Rational
+ nu42, de42 :: Integer
+ nu42d, de42d :: Double
+
+ d42 = 42
+ r42 = toRational d42
+ nu42 = numerator r42
+ de42 = denominator r42
+ nu42d= fromInteger nu42
+ de42d= fromInteger de42
+
+ (s,e)= decodeFloat (nu42d / de42d )
+ (s2,e2) = decodeFloat d42
@@ -0,0 +1,52 @@
+-- !!! basic Rational operations
+--
+import Data.Ratio
+
+main
+ = putStr
+ (-- Ratio Ints
+ show [i0a, i0b, i0c, i2a, i2b, im2a, im2b, i_pi, i_misc]
+ ++ "\n"
+ -- the Ints
+ ++ show ((map numerator [i0a, i0b, i0c, i2a, i2b, im2a, im2b, i_pi, i_misc])
+ ++(map denominator [i0a, i0b, i0c, i2a, i2b, im2a, im2b, i_pi, i_misc]))
+ ++ "\n"
+ -- Booleans
+-- ++ show []
+-- ++ "\n"
+
+ -- Rationals (Ratio Integers)
+ ++ show [r0a, r0b, r0c, r2a, r2b, rm2a, rm2b, r_pi, r_misc]
+ ++ "\n"
+ -- the Integers
+ ++ show ((map numerator [r0a, r0b, r0c, r2a, r2b, rm2a, rm2b, r_pi, r_misc])
+ ++(map denominator [r0a, r0b, r0c, r2a, r2b, rm2a, rm2b, r_pi, r_misc]))
+ ++ "\n"
+ -- Booleans
+-- ++ show []
+-- ++ "\n"
+ )
+ where
+ i0a, i0b, i0c, i2a, i2b, im2a, im2b, i_pi, i_misc :: Ratio Int
+
+ i0a = 0 % 1
+ i0b = (-0) % 1
+ i0c = 0 % (-1)
+ i2a = 4 % 2
+ i2b = (-4) % (-2)
+ im2a = (-4) % 2
+ im2b = 4 % (-2)
+ i_pi = 22 % 7
+ i_misc = 2 % 10000
+
+ r0a, r0b, r0c, r2a, r2b, rm2a, rm2b, r_pi, r_misc :: Rational
+
+ r0a = 0 % 1
+ r0b = (-0) % 1
+ r0c = 0 % (-1)
+ r2a = 4 % 2
+ r2b = (-4) % (-2)
+ rm2a = (-4) % 2
+ rm2b = 4 % (-2)
+ r_pi = 22 % 7
+ r_misc = 2 % 10000
@@ -0,0 +1,84 @@
+-- $Id: arith003.hs,v 1.2 2002/01/25 13:40:39 simonmar Exp $
+--
+-- !!! test Int/Integer arithmetic operations from the Prelude.
+--
+
+main
+ = putStr
+ (
+ showit (do_ops int_ops) ++
+ showit (do_ops integer_ops)
+ )
+
+showit :: (Show a, Integral a) => [(String, a, a, a)] -> String
+showit stuff = concat
+ [ str ++ " " ++ show l ++ " " ++ show r ++ " = " ++ show result ++ "\n"
+ | (str, l, r, result) <- stuff
+ ]
+
+do_ops :: Integral a => [((a -> a -> a), String, [(a,a)])]
+ -> [(String, a, a, a)]
+do_ops ops = [ (str, l, r, l `op` r) | (op,str,args) <- ops, (l,r) <- args ]
+
+small_operands, non_min_operands, operands, non_max_operands
+ :: Integral a => [a]
+small_operands = [ 0, 1, -1, 2, -2 ]
+operands = small_operands ++ [ fromIntegral minInt, fromIntegral maxInt ]
+non_min_operands = small_operands ++ [ fromIntegral maxInt ]
+non_max_operands = small_operands ++ [ fromIntegral minInt ]
+
+large_operands :: [ Integer ]
+large_operands = operands ++
+ [ fromIntegral minInt - 1,
+ fromIntegral maxInt + 1,
+ fromIntegral minInt * 2,
+ fromIntegral maxInt * 2,
+ fromIntegral minInt ^ 2,
+ fromIntegral maxInt ^ 2
+ ]
+
+integer_ops :: [((Integer -> Integer -> Integer), String, [(Integer,Integer)])]
+integer_ops = [
+ ((+), "(+)", both_large),
+ ((-), "(-)", both_large),
+ (div, "div", large_non_zero_r),
+ (mod, "mod", large_non_zero_r),
+ (quot, "quot", large_non_zero_r),
+ (rem, "rem", large_non_zero_r),
+ (gcd, "gcd", large_either_non_zero),
+ (lcm, "lcm", large_either_non_zero)
+ ]
+
+int_ops :: [((Int -> Int -> Int), String, [(Int,Int)])]
+int_ops = [
+ ((+), "(+)", both_small),
+ ((-), "(-)", both_small),
+ ((^), "(^)", small_non_neg_r),
+ (div, "div", non_min_l_or_zero_r),
+ (mod, "mod", non_min_l_or_zero_r),
+ (quot, "quot", non_min_l_or_zero_r),
+ (rem, "rem", non_min_l_or_zero_r),
+ (gcd, "gcd", non_min_either_non_zero),
+ (lcm, "lcm", non_max_r_either_non_zero)
+ ]
+
+-- NOTE: when abs(minInt) is undefined (it is in GHC, because
+-- abs(minInt) would be greater than maxInt), then gcd on Ints is also
+-- undefined when either operand is minInt.
+
+both_small, non_zero_r, non_min_either_non_zero, non_min_l_or_zero_r,
+ non_max_r_either_non_zero, small_non_neg_r
+ :: Integral a => [(a,a)]
+
+both_small = [ (l,r) | l <- operands, r <- operands ]
+both_large = [ (l,r) | l <- large_operands, r <- large_operands ]
+large_non_zero_r = [ (l,r) | l <- operands, r <- large_operands, r /= 0 ]
+non_zero_r = [ (l,r) | l <- operands, r <- operands, r /= 0 ]
+non_min_either_non_zero = [ (l,r) | l <- non_min_operands, r <- non_min_operands, l /= 0 || r /= 0 ]
+large_either_non_zero = [ (l,r) | l <- operands, r <- operands, l /= 0 || r /= 0 ]
+small_non_neg_r = [ (l,r) | l <- operands, r <- small_operands, r >= 0 ]
+non_min_l_or_zero_r = [ (l,r) | l <- non_min_operands, r <- operands, r /= 0 ]
+non_max_r_either_non_zero = [ (l,r) | l <- operands, r <- non_max_operands, l /= 0 || r /= 0 ]
+
+minInt = minBound :: Int
+maxInt = maxBound :: Int
@@ -0,0 +1,86 @@
+-- !!! test quot/rem/div/mod functions on Ints and Integers
+--
+main
+ = putStr
+ (-- w/ Ints and Integers
+ show (unzipWith div ints_list)
+ ++ "\n"
+ ++ show (unzipWith div integers_list)
+ ++ "\n"
+ ++ show (unzipWith rem ints_list)
+ ++ "\n"
+ ++ show (unzipWith rem integers_list)
+ ++ "\n"
+ ++ show (unzipWith quot ints_list)
+ ++ "\n"
+ ++ show (unzipWith quot integers_list)
+ ++ "\n"
+ ++ show (unzipWith mod ints_list)
+ ++ "\n"
+ ++ show (unzipWith mod integers_list)
+ ++ "\n"
+ ++ show (unzipWith law1 ints_list)
+ ++ "\n"
+ ++ show (unzipWith law1 integers_list)
+ ++ "\n"
+ ++ show (unzipWith law2 ints_list)
+ ++ "\n"
+ ++ show (unzipWith law2 integers_list)
+ ++ "\n"
+ )
+ where
+ ints_list :: [(Int, Int)]
+ integers_list :: [(Integer, Integer)]
+
+ ints_list = [
+ (0, 4),
+ (0, -8),
+ (7, 3),
+ (13, 4),
+ (13, -4),
+ (-13, 4),
+ (-13, -4),
+ (12345678, 10000),
+ (12345678, -10000),
+ (-12345678, 10000),
+ (-12345678, -10000),
+ (123456,10000),
+ (1234567,20000),
+ (12345678,-10000),
+ (123456789,10000),
+ (1234567890,-10000),
+ (-12345,10000),
+ (-123456789,-10000)
+ ]
+
+ integers_list = [
+ (0, 4),
+ (0, -8),
+ (7, 3),
+ (13, 4),
+ (13, -4),
+ (-13, 4),
+ (-13, -4),
+ (12345678, 10000),
+ (12345678, -10000),
+ (-12345678, 10000),
+ (-12345678, -10000),
+ (123456,10000),
+ (1234567,20000),
+ (12345678,-10000),
+ (123456789,10000),
+ (1234567890,-10000),
+ (-12345,10000),
+ (-123456789,-10000),
+ (12345678900,500000000),
+ (1234000000000000000000005678900,5001111111111111000000)
+ ]
+
+unzipWith :: (a -> b -> c) -> [(a,b)] -> [c]
+unzipWith f [] = []
+unzipWith f ((x,y):zs) = f x y : unzipWith f zs
+
+law1, law2 :: Integral a => a -> a -> Bool
+
+law1 x y = (x `quot` y)*y + (x `rem` y) == x
+law2 x y = (x `div` y)*y + (x `mod` y) == x
@@ -0,0 +1,60 @@
+-- !!! test RealFrac ops (ceiling/floor/etc.) on Floats/Doubles
+--
+main =
+ putStr $
+ unlines
+ [ -- just for fun, we show the floats to
+ -- exercise the code responsible.
+ 'A' : show (float_list :: [Float])
+ , 'B' : show (double_list :: [Double])
+ -- {Float,Double} inputs, {Int,Integer} outputs
+ , 'C' : show ((map ceiling small_float_list) :: [Int])
+ , 'D' : show ((map ceiling float_list) :: [Integer])
+ , 'E' : show ((map ceiling small_double_list) :: [Int])
+ , 'F' : show ((map ceiling double_list) :: [Integer])
+ , 'G' : show ((map floor small_float_list) :: [Int])
+ , 'H' : show ((map floor float_list) :: [Integer])
+ , 'I' : show ((map floor small_double_list) :: [Int])
+ , 'J' : show ((map floor double_list) :: [Integer])
+ , 'K' : show ((map truncate small_float_list) :: [Int])
+ , 'L' : show ((map truncate float_list) :: [Integer])
+ , 'M' : show ((map truncate small_double_list) :: [Int])
+ , 'N' : show ((map truncate double_list) :: [Integer])
+ , 'n' : show ((map round small_float_list) :: [Int])
+ , 'O' : show ((map round float_list) :: [Integer])
+ , 'P' : show ((map round small_double_list) :: [Int])
+ , 'Q' : show ((map round double_list) :: [Integer])
+ , 'R' : show ((map properFraction small_float_list) :: [(Int,Float)])
+ , 'S' : show ((map properFraction float_list) :: [(Integer,Float)])
+ , 'T' : show ((map properFraction small_double_list) :: [(Int,Double)])
+ , 'U' : show ((map properFraction double_list) :: [(Integer,Double)])
+ ]
+ where
+ -- these fit into an Int when truncated. Truncation when the
+ -- result does not fit into the target is undefined - not explicitly
+ -- so in Haskell 98, but that's the interpretation we've taken in GHC.
+ -- See bug #1254
+ small_float_list :: [Float]
+ small_float_list = [
+ 0.0, -0.0, 1.1, 2.8, 3.5, 4.5, -1.0000000001, -2.9999995,
+ -3.50000000001, -4.49999999999, 1000012.0, 123.456, 100.25,
+ 102.5, 0.0012, -0.00000012, 1.7e4, -1.7e-4, 0.15e-6, pi
+ ]
+
+ float_list :: [Float]
+ float_list = small_float_list ++ [
+ 1.18088e+11, 1.2111e+14
+ ]
+
+ -- these fit into an Int
+ small_double_list :: [Double]
+ small_double_list = [
+ 0.0, -0.0, 1.1, 2.8, 3.5, 4.5, -1.0000000001, -2.9999995,
+ -3.50000000001, -4.49999999999, 1000012.0, 123.456, 100.25,
+ 102.5, 0.0012, -0.00000012, 1.7e4, -1.7e-4, 0.15e-6, pi
+ ]
+
+ double_list :: [Double]
+ double_list = small_double_list ++ [
+ 1.18088e+11, 1.2111e+14
+ ]
@@ -0,0 +1,4 @@
+-- !!! printing Floats; was a bug in hbc (reported by andy)
+--
+
+main = print ((fromIntegral (42 :: Int)) :: Float)
Oops, something went wrong.

0 comments on commit afbbd6c

Please sign in to comment.