convertible 1.0.11.1 → 1.1.1.1
raw patch · 15 files changed
Files
- Data/Convertible.hs +2/−3
- Data/Convertible/Base.hs +11/−6
- Data/Convertible/Instances/C.hs +40/−40
- Data/Convertible/Instances/Num.hs +196/−196
- Data/Convertible/Instances/Text.hs +173/−22
- Data/Convertible/Instances/Time.hs +9/−22
- Data/Convertible/Utils.hs +5/−14
- Makefile +0/−30
- convertible.cabal +34/−50
- testsrc/TestInfrastructure.hs +0/−15
- testsrc/TestMap.hs +13/−9
- testsrc/TestNum.hs +49/−34
- testsrc/TestTime.hs +60/−56
- testsrc/runtests.hs +5/−11
- utils/genCinstances.hs +3/−4
Data/Convertible.hs view
@@ -31,10 +31,9 @@ module Data.Convertible ( module Data.Convertible.Base,- module Data.Convertible.Utils,- module Data.Convertible.Instances+ module Data.Convertible.Utils ) where import Data.Convertible.Base import Data.Convertible.Utils-import Data.Convertible.Instances+import Data.Convertible.Instances ()
Data/Convertible/Base.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {- Copyright (C) 2009-2011 John Goerzen <jgoerzen@complete.org> @@ -28,7 +29,9 @@ prettyConvertError ) where+#if !MIN_VERSION_mtl(2,3,0) import Control.Monad.Error+#endif import Data.Typeable {- | The result of a safe conversion via 'safeConvert'. -}@@ -61,7 +64,7 @@ an error with the conversion. For a function that does not raise an exception in that case, see 'safeConvert'. -} convert :: Convertible a b => a -> b-convert x = +convert x = case safeConvert x of Left e -> error (prettyConvertError e) Right r -> r@@ -89,25 +92,27 @@ convErrorMessage :: String} deriving (Eq, Read, Show) +#if !MIN_VERSION_mtl(2,3,0) instance Error ConvertError where strMsg x = ConvertError "(unknown)" "(unknown)" "(unknown)" x+#endif convError' :: (Show a, Typeable a, Typeable b) => String -> a -> b -> ConvertResult b-convError' msg inpval retval = +convError' msg inpval retval = Left $ ConvertError { convSourceValue = show inpval, convSourceType = show . typeOf $ inpval, convDestType = show . typeOf $ retval, convErrorMessage = msg}- + convError :: (Show a, Typeable a, Typeable b) => String -> a -> ConvertResult b-convError msg inpval = +convError msg inpval = convError' msg inpval undefined- + prettyConvertError :: ConvertError -> String prettyConvertError (ConvertError sv st dt em) = "Convertible: error converting source data " ++ sv ++ " of type " ++ st ++ " to type " ++ dt ++ ": " ++ em- +
Data/Convertible/Instances/C.hs view
@@ -29,104 +29,104 @@ -- remainder of this file generated by utils/genCinstances.hs -- Section 1-instance Convertible CFloat Int where +instance Convertible CFloat Int where safeConvert = boundedConversion (return . truncate)-instance Convertible Int CFloat where +instance Convertible Int CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Int8 where +instance Convertible CFloat Int8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int8 CFloat where +instance Convertible Int8 CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Int16 where +instance Convertible CFloat Int16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int16 CFloat where +instance Convertible Int16 CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Int32 where +instance Convertible CFloat Int32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int32 CFloat where +instance Convertible Int32 CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Int64 where +instance Convertible CFloat Int64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int64 CFloat where +instance Convertible Int64 CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Word where +instance Convertible CFloat Word where safeConvert = boundedConversion (return . truncate)-instance Convertible Word CFloat where +instance Convertible Word CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Word8 where +instance Convertible CFloat Word8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word8 CFloat where +instance Convertible Word8 CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Word16 where +instance Convertible CFloat Word16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word16 CFloat where +instance Convertible Word16 CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Word32 where +instance Convertible CFloat Word32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word32 CFloat where +instance Convertible Word32 CFloat where safeConvert = return . fromIntegral -instance Convertible CFloat Word64 where +instance Convertible CFloat Word64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word64 CFloat where +instance Convertible Word64 CFloat where safeConvert = return . fromIntegral -instance Convertible CDouble Int where +instance Convertible CDouble Int where safeConvert = boundedConversion (return . truncate)-instance Convertible Int CDouble where +instance Convertible Int CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Int8 where +instance Convertible CDouble Int8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int8 CDouble where +instance Convertible Int8 CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Int16 where +instance Convertible CDouble Int16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int16 CDouble where +instance Convertible Int16 CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Int32 where +instance Convertible CDouble Int32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int32 CDouble where +instance Convertible Int32 CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Int64 where +instance Convertible CDouble Int64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int64 CDouble where +instance Convertible Int64 CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Word where +instance Convertible CDouble Word where safeConvert = boundedConversion (return . truncate)-instance Convertible Word CDouble where +instance Convertible Word CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Word8 where +instance Convertible CDouble Word8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word8 CDouble where +instance Convertible Word8 CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Word16 where +instance Convertible CDouble Word16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word16 CDouble where +instance Convertible Word16 CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Word32 where +instance Convertible CDouble Word32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word32 CDouble where +instance Convertible Word32 CDouble where safeConvert = return . fromIntegral -instance Convertible CDouble Word64 where +instance Convertible CDouble Word64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word64 CDouble where +instance Convertible Word64 CDouble where safeConvert = return . fromIntegral -- Section 2
Data/Convertible/Instances/Num.hs view
@@ -48,7 +48,7 @@ "Word64"] allItems l1 l2 = concatMap (\x -> map (\y -> (x, y)) int) fp work = allItems fp int-printIt (f, i) = +printIt (f, i) = "instance Convertible " ++ f ++ " " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . truncate)\n\ \instance Convertible " ++ i ++ " " ++ f ++ " where \n\@@ -57,154 +57,154 @@ main = mapM_ (putStrLn . printIt) work -} -instance Convertible Double Int where +instance Convertible Double Int where safeConvert = boundedConversion (return . truncate)-instance Convertible Int Double where +instance Convertible Int Double where safeConvert = return . fromIntegral -instance Convertible Double Int8 where +instance Convertible Double Int8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int8 Double where +instance Convertible Int8 Double where safeConvert = return . fromIntegral -instance Convertible Double Int16 where +instance Convertible Double Int16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int16 Double where +instance Convertible Int16 Double where safeConvert = return . fromIntegral -instance Convertible Double Int32 where +instance Convertible Double Int32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int32 Double where +instance Convertible Int32 Double where safeConvert = return . fromIntegral -instance Convertible Double Int64 where +instance Convertible Double Int64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int64 Double where +instance Convertible Int64 Double where safeConvert = return . fromIntegral -instance Convertible Double Word where +instance Convertible Double Word where safeConvert = boundedConversion (return . truncate)-instance Convertible Word Double where +instance Convertible Word Double where safeConvert = return . fromIntegral -instance Convertible Double Word8 where +instance Convertible Double Word8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word8 Double where +instance Convertible Word8 Double where safeConvert = return . fromIntegral -instance Convertible Double Word16 where +instance Convertible Double Word16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word16 Double where +instance Convertible Word16 Double where safeConvert = return . fromIntegral -instance Convertible Double Word32 where +instance Convertible Double Word32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word32 Double where +instance Convertible Word32 Double where safeConvert = return . fromIntegral -instance Convertible Double Word64 where +instance Convertible Double Word64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word64 Double where +instance Convertible Word64 Double where safeConvert = return . fromIntegral -instance Convertible Float Int where +instance Convertible Float Int where safeConvert = boundedConversion (return . truncate)-instance Convertible Int Float where +instance Convertible Int Float where safeConvert = return . fromIntegral -instance Convertible Float Int8 where +instance Convertible Float Int8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int8 Float where +instance Convertible Int8 Float where safeConvert = return . fromIntegral -instance Convertible Float Int16 where +instance Convertible Float Int16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int16 Float where +instance Convertible Int16 Float where safeConvert = return . fromIntegral -instance Convertible Float Int32 where +instance Convertible Float Int32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int32 Float where +instance Convertible Int32 Float where safeConvert = return . fromIntegral -instance Convertible Float Int64 where +instance Convertible Float Int64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int64 Float where +instance Convertible Int64 Float where safeConvert = return . fromIntegral -instance Convertible Float Word where +instance Convertible Float Word where safeConvert = boundedConversion (return . truncate)-instance Convertible Word Float where +instance Convertible Word Float where safeConvert = return . fromIntegral -instance Convertible Float Word8 where +instance Convertible Float Word8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word8 Float where +instance Convertible Word8 Float where safeConvert = return . fromIntegral -instance Convertible Float Word16 where +instance Convertible Float Word16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word16 Float where +instance Convertible Word16 Float where safeConvert = return . fromIntegral -instance Convertible Float Word32 where +instance Convertible Float Word32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word32 Float where +instance Convertible Word32 Float where safeConvert = return . fromIntegral -instance Convertible Float Word64 where +instance Convertible Float Word64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word64 Float where +instance Convertible Word64 Float where safeConvert = return . fromIntegral -instance Convertible Rational Int where +instance Convertible Rational Int where safeConvert = boundedConversion (return . truncate)-instance Convertible Int Rational where +instance Convertible Int Rational where safeConvert = return . fromIntegral -instance Convertible Rational Int8 where +instance Convertible Rational Int8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int8 Rational where +instance Convertible Int8 Rational where safeConvert = return . fromIntegral -instance Convertible Rational Int16 where +instance Convertible Rational Int16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int16 Rational where +instance Convertible Int16 Rational where safeConvert = return . fromIntegral -instance Convertible Rational Int32 where +instance Convertible Rational Int32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int32 Rational where +instance Convertible Int32 Rational where safeConvert = return . fromIntegral -instance Convertible Rational Int64 where +instance Convertible Rational Int64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Int64 Rational where +instance Convertible Int64 Rational where safeConvert = return . fromIntegral -instance Convertible Rational Word where +instance Convertible Rational Word where safeConvert = boundedConversion (return . truncate)-instance Convertible Word Rational where +instance Convertible Word Rational where safeConvert = return . fromIntegral -instance Convertible Rational Word8 where +instance Convertible Rational Word8 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word8 Rational where +instance Convertible Word8 Rational where safeConvert = return . fromIntegral -instance Convertible Rational Word16 where +instance Convertible Rational Word16 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word16 Rational where +instance Convertible Word16 Rational where safeConvert = return . fromIntegral -instance Convertible Rational Word32 where +instance Convertible Rational Word32 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word32 Rational where +instance Convertible Word32 Rational where safeConvert = return . fromIntegral -instance Convertible Rational Word64 where +instance Convertible Rational Word64 where safeConvert = boundedConversion (return . truncate)-instance Convertible Word64 Rational where +instance Convertible Word64 Rational where safeConvert = return . fromIntegral @@ -215,7 +215,7 @@ "Word64"] allItems l1 l2 = concatMap (\x -> map (\y -> (x, y)) l1) l2 work = filter (\(a, b) -> a /= b) (allItems int int)-printIt (f, i) = +printIt (f, i) = "instance Convertible " ++ f ++ " " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . fromIntegral)\n" @@ -229,333 +229,333 @@ mapM_ (putStrLn . printInteger) int -} -instance Convertible Int Int8 where +instance Convertible Int Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Int16 where +instance Convertible Int Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Int32 where +instance Convertible Int Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Int64 where +instance Convertible Int Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Word where +instance Convertible Int Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Word8 where +instance Convertible Int Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Word16 where +instance Convertible Int Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Word32 where +instance Convertible Int Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int Word64 where +instance Convertible Int Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Int where +instance Convertible Int8 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Int16 where +instance Convertible Int8 Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Int32 where +instance Convertible Int8 Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Int64 where +instance Convertible Int8 Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Word where +instance Convertible Int8 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Word8 where +instance Convertible Int8 Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Word16 where +instance Convertible Int8 Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Word32 where +instance Convertible Int8 Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int8 Word64 where +instance Convertible Int8 Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Int where +instance Convertible Int16 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Int8 where +instance Convertible Int16 Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Int32 where +instance Convertible Int16 Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Int64 where +instance Convertible Int16 Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Word where +instance Convertible Int16 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Word8 where +instance Convertible Int16 Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Word16 where +instance Convertible Int16 Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Word32 where +instance Convertible Int16 Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int16 Word64 where +instance Convertible Int16 Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Int where +instance Convertible Int32 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Int8 where +instance Convertible Int32 Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Int16 where +instance Convertible Int32 Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Int64 where +instance Convertible Int32 Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Word where +instance Convertible Int32 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Word8 where +instance Convertible Int32 Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Word16 where +instance Convertible Int32 Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Word32 where +instance Convertible Int32 Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int32 Word64 where +instance Convertible Int32 Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Int where +instance Convertible Int64 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Int8 where +instance Convertible Int64 Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Int16 where +instance Convertible Int64 Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Int32 where +instance Convertible Int64 Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Word where +instance Convertible Int64 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Word8 where +instance Convertible Int64 Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Word16 where +instance Convertible Int64 Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Word32 where +instance Convertible Int64 Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Int64 Word64 where +instance Convertible Int64 Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Int where +instance Convertible Word Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Int8 where +instance Convertible Word Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Int16 where +instance Convertible Word Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Int32 where +instance Convertible Word Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Int64 where +instance Convertible Word Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Word8 where +instance Convertible Word Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Word16 where +instance Convertible Word Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Word32 where +instance Convertible Word Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word Word64 where +instance Convertible Word Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Int where +instance Convertible Word8 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Int8 where +instance Convertible Word8 Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Int16 where +instance Convertible Word8 Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Int32 where +instance Convertible Word8 Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Int64 where +instance Convertible Word8 Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Word where +instance Convertible Word8 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Word16 where +instance Convertible Word8 Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Word32 where +instance Convertible Word8 Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word8 Word64 where +instance Convertible Word8 Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Int where +instance Convertible Word16 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Int8 where +instance Convertible Word16 Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Int16 where +instance Convertible Word16 Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Int32 where +instance Convertible Word16 Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Int64 where +instance Convertible Word16 Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Word where +instance Convertible Word16 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Word8 where +instance Convertible Word16 Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Word32 where +instance Convertible Word16 Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word16 Word64 where +instance Convertible Word16 Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Int where +instance Convertible Word32 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Int8 where +instance Convertible Word32 Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Int16 where +instance Convertible Word32 Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Int32 where +instance Convertible Word32 Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Int64 where +instance Convertible Word32 Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Word where +instance Convertible Word32 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Word8 where +instance Convertible Word32 Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Word16 where +instance Convertible Word32 Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word32 Word64 where +instance Convertible Word32 Word64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Int where +instance Convertible Word64 Int where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Int8 where +instance Convertible Word64 Int8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Int16 where +instance Convertible Word64 Int16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Int32 where +instance Convertible Word64 Int32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Int64 where +instance Convertible Word64 Int64 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Word where +instance Convertible Word64 Word where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Word8 where +instance Convertible Word64 Word8 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Word16 where +instance Convertible Word64 Word16 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Word64 Word32 where +instance Convertible Word64 Word32 where safeConvert = boundedConversion (return . fromIntegral) -instance Convertible Integer Int where +instance Convertible Integer Int where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Int Integer where +instance Convertible Int Integer where safeConvert = return . fromIntegral -instance Convertible Integer Int8 where +instance Convertible Integer Int8 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Int8 Integer where +instance Convertible Int8 Integer where safeConvert = return . fromIntegral -instance Convertible Integer Int16 where +instance Convertible Integer Int16 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Int16 Integer where +instance Convertible Int16 Integer where safeConvert = return . fromIntegral -instance Convertible Integer Int32 where +instance Convertible Integer Int32 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Int32 Integer where +instance Convertible Int32 Integer where safeConvert = return . fromIntegral -instance Convertible Integer Int64 where +instance Convertible Integer Int64 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Int64 Integer where +instance Convertible Int64 Integer where safeConvert = return . fromIntegral -instance Convertible Integer Word where +instance Convertible Integer Word where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Word Integer where +instance Convertible Word Integer where safeConvert = return . fromIntegral -instance Convertible Integer Word8 where +instance Convertible Integer Word8 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Word8 Integer where +instance Convertible Word8 Integer where safeConvert = return . fromIntegral -instance Convertible Integer Word16 where +instance Convertible Integer Word16 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Word16 Integer where +instance Convertible Word16 Integer where safeConvert = return . fromIntegral -instance Convertible Integer Word32 where +instance Convertible Integer Word32 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Word32 Integer where +instance Convertible Word32 Integer where safeConvert = return . fromIntegral -instance Convertible Integer Word64 where +instance Convertible Integer Word64 where safeConvert = boundedConversion (return . fromIntegral)-instance Convertible Word64 Integer where +instance Convertible Word64 Integer where safeConvert = return . fromIntegral @@ -598,7 +598,7 @@ int = ["Int", "Int8", "Int16", "Int32", "Int64", "Word", "Word8", "Word16", "Word32", "Word64"]-printIt i = +printIt i = "instance Convertible Char " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . fromIntegral . fromEnum)\n\ \instance Convertible " ++ i ++ " Char where \n\@@ -607,65 +607,65 @@ main = do mapM_ (putStrLn . printIt) int -} -instance Convertible Char Int where - safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Int Char where - safeConvert = boundedConversion (return . toEnum . fromIntegral)+instance Convertible Char Int where+ safeConvert = boundedConversion (return . fromEnum)+instance Convertible Int Char where+ safeConvert = boundedConversion (return . toEnum) -instance Convertible Char Int8 where +instance Convertible Char Int8 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Int8 Char where +instance Convertible Int8 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Int16 where +instance Convertible Char Int16 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Int16 Char where +instance Convertible Int16 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Int32 where +instance Convertible Char Int32 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Int32 Char where +instance Convertible Int32 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Int64 where +instance Convertible Char Int64 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Int64 Char where +instance Convertible Int64 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Word where +instance Convertible Char Word where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Word Char where +instance Convertible Word Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Word8 where +instance Convertible Char Word8 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Word8 Char where +instance Convertible Word8 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Word16 where +instance Convertible Char Word16 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Word16 Char where +instance Convertible Word16 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Word32 where +instance Convertible Char Word32 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Word32 Char where +instance Convertible Word32 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) -instance Convertible Char Word64 where +instance Convertible Char Word64 where safeConvert = boundedConversion (return . fromIntegral . fromEnum)-instance Convertible Word64 Char where +instance Convertible Word64 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Integer Integer where- safeConvert = return . id+ safeConvert = return
Data/Convertible/Instances/Text.hs view
@@ -30,55 +30,206 @@ import Data.Convertible.Base import qualified Data.Text as TS+import qualified Data.Text.Encoding as TE import qualified Data.Text.Lazy as TL+import qualified Data.Text.Lazy.Builder as TLB+import qualified Data.Text.Lazy.Encoding as TLE import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as BL+import qualified Data.ByteString.Builder as BB import Data.Word (Word8)+import Data.Foldable --- Text -instance Convertible [Char] TS.Text where- safeConvert = Right . TS.pack instance Convertible TS.Text [Char] where+ {-# INLINE safeConvert #-} safeConvert = Right . TS.unpack -instance Convertible [Char] TL.Text where- safeConvert = Right . TL.pack+instance Convertible TS.Text TL.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TL.fromStrict +instance Convertible TS.Text TLB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TLB.fromText++instance Convertible TS.Text BS.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TE.encodeUtf8++instance Convertible TS.Text BL.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BL.fromStrict . TE.encodeUtf8++instance Convertible TS.Text BB.Builder where+ {-# INLINE safeConvert #-}+#if MIN_VERSION_text(1,2,0)+ safeConvert = Right . TE.encodeUtf8Builder+#else+ safeConvert = safeConvert . TE.encodeUtf8+#endif+++ instance Convertible TL.Text [Char] where+ {-# INLINE safeConvert #-} safeConvert = Right . TL.unpack -#if MIN_VERSION_text(0,8,1)-instance Convertible TS.Text TL.Text where- safeConvert = Right . TL.fromStrict- instance Convertible TL.Text TS.Text where+ {-# INLINE safeConvert #-} safeConvert = Right . TL.toStrict-#else-instance Convertible TS.Text TL.Text where- safeConvert = Right . TL.fromChunks . (:[]) -instance Convertible TL.Text TS.Text where- safeConvert = Right . TS.concat . TL.toChunks+instance Convertible TL.Text TLB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TLB.fromLazyText++instance Convertible TL.Text BS.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = safeConvert . TLE.encodeUtf8++instance Convertible TL.Text BL.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TLE.encodeUtf8++instance Convertible TL.Text BB.Builder where+ {-# INLINE safeConvert #-}+#if MIN_VERSION_text(1,2,0)+ safeConvert = Right . TLE.encodeUtf8Builder+#else+ safeConvert = safeConvert . TLE.encodeUtf8 #endif --- ByteString -instance Convertible [Word8] BS.ByteString where- safeConvert = Right . BS.pack +instance Convertible TLB.Builder [Char] where+ {-# INLINE safeConvert #-}+ safeConvert = safeConvert . TLB.toLazyText++instance Convertible TLB.Builder TS.Text where+ {-# INLINE safeConvert #-}+ safeConvert = safeConvert . TLB.toLazyText++instance Convertible TLB.Builder TL.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TLB.toLazyText++instance Convertible TLB.Builder BS.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = safeConvert . TLB.toLazyText++instance Convertible TLB.Builder BL.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = safeConvert . TLB.toLazyText++instance Convertible TLB.Builder BB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = safeConvert . TLB.toLazyText+++ instance Convertible BS.ByteString [Word8] where+ {-# INLINE safeConvert #-} safeConvert = Right . BS.unpack -instance Convertible [Word8] BL.ByteString where- safeConvert = Right . BL.pack+instance Convertible BS.ByteString TS.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TE.decodeUtf8 +instance Convertible BS.ByteString TL.Text where+ {-# INLINE safeConvert #-}+ safeConvert = fmap TL.fromStrict . safeConvert++instance Convertible BS.ByteString TLB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = fmap TLB.fromText . safeConvert++instance Convertible BS.ByteString BL.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BL.fromStrict++instance Convertible BS.ByteString BB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BB.byteString+++ instance Convertible BL.ByteString [Word8] where+ {-# INLINE safeConvert #-} safeConvert = Right . BL.unpack -instance Convertible BS.ByteString BL.ByteString where- safeConvert = Right . BL.fromChunks . (:[])+instance Convertible BL.ByteString TS.Text where+ {-# INLINE safeConvert #-}+ safeConvert = fmap TL.toStrict . safeConvert +instance Convertible BL.ByteString TL.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TLE.decodeUtf8++instance Convertible BL.ByteString TLB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = fmap TLB.fromLazyText . safeConvert+ instance Convertible BL.ByteString BS.ByteString where- safeConvert = Right . BS.concat . BL.toChunks+ {-# INLINE safeConvert #-}+ safeConvert = Right . BL.toStrict++instance Convertible BL.ByteString BB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BB.lazyByteString++++instance Convertible [Char] TS.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TS.pack++instance Convertible [Char] TL.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TL.pack++instance Convertible [Char] TLB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TLB.fromString++++instance Convertible [Word8] BS.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BS.pack++instance Convertible [Word8] BL.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BL.pack++instance Convertible [Word8] BB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . foldMap BB.word8++++instance Convertible Char TS.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TS.singleton++instance Convertible Char TL.Text where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TL.singleton++instance Convertible Char TLB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . TLB.singleton++++instance Convertible Word8 BS.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BS.singleton++instance Convertible Word8 BL.ByteString where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BL.singleton++instance Convertible Word8 BB.Builder where+ {-# INLINE safeConvert #-}+ safeConvert = Right . BB.word8
Data/Convertible/Instances/Time.hs view
@@ -29,12 +29,8 @@ import Data.Convertible.Instances.Num() import qualified System.Time as ST import Data.Time-import Data.Time.Clock import Data.Time.Clock.POSIX import Data.Time.Calendar.OrdinalDate-#ifndef TIME_GT_113-import Data.Typeable-#endif import Data.Ratio import Foreign.C.Types @@ -59,15 +55,6 @@ ---------------------------------------------------------------------- ------------------------------ POSIX and UTC times--#ifndef TIME_GT_113-instance Typeable NominalDiffTime where- typeOf _ = mkTypeName "NominalDiffTime"--instance Typeable UTCTime where- typeOf _ = mkTypeName "UTCTime"-#endif- {- Covered under Real a instance Convertible Rational POSIXTime where safeConvert = return . fromRational@@ -97,13 +84,13 @@ safeConvert = return . utcTimeToPOSIXSeconds instance Convertible Rational UTCTime where- safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime+ safeConvert a = posixSecondsToUTCTime <$> safeConvert a instance Convertible Integer UTCTime where- safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime+ safeConvert a = posixSecondsToUTCTime <$> safeConvert a instance Convertible Int UTCTime where- safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime+ safeConvert a = posixSecondsToUTCTime <$> safeConvert a instance Convertible Double UTCTime where- safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime+ safeConvert a = posixSecondsToUTCTime <$> safeConvert a instance Convertible UTCTime Rational where safeConvert = safeConvert . utcTimeToPOSIXSeconds@@ -138,13 +125,13 @@ instance Convertible ST.CalendarTime ZonedTime where safeConvert ct = return $ ZonedTime { zonedTimeToLocalTime = LocalTime {- localDay = fromGregorian (fromIntegral $ ST.ctYear ct) - (1 + (fromEnum $ ST.ctMonth ct))+ localDay = fromGregorian (fromIntegral $ ST.ctYear ct)+ (1 + fromEnum (ST.ctMonth ct)) (ST.ctDay ct), localTimeOfDay = TimeOfDay { todHour = ST.ctHour ct, todMin = ST.ctMin ct,- todSec = (fromIntegral $ ST.ctSec ct) + + todSec = fromIntegral (ST.ctSec ct) + fromRational (ST.ctPicosec ct % 1000000000000) } },@@ -160,7 +147,7 @@ safeConvert = convertVia (undefined::POSIXTime) instance Convertible ST.ClockTime POSIXTime where- safeConvert (ST.TOD x y) = return $ fromRational $ + safeConvert (ST.TOD x y) = return $ fromRational $ fromInteger x + fromRational (y % 1000000000000) instance Convertible ST.ClockTime UTCTime where safeConvert = convertVia (undefined::POSIXTime)@@ -172,7 +159,7 @@ instance Convertible POSIXTime ST.ClockTime where safeConvert x = return $ ST.TOD rsecs rpico where rsecs = floor x- rpico = truncate $ abs $ 1000000000000 * (x - (fromIntegral rsecs))+ rpico = truncate $ abs $ 1000000000000 * (x - fromIntegral rsecs) instance Convertible UTCTime ST.ClockTime where safeConvert = safeConvert . utcTimeToPOSIXSeconds
Data/Convertible/Utils.hs view
@@ -19,7 +19,6 @@ -} module Data.Convertible.Utils(boundedConversion,- mkTypeName, convertVia ) where@@ -31,9 +30,9 @@ Does this be examining the bounds of the destination type, converting to the type of the source via 'safeConvert', comparing to the source value. Results in an error if the conversion is out of bounds. -}-boundedConversion :: (Ord a, Bounded b, Show a, Show b, Convertible a Integer,+boundedConversion :: (Bounded b, Show a, Show b, Convertible a Integer, Convertible b Integer,- Typeable a, Typeable b) => + Typeable a, Typeable b) => (a -> ConvertResult b) -- ^ Function to do the conversion -> a -- ^ Input data -> ConvertResult b -- ^ Result@@ -41,21 +40,13 @@ do result <- func inp let smallest = asTypeOf minBound result let biggest = asTypeOf maxBound result- let smallest' = (convert smallest)::Integer- let biggest' = (convert biggest)::Integer- let inp' = (convert inp)::Integer+ let smallest' = convert smallest :: Integer+ let biggest' = convert biggest :: Integer+ let inp' = convert inp :: Integer if inp' < smallest' || inp' > biggest' then convError ("Input value outside of bounds: " ++ show (smallest, biggest)) inp else return result--{- | Useful for defining 'Typeable' instances. Example:-->instance Typeable TimeOfDay where-> typeOf _ = mkTypeName "TimeOfDay"--}-mkTypeName :: String -> TypeRep-mkTypeName name = mkTyConApp (mkTyCon name) [] {- | Useful for defining conversions that are implemented in terms of other conversions via an intermediary type. Instead of:
− Makefile
@@ -1,30 +0,0 @@-all: setup- @echo "Please use Cabal to build this package; not make."- ./setup configure- ./setup build--setup: Setup.lhs- ghc --make -o setup Setup.lhs--install: setup- ./setup install--clean:- -./setup clean- -runghc Setup.lhs clean--.PHONY: test-test: test-ghc test-hugs- @echo ""- @echo "All tests pass."--test-hugs:- @echo " ****** Running hugs tests"- runhugs -98 +o -P$(PWD):$(PWD)/testsrc: testsrc/runtests.hs--test-ghc:- @echo " ****** Building GHC tests"- runghc Setup.lhs configure -f buildtests- runghc Setup.lhs build- @echo " ****** Running GHC tests"- ./dist/build/runtests/runtests
convertible.cabal view
@@ -1,17 +1,15 @@ Name: convertible-Version: 1.0.11.1+Version: 1.1.1.1 License: BSD3-Maintainer: John Goerzen <jgoerzen@complete.org>+Maintainer: Janus Troelsen <ysangkok@gmail.com> Author: John Goerzen Copyright: Copyright (c) 2009-2011 John Goerzen license-file: LICENSE-extra-source-files: LICENSE, utils/genCinstances.hs,- Makefile,- testsrc/TestInfrastructure.hs,- testsrc/TestMap.hs,- testsrc/TestNum.hs,- testsrc/TestTime.hs-homepage: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/convertible+extra-source-files: LICENSE, utils/genCinstances.hs+homepage: http://hackage.haskell.org/package/convertible+bug-reports: https://github.com/hdbc/convertible/issues++ Category: Data synopsis: Typeclasses and instances for converting between types Description: convertible provides a typeclass with a single function@@ -37,31 +35,23 @@ Stability: Stable Build-Type: Simple -Cabal-Version: >=1.2--flag splitBase- description: Choose the new smaller, split-up base package.-flag buildtests- description: Build the executable to run unit tests- default: False+Cabal-Version: >=1.10 -flag time_gte_113- description: time > 1.1.3 has defined some more instances so omit them here+source-repository head+ type: git+ location: git://github.com/hdbc/convertible.git library- if flag(splitBase)- Build-Depends: base>=3 && <5, old-time, time>=1.1.2.4,- bytestring, containers, old-locale- if flag(time_gte_113)- Build-Depends: time>=1.1.3- CPP-OPTIONS: -DTIME_GT_113- else- Build-Depends: time<1.1.3- else- Build-Depends: base<3- Build-Depends: mtl, text >= 0.7+ Default-Language: Haskell2010+ Build-Depends: base>=3 && <5,+ old-time,+ time>=1.1.3,+ bytestring >= 0.10.2,+ containers,+ mtl,+ text >= 0.8 - GHC-Options: -O2 -Wall -fno-warn-orphans+ GHC-Options: -Wall -fno-warn-orphans -Wcpp-undef -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints -Winvalid-haddock -Wunused-packages -Wmissing-export-lists Exposed-Modules: Data.Convertible, Data.Convertible.Base,@@ -73,27 +63,21 @@ Data.Convertible.Instances.Text, Data.Convertible.Instances.Time - --Other-Modules: Database.HDBC.Utils- --Extensions: ExistentialQuantification, AllowOverlappingInstances,- -- AllowUndecidableInstances- Extensions: ExistentialQuantification, MultiParamTypeClasses,+ Default-Extensions:+ ExistentialQuantification, MultiParamTypeClasses, UndecidableInstances, FlexibleInstances,- FlexibleContexts, TypeSynonymInstances, CPP- -Executable runtests- if flag(buildtests)- Buildable: True- Build-Depends: HUnit, QuickCheck >= 2.0, testpack >= 2.0- if flag(time_gte_113)- Build-Depends: time>=1.1.3- CPP-OPTIONS: -DTIME_GT_113- else- Build-Depends: time<1.1.3- else- Buildable: False+ FlexibleContexts, TypeSynonymInstances+ Other-Extensions: CPP++test-suite runtests+ Default-Language: Haskell2010+ Type: exitcode-stdio-1.0+ Build-Depends: base, convertible, containers, QuickCheck >= 2.8, time>=1.1.3, old-time Main-Is: runtests.hs- Hs-Source-Dirs: ., testsrc- GHC-Options: -O2- Extensions: ExistentialQuantification, MultiParamTypeClasses,+ Hs-Source-Dirs: testsrc+ GHC-Options: -Wall -Wno-orphans -Wcpp-undef -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints -Winvalid-haddock -Wunused-packages+ Default-Extensions:+ ExistentialQuantification, MultiParamTypeClasses, UndecidableInstances, FlexibleInstances, FlexibleContexts, TypeSynonymInstances, CPP+ Other-Modules: TestNum, TestTime, TestMap
− testsrc/TestInfrastructure.hs
@@ -1,15 +0,0 @@-{--Copyright (C) 2009 John Goerzen <jgoerzen@complete.org>--All rights reserved.--For license and copyright information, see the file LICENSE--}-module TestInfrastructure where-import qualified Test.QuickCheck as QC-import qualified Test.HUnit as HU-import Test.HUnit.Tools--q :: QC.Testable a => String -> a -> HU.Test-q = qc2hu 250-
testsrc/TestMap.hs view
@@ -7,19 +7,23 @@ -} module TestMap where-import TestInfrastructure import Data.Convertible-import Test.QuickCheck-import Test.QuickCheck.Tools-import Test.QuickCheck.Instances-import qualified Test.QuickCheck.Property as P+import Test.QuickCheck ((===), Property, Testable, quickCheck) import qualified Data.Map as Map -propListMap :: [(Int, Int)] -> P.Result-propListMap x = safeConvert x @?= Right (Map.fromList x)+-- | [(Int, Int)] -> Map+propListMap :: [(Int, Int)] -> Property+propListMap x = safeConvert x === Right (Map.fromList x) -propMapList :: Map.Map Int Int -> P.Result-propMapList x = safeConvert x @?= Right (Map.toList x)+-- | Map -> [(Int, Int)]+propMapList :: Map.Map Int Int -> Property+propMapList x = safeConvert x === Right (Map.toList x) +q :: Testable prop => String -> prop -> IO ()+q testLabel prop = do+ putStrLn testLabel+ quickCheck prop++allt :: [IO ()] allt = [q "[(Int, Int)] -> Map" propListMap, q "Map -> [(Int, Int)]" propMapList]
testsrc/TestNum.hs view
@@ -7,87 +7,102 @@ -} module TestNum where-import TestInfrastructure import Data.Convertible-import Test.QuickCheck-import Test.QuickCheck.Tools-import qualified Test.QuickCheck.Property as P-import Data.Word+import Test.QuickCheck ((===), (==>), Property, Testable, quickCheck)+import Data.Word (Word8) -prop_int_to_integer :: Int -> P.Result+-- | Int -> Integer+prop_int_to_integer :: Int -> Property prop_int_to_integer x =- safeConvert x @?= Right ((fromIntegral x)::Integer)+ safeConvert x === Right (fromIntegral x :: Integer) +-- | Integer -> Int (safe bounds) prop_integer_to_int_pass :: Integer -> Property prop_integer_to_int_pass x = (x <= fromIntegral (maxBound :: Int)) &&- (x >= fromIntegral (minBound :: Int)) ==> - safeConvert x @?= Right ((fromIntegral x)::Int)+ (x >= fromIntegral (minBound :: Int)) ==>+ safeConvert x === Right (fromIntegral x :: Int) -prop_integer_to_word8 :: Integer -> P.Result+-- | Integer -> Word8 (general)+prop_integer_to_word8 :: Integer -> Property prop_integer_to_word8 x =- safeConvert x @?= if x >= fromIntegral (minBound :: Word8) &&+ safeConvert x === if x >= fromIntegral (minBound :: Word8) && x <= fromIntegral (maxBound :: Word8)- then Right ((fromIntegral x)::Word8)+ then Right (fromIntegral x :: Word8) else Left $ ConvertError (show x) "Integer" "Word8" "Input value outside of bounds: (0,255)" +-- | Integer -> Word8 (safe bounds) prop_integer_to_word8_safe :: Integer -> Property prop_integer_to_word8_safe x = x <= fromIntegral (maxBound :: Word8) && x >= fromIntegral (minBound :: Word8) ==>- safeConvert x @?= Right ((fromIntegral x)::Word8)+ safeConvert x === Right (fromIntegral x :: Word8) +-- | Integer -> Word8 (unsafe bounds) prop_integer_to_word8_unsafe :: Integer -> Property prop_integer_to_word8_unsafe x = x < fromIntegral (minBound :: Word8) || x > fromIntegral (maxBound :: Word8) ==>- ((safeConvert x)::ConvertResult Word8) @?= (Left $ ConvertError (show x) "Integer" "Word8" "Input value outside of bounds: (0,255)")+ (safeConvert x :: ConvertResult Word8) === Left (ConvertError (show x) "Integer" "Word8" "Input value outside of bounds: (0,255)") -prop_double_to_word8 :: Double -> P.Result+-- | Double -> Word8 (general)+prop_double_to_word8 :: Double -> Property prop_double_to_word8 x =- safeConvert x @?= if truncate x >= toInteger (minBound :: Word8) &&+ safeConvert x === if truncate x >= toInteger (minBound :: Word8) && truncate x <= toInteger (maxBound :: Word8)- then Right ((truncate x)::Word8)+ then Right (truncate x :: Word8) else Left $ ConvertError (show x) "Double" "Word8" "Input value outside of bounds: (0,255)" +-- | Double -> Word8 (safe bounds) prop_double_to_word8_safe :: Double -> Property prop_double_to_word8_safe x = x <= fromIntegral (maxBound :: Word8) && x >= fromIntegral (minBound :: Word8) ==>- safeConvert x @?= Right ((truncate x)::Word8)+ safeConvert x === Right (truncate x :: Word8) +-- | Double -> Word8 (unsafe bounds) prop_double_to_word8_unsafe :: Double -> Property prop_double_to_word8_unsafe x = truncate x < toInteger (minBound :: Word8) || truncate x > toInteger (maxBound :: Word8) ==>- ((safeConvert x)::ConvertResult Word8) @?= (Left $ ConvertError (show x) "Double" "Word8" "Input value outside of bounds: (0,255)")+ (safeConvert x :: ConvertResult Word8) === Left (ConvertError (show x) "Double" "Word8" "Input value outside of bounds: (0,255)") -propIntDouble :: Int -> P.Result+-- | Int -> Double+propIntDouble :: Int -> Property propIntDouble x =- safeConvert x @?= Right ((fromIntegral x)::Double)+ safeConvert x === Right (fromIntegral x :: Double) -propIntChar :: Int -> P.Result+-- | Int -> Char+propIntChar :: Int -> Property propIntChar x =- safeConvert x @?= if x >= fromEnum (minBound :: Char) &&+ safeConvert x === if x >= fromEnum (minBound :: Char) && x <= fromEnum (maxBound :: Char)- then Right ((toEnum x)::Char)+ then Right (toEnum x :: Char) else Left $ ConvertError (show x) "Int" "Char" "Input value outside of bounds: ('\\NUL','\\1114111')" -propCharInt :: Char -> P.Result+-- | Char -> Int+propCharInt :: Char -> Property propCharInt c =- safeConvert c @?= Right ((fromEnum c)::Int)- where x = fromEnum c+ safeConvert c === Right (fromEnum c :: Int) -propIntIntegerInt :: Int -> P.Result+-- | identity Int -> Integer -> Int+propIntIntegerInt :: Int -> Property propIntIntegerInt x =- Right x @=? do r1 <- ((safeConvert x)::ConvertResult Integer)- ((safeConvert r1)::ConvertResult Int)- -propDoubleRationalDouble :: Double -> P.Result+ Right x === do r1 <- safeConvert x :: ConvertResult Integer+ safeConvert r1 :: ConvertResult Int++-- | identity Double -> Rational -> Double+propDoubleRationalDouble :: Double -> Property propDoubleRationalDouble x =- Right x @=? do r1 <- ((safeConvert x)::ConvertResult Rational)- ((safeConvert r1)::ConvertResult Double)+ Right x === do r1 <- safeConvert x :: ConvertResult Rational+ safeConvert r1 :: ConvertResult Double +q :: Testable prop => String -> prop -> IO ()+q testLabel prop = do+ putStrLn testLabel+ quickCheck prop++allt :: [IO ()] allt = [q "Int -> Integer" prop_int_to_integer, q "Integer -> Int (safe bounds)" prop_integer_to_int_pass, q "Integer -> Word8 (general)" prop_integer_to_word8,
testsrc/TestTime.hs view
@@ -7,12 +7,8 @@ -} module TestTime where-import TestInfrastructure import Data.Convertible-import Test.QuickCheck-import Test.QuickCheck.Tools-import Test.QuickCheck.Instances-import qualified Test.QuickCheck.Property as P+import Test.QuickCheck ((===), Arbitrary, Property, Testable, arbitrary, choose, quickCheck, sized) import qualified System.Time as ST import Data.Time import Data.Time.Clock.POSIX@@ -44,36 +40,36 @@ instance Eq ZonedTime where a == b = zonedTimeToUTC a == zonedTimeToUTC b -propCltCalt :: ST.ClockTime -> P.Result+propCltCalt :: ST.ClockTime -> Property propCltCalt x =- safeConvert x @?= Right (ST.toUTCTime x)+ safeConvert x === Right (ST.toUTCTime x) -propCltCaltClt :: ST.ClockTime -> P.Result+propCltCaltClt :: ST.ClockTime -> Property propCltCaltClt x =- Right x @=? do r1 <- ((safeConvert x)::ConvertResult ST.CalendarTime)+ Right x === do r1 <- safeConvert x :: ConvertResult ST.CalendarTime safeConvert r1 -propCltPT :: ST.ClockTime -> P.Result+propCltPT :: ST.ClockTime -> Property propCltPT x@(ST.TOD y z) =- safeConvert x @?= Right (r::POSIXTime)+ safeConvert x === Right (r::POSIXTime) where r = fromRational $ fromInteger y + fromRational (z % 1000000000000) -propPTClt :: POSIXTime -> P.Result+propPTClt :: POSIXTime -> Property propPTClt x =- safeConvert x @?= Right (r::ST.ClockTime)+ safeConvert x === Right (r::ST.ClockTime) where r = ST.TOD rsecs rpico rsecs = floor x- rpico = truncate $ abs $ 1000000000000 * (x - (fromIntegral rsecs))+ rpico = truncate $ abs $ 1000000000000 * (x - fromIntegral rsecs) -propCaltPT :: ST.CalendarTime -> P.Result+propCaltPT :: ST.CalendarTime -> Property propCaltPT x =- safeConvert x @?= expected+ safeConvert x === expected where expected = do r <- safeConvert x- (safeConvert (r :: ST.ClockTime))::ConvertResult POSIXTime+ safeConvert (r :: ST.ClockTime) :: ConvertResult POSIXTime -propCltPTClt :: ST.ClockTime -> P.Result+propCltPTClt :: ST.ClockTime -> Property propCltPTClt x =- Right (toTOD x) @=? case do r1 <- (safeConvert x)::ConvertResult POSIXTime+ Right (toTOD x) === case do r1 <- safeConvert x :: ConvertResult POSIXTime safeConvert r1 of Left x -> Left x Right y -> Right $ toTOD y@@ -83,76 +79,76 @@ safeConvert r1 -} -propPTZTPT :: POSIXTime -> P.Result+propPTZTPT :: POSIXTime -> Property propPTZTPT x =- Right x @=? do r1 <- safeConvert x+ Right x === do r1 <- safeConvert x safeConvert (r1 :: ZonedTime) -propPTCltPT :: POSIXTime -> P.Result+propPTCltPT :: POSIXTime -> Property propPTCltPT x =- Right x @=? do r1 <- (safeConvert x)::ConvertResult ST.ClockTime+ Right x === do r1 <- safeConvert x :: ConvertResult ST.ClockTime safeConvert r1 -propPTCalPT :: POSIXTime -> P.Result+propPTCalPT :: POSIXTime -> Property propPTCalPT x =- Right x @=? do r1 <- safeConvert x+ Right x === do r1 <- safeConvert x safeConvert (r1::ST.CalendarTime) -propUTCCaltUTC :: UTCTime -> P.Result+propUTCCaltUTC :: UTCTime -> Property propUTCCaltUTC x =- Right x @=? do r1 <- safeConvert x+ Right x === do r1 <- safeConvert x safeConvert (r1::ST.CalendarTime) -propPTUTC :: POSIXTime -> P.Result+propPTUTC :: POSIXTime -> Property propPTUTC x =- safeConvert x @?= Right (posixSecondsToUTCTime x)-propUTCPT :: UTCTime -> P.Result+ safeConvert x === Right (posixSecondsToUTCTime x)+propUTCPT :: UTCTime -> Property propUTCPT x =- safeConvert x @?= Right (utcTimeToPOSIXSeconds x)+ safeConvert x === Right (utcTimeToPOSIXSeconds x) -propCltUTC :: ST.ClockTime -> P.Result+propCltUTC :: ST.ClockTime -> Property propCltUTC x =- safeConvert x @?= Right (posixSecondsToUTCTime . convert $ x)+ safeConvert x === Right (posixSecondsToUTCTime . convert $ x) -propZTCTeqZTCaltCt :: ZonedTime -> P.Result+propZTCTeqZTCaltCt :: ZonedTime -> Property propZTCTeqZTCaltCt x =- route1 @=? route2- where route1 = (safeConvert x)::ConvertResult ST.ClockTime+ route1 === route2+ where route1 = safeConvert x :: ConvertResult ST.ClockTime route2 = do calt <- safeConvert x safeConvert (calt :: ST.CalendarTime) -propCaltZTCalt :: ST.ClockTime -> P.Result+propCaltZTCalt :: ST.ClockTime -> Property propCaltZTCalt x =- Right x @=? do zt <- ((safeConvert calt)::ConvertResult ZonedTime)- calt' <- ((safeConvert zt)::ConvertResult ST.CalendarTime)+ Right x === do zt <- safeConvert calt :: ConvertResult ZonedTime+ calt' <- safeConvert zt :: ConvertResult ST.CalendarTime return (ST.toClockTime calt') where calt = ST.toUTCTime x -propCaltZTCalt2 :: ST.CalendarTime -> P.Result+propCaltZTCalt2 :: ST.CalendarTime -> Property propCaltZTCalt2 x =- Right x @=? do zt <- safeConvert x+ Right x === do zt <- safeConvert x safeConvert (zt :: ZonedTime) -propZTCaltCtZT :: ZonedTime -> P.Result+propZTCaltCtZT :: ZonedTime -> Property propZTCaltCtZT x =- Right x @=? do calt <- safeConvert x+ Right x === do calt <- safeConvert x ct <- safeConvert (calt :: ST.CalendarTime) safeConvert (ct :: ST.ClockTime) -propZTCtCaltZT :: ZonedTime -> P.Result+propZTCtCaltZT :: ZonedTime -> Property propZTCtCaltZT x =- Right x @=? do ct <- safeConvert x+ Right x === do ct <- safeConvert x calt <- safeConvert (ct :: ST.ClockTime) safeConvert (calt :: ST.CalendarTime) -propZTCaltZT :: ZonedTime -> P.Result+propZTCaltZT :: ZonedTime -> Property propZTCaltZT x =- Right x @=? do calt <- safeConvert x+ Right x === do calt <- safeConvert x safeConvert (calt :: ST.CalendarTime) -propZTCtCaltCtZT :: ZonedTime -> P.Result+propZTCtCaltCtZT :: ZonedTime -> Property propZTCtCaltCtZT x =- Right x @=? do ct <- safeConvert x+ Right x === do ct <- safeConvert x calt <- safeConvert (ct :: ST.ClockTime) ct' <- safeConvert (calt :: ST.CalendarTime) safeConvert (ct' :: ST.ClockTime)@@ -161,22 +157,30 @@ propUTCZT x = x == zonedTimeToUTC (convert x) -propUTCZTUTC :: UTCTime -> P.Result+propUTCZTUTC :: UTCTime -> Property propUTCZTUTC x =- Right x @=? do r1 <- ((safeConvert x)::ConvertResult ZonedTime)+ Right x === do r1 <- safeConvert x :: ConvertResult ZonedTime safeConvert r1 -propNdtTdNdt :: NominalDiffTime -> P.Result+propNdtTdNdt :: NominalDiffTime -> Property propNdtTdNdt x =- Right x @=? do r1 <- ((safeConvert x)::ConvertResult ST.TimeDiff)+ Right x === do r1 <- safeConvert x :: ConvertResult ST.TimeDiff safeConvert r1 -propPTCPT :: POSIXTime -> P.Result+propPTCPT :: POSIXTime -> Property propPTCPT x =- Right testval @=? do r1 <- safeConvert testval+ Right testval === do r1 <- safeConvert testval safeConvert (r1 :: CTime)- where testval = (convert ((truncate x)::Integer))::POSIXTime -- CTime doesn't support picosecs+ where+ testval :: POSIXTime+ testval = convert (truncate x :: Integer) -- CTime doesn't support picosecs +q :: Testable prop => String -> prop -> IO ()+q testLabel prop = do+ putStrLn testLabel+ quickCheck prop++allt :: [IO ()] allt = [q "ClockTime -> CalendarTime" propCltCalt, q "ClockTime -> CalendarTime -> ClockTime" propCltCaltClt, q "ClockTime -> POSIXTime" propCltPT,
testsrc/runtests.hs view
@@ -1,17 +1,11 @@ module Main where-import qualified Test.HUnit as HU-import Test.HUnit.Tools import qualified TestNum import qualified TestMap import qualified TestTime -test1 = HU.TestCase ((HU.@=?) "x" "x")--alltests = [HU.TestLabel "test1" test1,- tl "TestNum" TestNum.allt,- tl "TestMap" TestMap.allt,- tl "TestTime" TestTime.allt]--main = do runVerboseTests (HU.TestList alltests)- return ()+main :: IO ()+main = do+ sequence_ TestNum.allt+ sequence_ TestMap.allt+ sequence_ TestTime.allt
utils/genCinstances.hs view
@@ -5,7 +5,7 @@ "Word64"] hsfloat = ["Double", "Float", "Rational"] -printFP (f, i) = +printFP (f, i) = "instance Convertible " ++ f ++ " " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . truncate)\n\ \instance Convertible " ++ i ++ " " ++ f ++ " where \n\@@ -29,7 +29,7 @@ \instance Convertible Char " ++ i ++ " where\n\ \ safeConvert = boundedConversion (return . fromIntegral . fromEnum)\n" -printFP1 (f1, f2) = +printFP1 (f1, f2) = "instance Convertible " ++ f1 ++ " " ++ f2 ++ " where\n\ \ safeConvert = return . realToFrac\n" @@ -48,7 +48,7 @@ putStrLn "-- Section 3" mapM_ (putStrLn . printIntInt) (concatMap (\x -> map (\y -> (x, y)) hsint) cint) putStrLn "-- Section 4"- mapM_ (putStrLn . printInt) . filter (\(a, b) -> a /= b) $ + mapM_ (putStrLn . printInt) . filter (\(a, b) -> a /= b) $ (concatMap (\x -> map (\y -> (x, y)) cint) cint) putStrLn "-- Section 5" mapM_ (putStrLn . printFP1) . filter (\(a, b) -> a /= b) $@@ -63,4 +63,3 @@ -