Skip to content

Commit

Permalink
Merge branch 'upstream'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoerzen committed Feb 17, 2010
2 parents a0e7272 + 8a22de2 commit 6908e03
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 84 deletions.
82 changes: 0 additions & 82 deletions Data/Convertible/Instances/C.hs
Expand Up @@ -129,56 +129,6 @@ instance Convertible CDouble Word64 where
instance Convertible Word64 CDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Int where
safeConvert = boundedConversion (return . truncate)
instance Convertible Int CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Int8 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Int8 CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Int16 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Int16 CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Int32 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Int32 CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Int64 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Int64 CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Word where
safeConvert = boundedConversion (return . truncate)
instance Convertible Word CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Word8 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Word8 CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Word16 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Word16 CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Word32 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Word32 CLDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Word64 where
safeConvert = boundedConversion (return . truncate)
instance Convertible Word64 CLDouble where
safeConvert = return . fromIntegral

-- Section 2
instance Convertible CFloat Double where
safeConvert = return . realToFrac
Expand Down Expand Up @@ -210,21 +160,6 @@ instance Convertible CDouble Rational where
instance Convertible Rational CDouble where
safeConvert = return . realToFrac

instance Convertible CLDouble Double where
safeConvert = return . realToFrac
instance Convertible Double CLDouble where
safeConvert = return . realToFrac

instance Convertible CLDouble Float where
safeConvert = return . realToFrac
instance Convertible Float CLDouble where
safeConvert = return . realToFrac

instance Convertible CLDouble Rational where
safeConvert = return . realToFrac
instance Convertible Rational CLDouble where
safeConvert = return . realToFrac

-- Section 3
instance Convertible CChar Int where
safeConvert = boundedConversion (return . fromIntegral)
Expand Down Expand Up @@ -1349,21 +1284,9 @@ instance Convertible CULLong CLLong where
instance Convertible CFloat CDouble where
safeConvert = return . realToFrac

instance Convertible CFloat CLDouble where
safeConvert = return . realToFrac

instance Convertible CDouble CFloat where
safeConvert = return . realToFrac

instance Convertible CDouble CLDouble where
safeConvert = return . realToFrac

instance Convertible CLDouble CFloat where
safeConvert = return . realToFrac

instance Convertible CLDouble CDouble where
safeConvert = return . realToFrac

-- Section 6
instance Convertible CFloat Integer where
safeConvert = return . truncate
Expand All @@ -1375,11 +1298,6 @@ instance Convertible CDouble Integer where
instance Convertible Integer CDouble where
safeConvert = return . fromIntegral

instance Convertible CLDouble Integer where
safeConvert = return . truncate
instance Convertible Integer CLDouble where
safeConvert = return . fromIntegral

-- Section 7
instance Convertible CChar Integer where
safeConvert = return . fromIntegral
Expand Down
30 changes: 30 additions & 0 deletions Makefile
@@ -0,0 +1,30 @@
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
9 changes: 7 additions & 2 deletions convertible.cabal
@@ -1,11 +1,16 @@
Name: convertible
Version: 1.0.7
Version: 1.0.8
License: LGPL
Maintainer: John Goerzen <jgoerzen@complete.org>
Author: John Goerzen
Copyright: Copyright (c) 2009-2009 John Goerzen
license-file: COPYRIGHT
extra-source-files: COPYING, utils/genCinstances.hs
extra-source-files: COPYING, 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
Category: Data
synopsis: Typeclasses and instances for converting between types
Expand Down
17 changes: 17 additions & 0 deletions testsrc/TestInfrastructure.hs
@@ -0,0 +1,17 @@
{-
Copyright (C) 2009 John Goerzen <jgoerzen@complete.org>
All rights reserved.
For license and copyright information, see the file COPYRIGHT
-}
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

qverbose :: QC.Testable a => String -> a -> HU.Test
qverbose = qc2huVerbose 250
24 changes: 24 additions & 0 deletions testsrc/TestMap.hs
@@ -0,0 +1,24 @@
{-
Copyright (C) 2009 John Goerzen <jgoerzen@complete.org>
All rights reserved.
For license and copyright information, see the file COPYRIGHT
-}

module TestMap where
import TestInfrastructure
import Data.Convertible
import Test.QuickCheck
import Test.QuickCheck.Tools
import Test.QuickCheck.Instances
import qualified Data.Map as Map

propListMap :: [(Int, Int)] -> Result
propListMap x = safeConvert x @?= Right (Map.fromList x)

propMapList :: Map.Map Int Int -> Result
propMapList x = safeConvert x @?= Right (Map.toList x)

allt = [q "[(Int, Int)] -> Map" propListMap,
q "Map -> [(Int, Int)]" propMapList]
104 changes: 104 additions & 0 deletions testsrc/TestNum.hs
@@ -0,0 +1,104 @@
{-
Copyright (C) 2009 John Goerzen <jgoerzen@complete.org>
All rights reserved.
For license and copyright information, see the file COPYRIGHT
-}

module TestNum where
import TestInfrastructure
import Data.Convertible
import Test.QuickCheck
import Test.QuickCheck.Tools
import Data.Word

prop_int_to_integer :: Int -> Result
prop_int_to_integer x =
safeConvert x @?= Right ((fromIntegral x)::Integer)

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)

prop_integer_to_word8 :: Integer -> Result
prop_integer_to_word8 x =
safeConvert x @?= if x >= fromIntegral (minBound :: Word8) &&
x <= fromIntegral (maxBound :: Word8)
then Right ((fromIntegral x)::Word8)
else Left $ ConvertError (show x) "Integer" "Word8" "Input value outside of bounds: (0,255)"

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)

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)")

prop_double_to_word8 :: Double -> Result
prop_double_to_word8 x =
safeConvert x @?= if truncate x >= toInteger (minBound :: Word8) &&
truncate x <= toInteger (maxBound :: Word8)
then Right ((truncate x)::Word8)
else Left $ ConvertError (show x) "Double" "Word8" "Input value outside of bounds: (0,255)"

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)

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)")

propIntDouble :: Int -> Result
propIntDouble x =
safeConvert x @?= Right ((fromIntegral x)::Double)

propIntChar :: Int -> Result
propIntChar x =
safeConvert x @?= if x >= fromEnum (minBound :: Char) &&
x <= fromEnum (maxBound :: Char)
then Right ((toEnum x)::Char)
else Left $ ConvertError (show x) "Int" "Char" "Input value outside of bounds: ('\\NUL','\\1114111')"

propCharInt :: Int -> Property
propCharInt x =
x >= fromEnum (minBound :: Char) && x <= fromEnum (maxBound :: Char) ==>
safeConvert c @?= Right ((fromEnum c)::Int)
where c = (toEnum x)::Char

propIntIntegerInt :: Int -> Result
propIntIntegerInt x =
Right x @=? do r1 <- ((safeConvert x)::ConvertResult Integer)
((safeConvert r1)::ConvertResult Int)

propDoubleRationalDouble :: Double -> Result
propDoubleRationalDouble x =
Right x @=? do r1 <- ((safeConvert x)::ConvertResult Rational)
((safeConvert r1)::ConvertResult Double)

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,
q "Integer -> Word8 (safe bounds)" prop_integer_to_word8_safe,
q "Integer -> Word8 (unsafe bounds)" prop_integer_to_word8_unsafe,
q "Double -> Word8 (general)" prop_double_to_word8,
q "Double -> Word8 (safe bounds)" prop_double_to_word8_safe,
q "Double -> Word8 (unsafe bounds)" prop_double_to_word8_unsafe,
q "Int -> Double" propIntDouble,
q "Int -> Char" propIntChar,
q "Char -> Int" propCharInt,
q "identity Int -> Integer -> Int" propIntIntegerInt,
q "identity Double -> Rational -> Double" propDoubleRationalDouble
]

0 comments on commit 6908e03

Please sign in to comment.