[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 14:45:24 UTC 2010
The following commit has been merged in the master branch:
commit 2d3a1471e899f92160c4b5b67c4f4d9c7b90f59f
Author: John Goerzen <jgoerzen at complete.org>
Date: Sat Oct 23 02:29:43 2004 +0100
Added Bits
Keywords:
(jgoerzen at complete.org--projects/missingh--head--1.0--patch-101)
diff --git a/ChangeLog b/ChangeLog
index 22dfb37..390557f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,21 @@
# arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--1.0
#
+2004-10-22 20:29:43 GMT John Goerzen <jgoerzen at complete.org> patch-101
+
+ Summary:
+ Added Bits
+ Revision:
+ missingh--head--1.0--patch-101
+
+
+ new files:
+ libsrc/MissingH/Bits.hs testsrc/Bitstest.hs
+
+ modified files:
+ ChangeLog Setup.description testsrc/Tests.hs
+
+
2004-10-22 18:46:52 GMT John Goerzen <jgoerzen at complete.org> patch-100
Summary:
diff --git a/Setup.description b/Setup.description
index 33e3eb8..5bd151f 100644
--- a/Setup.description
+++ b/Setup.description
@@ -18,6 +18,7 @@ Modules: MissingH.IO, MissingH.IO.Binary, MissingH.List,
MissingH.Network.FTP.Client,
MissingH.Network.FTP.Parser,
MissingH.Parsec,
+ MissingH.Bits,
MissingH.Wash.Mail.Email,
MissingH.Wash.Mail.EmailConfig,
MissingH.Wash.Mail.HeaderField,
diff --git a/libsrc/MissingH/Bits.hs b/libsrc/MissingH/Bits.hs
new file mode 100644
index 0000000..f6b46b7
--- /dev/null
+++ b/libsrc/MissingH/Bits.hs
@@ -0,0 +1,65 @@
+{- arch-tag: Bit utilities main file
+Copyright (C) 2004 John Goerzen <jgoerzen at complete.org>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+-}
+
+{- |
+ Module : MissingH.Bits
+ Copyright : Copyright (C) 2004 John Goerzen
+ License : GNU GPL, version 2 or above
+
+ Maintainer : John Goerzen,
+ Maintainer : jgoerzen at complete.org
+ Stability : provisional
+ Portability: portable to platforms with rawSystem
+
+ Bit-related utilities
+
+Written by John Goerzen, jgoerzen\@complete.org
+-}
+
+module MissingH.Bits(getBytes, fromBytes)
+where
+import Data.Bits
+
+{- | Returns a list representing the bytes that comprise a data type.
+
+Example:
+
+> getBytes (0x12345678::Int) -> [0x12, 0x34, 0x56, 0x78]
+-}
+getBytes :: (Integral a, Bounded a, Bits a) => a -> [a]
+getBytes input =
+ let getByte x 0 = []
+ getByte x remaining = (x .&. 0xff) : getByte (shiftR x 8) (remaining - 1)
+ in
+ if (bitSize input `mod` 8) /= 0
+ then error "Input data bit size must be a multiple of 8"
+ else reverse $ getByte input (bitSize input `div` 8)
+
+{- | The opposite of 'getBytes', this function builds a number based on
+its component bytes.
+
+Results are undefined if any components of the input list are > 0xff!
+
+-}
+
+fromBytes :: (Bits a) => [a] -> a
+fromBytes input =
+ let dofb accum [] = accum
+ dofb accum (x:xs) = dofb ((shiftL accum 8) .|. x) xs
+ in
+ dofb 0 input
diff --git a/testsrc/Bitstest.hs b/testsrc/Bitstest.hs
new file mode 100644
index 0000000..944df72
--- /dev/null
+++ b/testsrc/Bitstest.hs
@@ -0,0 +1,50 @@
+{- arch-tag: Bits tests main file
+Copyright (C) 2004 John Goerzen <jgoerzen at complete.org>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+-}
+
+module Bitstest(tests) where
+import HUnit
+import MissingH.Bits
+import Data.Word
+
+test_fromBytes =
+ let f :: [Word32] -> Word32 -> Assertion
+ f inp exp = exp @=? fromBytes inp in
+ do
+ f [] 0
+ f [0] 0
+ f [1] 1
+ f [0xff, 0] 0xff00
+ f [0x0, 0xff] 0xff
+ f [0x12, 0x34, 0x56, 0x78] 0x12345678
+ f [0xff, 0xff, 0xff, 0xff] 0xffffffff
+ f [0xff, 0, 0, 0] 0xff000000
+
+test_getBytes =
+ let f :: Word32 -> [Word32] -> Assertion
+ f inp exp = exp @=? getBytes inp in
+ do
+ f 0 [0, 0, 0, 0]
+ f 0x1200 [0, 0, 0x12, 0]
+ f 0x0012 [0, 0, 0, 0x12]
+ f 0xffffffff [0xff, 0xff, 0xff, 0xff]
+ f 0x12345678 [0x12, 0x34, 0x56, 0x78]
+ f 0xf0000000 [0xf0, 0, 0, 0]
+
+tests = TestList [TestLabel "getBytes" (TestCase test_getBytes),
+ TestLabel "fromBytes" (TestCase test_fromBytes)
+ ]
\ No newline at end of file
diff --git a/testsrc/Tests.hs b/testsrc/Tests.hs
index 4b281cc..cef9ef6 100644
--- a/testsrc/Tests.hs
+++ b/testsrc/Tests.hs
@@ -24,6 +24,7 @@ import qualified FiniteMaptest
import qualified Pathtest
import qualified Strtest
import qualified IOtest
+import qualified Bitstest
import qualified Network.FTP.Parsertest
test1 = TestCase ("x" @=? "x")
@@ -34,6 +35,7 @@ tests = TestList [TestLabel "test1" test1,
TestLabel "FiniteMap" FiniteMaptest.tests,
TestLabel "Path" Pathtest.tests,
TestLabel "MIMETypes" MIMETypestest.tests,
+ TestLabel "Bitstest" Bitstest.tests,
TestLabel "Network.FTP.Parser" Network.FTP.Parsertest.tests]
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list