Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge

  • Loading branch information...
commit 4af07a7e9f8c1957ce9b436f1b5d12039d24f048 2 parents e10bee1 + e07d5fc
Bryan O'Sullivan authored March 11, 2014
10  base16-bytestring.cabal
@@ -14,7 +14,7 @@ category:            Data
14 14
 build-type:          Simple
15 15
 extra-source-files:  README.markdown
16 16
 
17  
-Cabal-version:       >=1.6
  17
+Cabal-version:       >=1.8
18 18
 
19 19
 library
20 20
   exposed-modules:
@@ -36,3 +36,11 @@ source-repository head
36 36
 source-repository head
37 37
   type:     mercurial
38 38
   location: http://bitbucket.org/bos/base16-bytestring
  39
+
  40
+test-suite test
  41
+  type: exitcode-stdio-1.0
  42
+  hs-source-dirs: tests
  43
+  main-is: Tests.hs
  44
+  build-depends: base
  45
+               , base16-bytestring
  46
+               , bytestring
52  tests/Tests.hs
... ...
@@ -0,0 +1,52 @@
  1
+import Data.Char (ord)
  2
+import Data.Word (Word8)
  3
+import qualified Data.ByteString as B
  4
+import qualified Data.ByteString.Base16 as Base16
  5
+
  6
+-- Three-line "test framework" for Haskell.  Crude, but at least it tells you
  7
+-- the line number of the failure without you having to specify it.
  8
+shouldBe :: (Eq a, Show a) => a -> a -> IO Bool
  9
+shouldBe a b = return (a == b)
  10
+infix 0 `shouldBe`
  11
+
  12
+c2w :: Char -> Word8
  13
+c2w = fromIntegral . ord
  14
+
  15
+main :: IO ()
  16
+main = do
  17
+    let hexL  = map c2w "0123456789abcdef"
  18
+        hexU  = map c2w "0123456789ABCDEF"
  19
+        hexUL = map c2w "0123456789ABCDEFabcdef"
  20
+        notHex = [c | c <- [0..255], c `notElem` hexUL]
  21
+        hexL2 = do a <- hexL; b <- hexL; [a,b]
  22
+        hexU2 = do a <- hexU; b <- hexU; [a,b]
  23
+        bytes = B.pack [0..255]
  24
+
  25
+    -- Encode every byte
  26
+    True <- Base16.encode bytes `shouldBe` B.pack hexL2
  27
+
  28
+    -- Decode every valid hex pair
  29
+    True <- Base16.decode (B.pack hexL2) `shouldBe` (bytes, B.empty)
  30
+    True <- Base16.decode (B.pack hexU2) `shouldBe` (bytes, B.empty)
  31
+
  32
+    -- Decode every invalid byte paired with a correct byte
  33
+    let bads1 = [B.pack [a,b] | a <- notHex, b <- hexUL]
  34
+    let bads2 = [B.pack [a,b] | a <- hexUL, b <- notHex]
  35
+    True <- map Base16.decode bads1 `shouldBe` map (\s -> (B.empty, s)) bads1
  36
+    True <- map Base16.decode bads2 `shouldBe` map (\s -> (B.empty, s)) bads2
  37
+
  38
+    -- Like above, but start with a correct byte
  39
+    let correctHex   = B.pack [97,98]
  40
+        correctBytes = B.pack [171]
  41
+    True <- map (Base16.decode . (correctHex `B.append`)) bads1
  42
+            `shouldBe` map (\s -> (correctBytes, s)) bads1
  43
+    True <- map (Base16.decode . (correctHex `B.append`)) bads2
  44
+            `shouldBe` map (\s -> (correctBytes, s)) bads2
  45
+
  46
+    -- Like above, but end with a correct byte
  47
+    True <- map (Base16.decode . (`B.append` correctHex)) bads1
  48
+            `shouldBe` map (\s -> (B.empty, s `B.append` correctHex)) bads1
  49
+    True <- map (Base16.decode . (`B.append` correctHex)) bads2
  50
+            `shouldBe` map (\s -> (B.empty, s `B.append` correctHex)) bads2
  51
+
  52
+    return ()

0 notes on commit 4af07a7

Please sign in to comment.
Something went wrong with that request. Please try again.