on master help sign in
add tests for ByteString functions
commitfce410d5cd097137c1f119f05eae657a2c8b3569 authorFrancesco Mazzoli <f@mazzo.li> dateSat 19 Oct 2013, 10:28 (13 years ago) parent3fd3cf5
1 file changed+41−9
tests/TestUUID.hs+41−9
tests/TestUUID.hs+41−9
@@ -1,7 +1,10 @@
1+{-# LANGUAGE ViewPatterns #-}
12import Control.Monad (replicateM)
23import Data.Bits
3−import qualified Data.ByteString.Lazy as B
4+import qualified Data.ByteString.Lazy as BL
5+import qualified Data.ByteString.Char8 as BC8
46import Data.Char (ord)
7+import Data.Functor ((<$>))
58import Data.List (nub, (\\))
69import Data.Maybe
710import Data.Word
@@ -21,9 +24,9 @@ import System.IO
2124isValidVersion :: Int -> U.UUID -> Bool
2225isValidVersion v u = lenOK && variantOK && versionOK
2326 where bs = U.toByteString u
24− lenOK = B.length bs == 16
25− variantOK = (B.index bs 8) .&. 0xc0 == 0x80
26− versionOK = (B.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
27+ lenOK = BL.length bs == 16
28+ variantOK = (BL.index bs 8) .&. 0xc0 == 0x80
29+ versionOK = (BL.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
2730
2831
2932instance Arbitrary U.UUID where
@@ -40,7 +43,7 @@ test_null = H.TestList [
4043test_nil :: H.Test
4144test_nil = H.TestList [
4245 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
43− "nil bytes" ~: U.toByteString U.nil @?= B.pack (replicate 16 0)
46+ "nil bytes" ~: U.toByteString U.nil @?= BL.pack (replicate 16 0)
4447 ]
4548
4649test_conv :: H.Test
@@ -48,9 +51,9 @@ test_conv = H.TestList [
4851 "conv bytes to string" ~:
4952 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
5053 "conv string to bytes" ~:
51− maybe B.empty (U.toByteString) (U.fromString s16) @?= b16
54+ maybe BL.empty (U.toByteString) (U.fromString s16) @?= b16
5255 ]
53− where b16 = B.pack [1..16]
56+ where b16 = BL.pack [1..16]
5457 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
5558
5659test_v1 :: [Maybe U.UUID] -> H.Test
@@ -97,7 +100,7 @@ prop_stringLength = testProperty "String length" stringLength
97100prop_byteStringLength :: Test
98101prop_byteStringLength = testProperty "ByteString length" byteStringLength
99102 where byteStringLength :: U.UUID -> Bool
100− byteStringLength u = B.length (U.toByteString u) == 16
103+ byteStringLength u = BL.length (U.toByteString u) == 16
101104
102105prop_randomsDiffer :: Test
103106prop_randomsDiffer = testProperty "Randoms differ" randomsDiffer
@@ -141,6 +144,29 @@ prop_readShowRoundTrip = testProperty "Read/Show round-trip" prop
141144 prop :: U.UUID -> Bool
142145 prop uuid = read (show (Just uuid)) == Just uuid
143146
147+-- Mostly going to test for wrong UUIDs
148+fromASCIIBytes_fromString1 :: String -> Bool
149+fromASCIIBytes_fromString1 s =
150+ if all (\c -> ord c < 256) s
151+ then U.fromString s == U.fromASCIIBytes (BC8.pack s)
152+ else True
153+
154+fromASCIIBytes_fromString2 :: U.UUID -> Bool
155+fromASCIIBytes_fromString2 (U.toString -> s) =
156+ U.fromString s == U.fromASCIIBytes (BC8.pack s)
157+
158+toASCIIBytes_toString :: U.UUID -> Bool
159+toASCIIBytes_toString uuid =
160+ U.toString uuid == BC8.unpack (U.toASCIIBytes uuid)
161+
162+fromASCIIBytes_toASCIIBytes :: U.UUID -> Bool
163+fromASCIIBytes_toASCIIBytes (BC8.pack . U.toString -> bs) =
164+ Just bs == (U.toASCIIBytes <$> U.fromASCIIBytes bs)
165+
166+toASCIIBytes_fromASCIIBytes :: U.UUID -> Bool
167+toASCIIBytes_fromASCIIBytes uuid =
168+ Just uuid == U.fromASCIIBytes (U.toASCIIBytes uuid)
169+
144170main :: IO ()
145171main = do
146172 v1s <- replicateM 100 U.nextUUID
@@ -167,4 +193,10 @@ main = do
167193 prop_v5NotNull,
168194 prop_v5Valid
169195 ]
170− ]
196+ , [ testProperty "fromASCIIBytes_fromString1" fromASCIIBytes_fromString1
197+ , testProperty "fromASCIIBytes_fromString2" fromASCIIBytes_fromString2
198+ , testProperty "fromASCIIBytes_toString" toASCIIBytes_toString
199+ , testProperty "fromASCIIBytes_toASCIIBytes" fromASCIIBytes_toASCIIBytes
200+ , testProperty "toASCIIBytes_fromASCIIBytes" toASCIIBytes_fromASCIIBytes
201+ ]
202+ ]