number-length 0.1.0.0 → 0.1.0.1
raw patch · 8 files changed
+166/−88 lines, 8 filesdep ~basedep ~test-framework-quickcheck2PVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, test-framework-quickcheck2
API changes (from Hackage documentation)
Files
- ChangeLog.md +15/−1
- README.md +8/−1
- number-length.cabal +35/−39
- src/Data/NumberLength/Int.hs +14/−4
- src/Data/NumberLength/Word.hs +12/−2
- test/TestCase/Data/NumberLength.hs +49/−8
- test/TestCase/Data/NumberLength/Int.hs +27/−27
- test/TestCase/Data/NumberLength/Word.hs +6/−6
ChangeLog.md view
@@ -1,10 +1,24 @@ # ChangeLog / ReleaseNotes +## Version 0.1.0.1++* Removed dead code. (**minor change**)+* Better and expanded test suite, which now includes tests for methods of+ `SignedNumberLength`. (**minor change**)+* Depending on tagged package only for GHC <7.8. This is due to the fact, that+ `Data.Proxy` module was introduce in base ==4.7, which was bundled with GHC+ 7.8. (**minor change**)+* Small examples included in description. (**minor change**)+* Uploaded to [Hackage][]:+ <http://hackage.haskell.org/package/number-length-0.1.0.1>++ ## Version 0.1.0.0 * First public release.-* Uploaded to [Hackage][]: <http://hackage.haskell.org/package/number-length-0.1.0.0>+* Uploaded to [Hackage][]:+ <http://hackage.haskell.org/package/number-length-0.1.0.0>
README.md view
@@ -6,6 +6,7 @@ [][tl;dr Legal: BSD3] [](https://travis-ci.org/trskop/number-length)+[](https://coveralls.io/github/trskop/number-length?branch=master) ## Description@@ -16,7 +17,13 @@ decimal or hexadecimal form. In example when allocating buffer while serializing a number in to binary or human readable format. + λ> numberLength (123 :: Int)+ 3 + λ> numberLengthHex (123 :: Int) -- 123 = 7b in hex+ 2++ ## Building Options * `-fpedantic` (disabled by default)@@ -37,7 +44,7 @@ [Hackage: number-length]: http://hackage.haskell.org/package/number-length- "number-length package on Hackage"+ "Package number-length on Hackage" [Haskell.org]: http://www.haskell.org "The Haskell Programming Language"
number-length.cabal view
@@ -1,11 +1,21 @@ name: number-length-version: 0.1.0.0+version: 0.1.0.1 synopsis: Number of digits in a number in decimal and hexadecimal representation. description: There are situations when it is necessary to know the length of a number in decimal or hexadecimal form. In example when allocating buffer while serializing a number in to binary or human readable format.+ .+ @+ λ> numberLength (123 :: Int)+ 3+ @+ .+ @+ λ> numberLengthHex (123 :: Int) -- 123 = 7b in hex+ 2+ @ homepage: https://github.com/trskop/number-length license: BSD3@@ -24,12 +34,6 @@ default: False manual: True -flag old-base- description: Supplement Data.Proxy, which is available since- base >=4.7, using tagged package.- default: False- manual: False- library hs-source-dirs: src exposed-modules:@@ -42,16 +46,12 @@ default-language: Haskell2010 other-extensions: NoImplicitPrelude - if flag(old-base)- build-depends:- base >=4.5 && <4.7- , tagged >=0.6 && <0.9- else- build-depends:- base >=4.7 && <5+ build-depends: base >=4.5 && <5+ if impl(ghc <7.8)+ -- Data.Proxy was introduced in base ==4.7, which was bundled with GHC 7.8.+ build-depends: tagged >=0.6 && <0.9 ghc-options: -Wall -fwarn-tabs- if flag(pedantic) ghc-options: -fwarn-implicit-prelude@@ -71,9 +71,17 @@ default-language: Haskell2010 other-extensions: DefaultSignatures, NoImplicitPrelude + -- {{{ Dependencies of the library ------------------------------------------+ if impl(ghc <7.8)+ -- Data.Proxy was introduced in base ==4.7, which was bundled with GHC 7.8.+ build-depends: tagged >=0.6 && <0.9+ build-depends:- -- {{{ Test-suite dependencies --------------------------------------------- HUnit >=1.2 && <2+ base >=4.5 && <5+ -- }}} Dependencies of the library ------------------------------------------++ -- {{{ Test-suite dependencies ----------------------------------------------+ , HUnit >=1.2 && <2 -- ^ Same constraints as test-framework-hunit , QuickCheck >=2.6 && <2.9 -- ^ Same constraints as test-framework-quickcheck2==0.3.0.3.@@ -82,21 +90,12 @@ -- depend on it have less restrictive bounds. , test-framework-hunit >=0.2.6 && <1 -- ^ Lower versions have more restrictive bounds on test-framework.- , test-framework-quickcheck2 >=0.3+ , test-framework-quickcheck2 >=0.3 && <1 -- ^ There were changes in QuickCheck bounds in 0.2 branch and last one -- on it had a compilation failure on Hackage.- -- }}} Test-suite dependencies ---------------------------------------------- if flag(old-base)- build-depends:- base >=4.5 && <4.7- , tagged >=0.6 && <0.9- else- build-depends:- base >=4.7 && <5+ -- }}} Test-suite dependencies ---------------------------------------------- ghc-options: -Wall -fwarn-tabs- if flag(pedantic) ghc-options: -fwarn-implicit-prelude@@ -113,21 +112,18 @@ other-extensions: NoImplicitPrelude -- {{{ Dependencies of the library ------------------------------------------- if flag(old-base)- build-depends:- base >=4.5 && <4.7- , tagged >=0.6 && <0.9- else- build-depends:- base >=4.7 && <5- -- }}} Dependencies of the library ------------------------------------------+ if impl(ghc <7.8)+ -- Data.Proxy was introduced in base ==4.7, which was bundled with GHC 7.8.+ build-depends: tagged >=0.6 && <0.9 build-depends:- criterion >= 1.0.0.0 && < 2+ base >=4.5 && <5+ -- }}} Dependencies of the library ------------------------------------------++ , criterion >=1.0.0.0 && <2 , number-length ghc-options: -Wall -fwarn-tabs- if flag(pedantic) ghc-options: -fwarn-implicit-prelude@@ -141,4 +137,4 @@ source-repository this type: git location: git://github.com/trskop/number-length.git- tag: v0.1.0.0+ tag: v0.1.0.1
src/Data/NumberLength/Int.hs view
@@ -13,14 +13,14 @@ -- or hexadecimal representation. module Data.NumberLength.Int (- -- * Decimal+ -- * Decimal (base 10) lengthInt , lengthInt8 , lengthInt16 , lengthInt32 , lengthInt64 - -- * Hexadecimal+ -- * Hexadecimal (base 16) , lengthIntHex , lengthInt8hex , lengthInt16hex@@ -46,6 +46,7 @@ -- {{{ Decimal ---------------------------------------------------------------- +-- | Number of digits in a @number :: 'Int8'@ in base 10. lengthInt8 :: Int8 -> Int lengthInt8 n | n < 0 = go (negate (fromIntegral n))@@ -59,6 +60,7 @@ | otherwise = 3 {-# INLINE lengthInt8 #-} +-- | Number of digits in a @number :: 'Int16'@ in base 10. lengthInt16 :: Int16 -> Int lengthInt16 n | n < 0 = go (negate (fromIntegral n))@@ -74,6 +76,7 @@ | otherwise = 5 {-# INLINE lengthInt16 #-} +-- | Number of digits in a @number :: 'Int32'@ in base 10. lengthInt32 :: Int32 -> Int lengthInt32 n | n == minBound = 10 -- "negate minBound" is out of range of Int32.@@ -92,6 +95,7 @@ -- m >= 10000 {-# INLINE lengthInt32 #-} +-- | Number of digits in a @number :: 'Int64'@ in base 10. lengthInt64 :: Int64 -> Int lengthInt64 n | n == minBound = 19 -- "negate minBound" is out of range of Int64@@ -111,6 +115,7 @@ -- m >= 10000 {-# INLINE lengthInt64 #-} +-- | Number of digits in a @number :: 'Int'@ in base 10. lengthInt :: Int -> Int lengthInt n = l32 `either32or64` l64 where@@ -153,6 +158,7 @@ -- {{{ Hexadecimal ------------------------------------------------------------ +-- | Number of digits in a @number :: 'Int8'@ in base 16. lengthInt8hex :: Int8 -> Int lengthInt8hex n | n < 16 && n > -16 = 1@@ -160,6 +166,7 @@ -- Maximum is 127 = 0x7f for positive and 128 = 0x80 for negative integer. {-# INLINE lengthInt8hex #-} +-- | Number of digits in a @number :: 'Int16'@ in base 16. lengthInt16hex :: Int16 -> Int lengthInt16hex n | n < 0 = go (negate (fromIntegral n))@@ -175,6 +182,7 @@ | otherwise = 4 {-# INLINE lengthInt16hex #-} +-- | Number of digits in a @number :: 'Int32'@ in base 16. lengthInt32hex :: Int32 -> Int lengthInt32hex n | n == minBound = 8 -- "negate minBound" is out of range of Int32.@@ -193,10 +201,11 @@ -- m >= 0x10000 {-# INLINE lengthInt32hex #-} +-- | Number of digits in a @number :: 'Int64'@ in base 16. lengthInt64hex :: Int64 -> Int lengthInt64hex n | n == minBound = 16 -- "negate minBound" is out of range of Int64- | n < 0 = if n == minBound then 16 else go (negate n)+ | n < 0 = go (negate n) | otherwise = go n where -- Maximum is 9223372036854775807 = 0x7fffffffffffffff for positive and@@ -211,13 +220,14 @@ -- m >= 0x10000 {-# INLINE lengthInt64hex #-} +-- | Number of digits in a @number :: 'Int'@ in base 16. lengthIntHex :: Int -> Int lengthIntHex n = l32hex `either32or64` l64hex where -- Same code as lengthInt64hex: l64hex | n == minBound = 16 -- "negate minBound" is out of range of Int64- | n < 0 = if n == minBound then 16 else go (negate n)+ | n < 0 = go (negate n) | otherwise = go n where -- Maximum is 9223372036854775807 = 0x7fffffffffffffff for positive and
src/Data/NumberLength/Word.hs view
@@ -13,14 +13,14 @@ -- or hexadecimal representation. module Data.NumberLength.Word (- -- * Decimal+ -- * Decimal (base 10) lengthWord , lengthWord8 , lengthWord16 , lengthWord32 , lengthWord64 - -- * Hexadecimal+ -- * Hexadecimal (base 16) , lengthWordHex , lengthWord8hex , lengthWord16hex@@ -45,6 +45,7 @@ -- {{{ Decimal ---------------------------------------------------------------- +-- | Number of digits in a @number :: 'Word8'@ in base 10. lengthWord8 :: Word8 -> Int lengthWord8 n -- Maximum is 255. | n < 10 = 1@@ -52,6 +53,7 @@ | otherwise = 3 {-# INLINE lengthWord8 #-} +-- | Number of digits in a @number :: 'Word16'@ in base 10. lengthWord16 :: Word16 -> Int lengthWord16 n -- Maximum is 65535. | n < 10 = 1@@ -61,6 +63,7 @@ | otherwise = 5 {-# INLINE lengthWord16 #-} +-- | Number of digits in a @number :: 'Word32'@ in base 10. lengthWord32 :: Word32 -> Int lengthWord32 n -- Maximum is 4294967295. | n < 10 = 1@@ -72,6 +75,7 @@ -- n >= 10000 {-# INLINE lengthWord32 #-} +-- | Number of digits in a @number :: 'Word64'@ in base 10. lengthWord64 :: Word64 -> Int lengthWord64 n -- Maximum is 18446744073709551615. | n < 10 = 1@@ -84,6 +88,7 @@ -- n >= 10000 {-# INLINE lengthWord64 #-} +-- | Number of digits in a @number :: 'Word'@ in base 10. lengthWord :: Word -> Int lengthWord n = lengthWord32 (fromIntegral n) `either32or64` lengthWord64 (fromIntegral n)@@ -93,12 +98,14 @@ -- {{{ Hexadecimal ------------------------------------------------------------ +-- | Number of digits in a @number :: 'Word8'@ in base 16. lengthWord8hex :: Word8 -> Int lengthWord8hex n -- Maximum is 255 = 0xff. | n < 16 = 1 | otherwise = 2 {-# INLINE lengthWord8hex #-} +-- | Number of digits in a @number :: 'Word16'@ in base 16. lengthWord16hex :: Word16 -> Int lengthWord16hex n -- Maximum is 65535 = 0xffff. | n < 0x10 = 1@@ -107,6 +114,7 @@ | otherwise = 4 {-# INLINE lengthWord16hex #-} +-- | Number of digits in a @number :: 'Word32'@ in base 16. lengthWord32hex :: Word32 -> Int lengthWord32hex n -- Maximum is 4294967295 = 0xffffffff. | n < 0x10 = 1@@ -117,6 +125,7 @@ -- n >= 0x10000 {-# INLINE lengthWord32hex #-} +-- | Number of digits in a @number :: 'Word64'@ in base 16. lengthWord64hex :: Word64 -> Int lengthWord64hex n -- Maximum is 18446744073709551615 = 0xffffffffffffffff. | n < 0x10 = 1@@ -128,6 +137,7 @@ -- n >= 0x10000 {-# INLINE lengthWord64hex #-} +-- | Number of digits in a @number :: 'Word'@ in base 16. lengthWordHex :: Word -> Int lengthWordHex n = lengthWord32hex (fromIntegral n) `either32or64` lengthWord64hex (fromIntegral n)
test/TestCase/Data/NumberLength.hs view
@@ -2,32 +2,37 @@ -- | -- Module: $HEADER$ -- Description: TODO--- Copyright: (c) 2015, Peter Trško+-- Copyright: (c) 2015-2016, Peter Trško -- License: BSD3 -- -- Stability: experimental -- Portability: NoImplicitPrelude-module TestCase.Data.NumberLength+module TestCase.Data.NumberLength (tests) where -import Prelude (Bounded(maxBound))+import Prelude (Bounded(maxBound), Integral, fromIntegral) import Control.Applicative (liftA2) import Data.Bool (Bool) import Data.Eq (Eq((==)))-import Data.Function (($))+import Data.Function ((.), ($)) import Data.Int (Int, Int16, Int32, Int64, Int8)-import Data.Proxy (Proxy(Proxy))+import Data.List (length)+import Data.Proxy (Proxy(Proxy), asProxyTypeOf)+import Data.String (String) import Data.Word (Word, Word16, Word32, Word64, Word8)+import Text.Printf (PrintfArg, printf)+import Text.Show (Show(show)) -import Test.HUnit ((@?=)) import Test.Framework (Test, testGroup) import Test.Framework.Providers.HUnit (testCase) import Test.Framework.Providers.QuickCheck2 (testProperty)+import Test.HUnit ((@?=)) import Data.NumberLength- ( NumberLength(numberLength, numberLengthHex)- , BoundedNumberLength(maxNumberLength, maxNumberLengthHex)+ ( BoundedNumberLength(maxNumberLength, maxNumberLengthHex)+ , NumberLength(numberLength, numberLengthHex)+ , SignedNumberLength(signedNumberLength, signedNumberLengthHex) ) import Data.NumberLength.Int ( lengthInt@@ -85,6 +90,28 @@ ] ] + , testGroup "class SignedNumberLength"+ [ testGroup "signedNumberLength"+ [ testProperty "Int" $ signedNumberLength <==> lengthOfShow int+ , testProperty "Int8" $ signedNumberLength <==> lengthOfShow int8+ , testProperty "Int16" $ signedNumberLength <==> lengthOfShow int16+ , testProperty "Int32" $ signedNumberLength <==> lengthOfShow int32+ , testProperty "Int64" $ signedNumberLength <==> lengthOfShow int64+ ]+ , testGroup "signedNumberLengthHex"+ [ testProperty "Int"+ $ signedNumberLengthHex <==> lengthOfHex int word+ , testProperty "Int8"+ $ signedNumberLengthHex <==> lengthOfHex int8 word8+ , testProperty "Int16"+ $ signedNumberLengthHex <==> lengthOfHex int16 word16+ , testProperty "Int32"+ $ signedNumberLengthHex <==> lengthOfHex int32 word32+ , testProperty "Int64"+ $ signedNumberLengthHex <==> lengthOfHex int64 word64+ ]+ ]+ , testGroup "class BoundedNumberLength" [ testGroup "maxNumberLength" [ testCase "Int"@@ -145,6 +172,20 @@ word16 = Proxy :: Proxy Word16 word32 = Proxy :: Proxy Word32 word64 = Proxy :: Proxy Word64++lengthOfShow :: Show a => Proxy a -> a -> Int+lengthOfShow _proxy = length . show++lengthOfHex+ :: (Integral a, Integral b, PrintfArg b)+ => Proxy a+ -> Proxy b+ -> a+ -> Int+lengthOfHex proxyA proxyB a = length (hex :: String)+ where+ hex = printf "%x" (b `asProxyTypeOf` proxyB)+ b = fromIntegral (a `asProxyTypeOf` proxyA) (<==>) :: Eq b => (a -> b) -> (a -> b) -> a -> Bool (<==>) = liftA2 (==)
test/TestCase/Data/NumberLength/Int.hs view
@@ -2,12 +2,12 @@ -- | -- Module: $HEADER$ -- Description: TODO--- Copyright: (c) 2015, Peter Trško+-- Copyright: (c) 2015-2016, Peter Trško -- License: BSD3 -- -- Stability: experimental -- Portability: NoImplicitPrelude-module TestCase.Data.NumberLength.Int+module TestCase.Data.NumberLength.Int (tests) where import Prelude@@ -28,10 +28,10 @@ import Text.Printf (PrintfArg, printf) import Text.Show (Show(show)) -import Test.HUnit (Assertion, (@?=)) import Test.Framework (Test) import Test.Framework.Providers.HUnit (testCase) import Test.Framework.Providers.QuickCheck2 (testProperty)+import Test.HUnit (Assertion, (@?=)) import Data.NumberLength.Int ( lengthInt@@ -49,45 +49,45 @@ tests :: [Test] tests =- [ testCase "lengthInt8 minBound" test_lengthInt8_minBound- , testCase "lengthInt8 maxBound" test_lengthInt8_maxBound- , testProperty "lengthInt8 = length . show" property_lengthInt8+ [ testCase "lengthInt8 minBound" test_lengthInt8_minBound+ , testCase "lengthInt8 maxBound" test_lengthInt8_maxBound+ , testProperty "lengthInt8 = length . show" property_lengthInt8 - , testCase "lengthInt16 minBound" test_lengthInt16_minBound- , testCase "lengthInt16 maxBound" test_lengthInt16_maxBound+ , testCase "lengthInt16 minBound" test_lengthInt16_minBound+ , testCase "lengthInt16 maxBound" test_lengthInt16_maxBound , testProperty "lengthInt16 = length . show" property_lengthInt16 - , testCase "lengthInt32 minBound" test_lengthInt32_minBound- , testCase "lengthInt32 maxBound" test_lengthInt32_maxBound+ , testCase "lengthInt32 minBound" test_lengthInt32_minBound+ , testCase "lengthInt32 maxBound" test_lengthInt32_maxBound , testProperty "lengthInt32 = length . show" property_lengthInt32 - , testCase "lengthInt64 minBound" test_lengthInt64_minBound- , testCase "lengthInt64 maxBound" test_lengthInt64_maxBound+ , testCase "lengthInt64 minBound" test_lengthInt64_minBound+ , testCase "lengthInt64 maxBound" test_lengthInt64_maxBound , testProperty "lengthInt64 = length . show" property_lengthInt64 - , testCase "lengthInt minBound" test_lengthInt_minBound- , testCase "lengthInt maxBound" test_lengthInt_maxBound- , testProperty "lengthInt = length . show" property_lengthInt+ , testCase "lengthInt minBound" test_lengthInt_minBound+ , testCase "lengthInt maxBound" test_lengthInt_maxBound+ , testProperty "lengthInt = length . show" property_lengthInt - , testCase "lengthInt8hex minBound" test_lengthInt8hex_minBound- , testCase "lengthInt8hex maxBound" test_lengthInt8hex_maxBound- , testProperty "lengthInt8hex = length . show" property_lengthInt8hex+ , testCase "lengthInt8hex minBound" test_lengthInt8hex_minBound+ , testCase "lengthInt8hex maxBound" test_lengthInt8hex_maxBound+ , testProperty "lengthInt8hex = length . show" property_lengthInt8hex - , testCase "lengthInt16hex minBound" test_lengthInt16hex_minBound- , testCase "lengthInt16hex maxBound" test_lengthInt16hex_maxBound+ , testCase "lengthInt16hex minBound" test_lengthInt16hex_minBound+ , testCase "lengthInt16hex maxBound" test_lengthInt16hex_maxBound , testProperty "lengthInt16hex = length . show" property_lengthInt16hex - , testCase "lengthInt32hex minBound" test_lengthInt32hex_minBound- , testCase "lengthInt32hex maxBound" test_lengthInt32hex_maxBound+ , testCase "lengthInt32hex minBound" test_lengthInt32hex_minBound+ , testCase "lengthInt32hex maxBound" test_lengthInt32hex_maxBound , testProperty "lengthInt32hex = length . show" property_lengthInt32hex - , testCase "lengthInt64hex minBound" test_lengthInt64hex_minBound- , testCase "lengthInt64hex maxBound" test_lengthInt64hex_maxBound+ , testCase "lengthInt64hex minBound" test_lengthInt64hex_minBound+ , testCase "lengthInt64hex maxBound" test_lengthInt64hex_maxBound , testProperty "lengthInt64hex = length . show" property_lengthInt64hex - , testCase "lengthIntHex minBound" test_lengthIntHex_minBound- , testCase "lengthIntHex maxBound" test_lengthIntHex_maxBound- , testProperty "lengthIntHex = length . show" property_lengthIntHex+ , testCase "lengthIntHex minBound" test_lengthIntHex_minBound+ , testCase "lengthIntHex maxBound" test_lengthIntHex_maxBound+ , testProperty "lengthIntHex = length . show" property_lengthIntHex ] numberLengthDec :: Show n => n -> Int
test/TestCase/Data/NumberLength/Word.hs view
@@ -2,12 +2,12 @@ -- | -- Module: $HEADER$ -- Description: TODO--- Copyright: (c) 2015, Peter Trško+-- Copyright: (c) 2015-2016, Peter Trško -- License: BSD3 -- -- Stability: experimental -- Portability: NoImplicitPrelude-module TestCase.Data.NumberLength.Word+module TestCase.Data.NumberLength.Word (tests) where import Prelude (Bounded(maxBound, minBound), Integral, fromIntegral)@@ -23,10 +23,10 @@ import Text.Printf (PrintfArg, printf) import Text.Show (Show(show)) -import Test.HUnit (Assertion, (@?=)) import Test.Framework (Test) import Test.Framework.Providers.HUnit (testCase) import Test.Framework.Providers.QuickCheck2 (testProperty)+import Test.HUnit (Assertion, (@?=)) import Data.NumberLength.Word ( lengthWord@@ -66,15 +66,15 @@ , testCase "lengthWord8hex minBound" test_lengthWord8hex_minBound , testCase "lengthWord8hex maxBound" test_lengthWord8hex_maxBound- , testProperty "lengthWord8hex = length . show" property_lengthWord8+ , testProperty "lengthWord8hex = length . show" property_lengthWord8hex , testCase "lengthWord16hex minBound" test_lengthWord16hex_minBound , testCase "lengthWord16hex maxBound" test_lengthWord16hex_maxBound- , testProperty "lengthWord16hex = length . show" property_lengthWord16+ , testProperty "lengthWord16hex = length . show" property_lengthWord16hex , testCase "lengthWord32hex minBound" test_lengthWord32hex_minBound , testCase "lengthWord32hex maxBound" test_lengthWord32hex_maxBound- , testProperty "lengthWord32hex = length . show" property_lengthWord32+ , testProperty "lengthWord32hex = length . show" property_lengthWord32hex , testCase "lengthWord64hex minBound" test_lengthWord64hex_minBound , testCase "lengthWord64hex maxBound" test_lengthWord64hex_maxBound