@@ -1,7 +1,10 @@
1+{-# LANGUAGE ViewPatterns #-}
1import Control.Monad (replicateM)
2import Control.Monad (replicateM)
2import Data.Bits
3import 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
4import Data.Char (ord)
6import Data.Char (ord)
7+import Data.Functor ((<$>))
5import Data.List (nub, (\\))
8import Data.List (nub, (\\))
6import Data.Maybe
9import Data.Maybe
7import Data.Word
10import Data.Word
@@ -21,9 +24,9 @@ import System.IO
21isValidVersion :: Int -> U.UUID -> Bool
24isValidVersion :: Int -> U.UUID -> Bool
22isValidVersion v u = lenOK && variantOK && versionOK
25isValidVersion v u = lenOK && variantOK && versionOK
23 where bs = U.toByteString u
26 where bs = U.toByteString u
24− lenOK = B.length bs == 16
27+ lenOK = BL.length bs == 16
25− variantOK = (B.index bs 8) .&. 0xc0 == 0x80
28+ variantOK = (BL.index bs 8) .&. 0xc0 == 0x80
26− versionOK = (B.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
29+ versionOK = (BL.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
29instance Arbitrary U.UUID where
32instance Arbitrary U.UUID where
@@ -40,7 +43,7 @@ test_null = H.TestList [
40test_nil :: H.Test
43test_nil :: H.Test
41test_nil = H.TestList [
44test_nil = H.TestList [
42 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
45 "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)
46test_conv :: H.Test
49test_conv :: H.Test
@@ -48,9 +51,9 @@ test_conv = H.TestList [
48 "conv bytes to string" ~:
51 "conv bytes to string" ~:
49 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
52 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
50 "conv string to bytes" ~:
53 "conv string to bytes" ~:
51− maybe B.empty (U.toByteString) (U.fromString s16) @?= b16
54+ maybe BL.empty (U.toByteString) (U.fromString s16) @?= b16
53− where b16 = B.pack [1..16]
56+ where b16 = BL.pack [1..16]
54 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
57 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
56test_v1 :: [Maybe U.UUID] -> H.Test
59test_v1 :: [Maybe U.UUID] -> H.Test
@@ -97,7 +100,7 @@ prop_stringLength = testProperty "String length" stringLength
97prop_byteStringLength :: Test
100prop_byteStringLength :: Test
98prop_byteStringLength = testProperty "ByteString length" byteStringLength
101prop_byteStringLength = testProperty "ByteString length" byteStringLength
99 where byteStringLength :: U.UUID -> Bool
102 where byteStringLength :: U.UUID -> Bool
100− byteStringLength u = B.length (U.toByteString u) == 16
103+ byteStringLength u = BL.length (U.toByteString u) == 16
102prop_randomsDiffer :: Test
105prop_randomsDiffer :: Test
103prop_randomsDiffer = testProperty "Randoms differ" randomsDiffer
106prop_randomsDiffer = testProperty "Randoms differ" randomsDiffer
@@ -141,6 +144,29 @@ prop_readShowRoundTrip = testProperty "Read/Show round-trip" prop
141 prop :: U.UUID -> Bool
144 prop :: U.UUID -> Bool
142 prop uuid = read (show (Just uuid)) == Just uuid
145 prop uuid = read (show (Just uuid)) == Just uuid
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)
154+fromASCIIBytes_fromString2 :: U.UUID -> Bool
155+fromASCIIBytes_fromString2 (U.toString -> s) =
156+ U.fromString s == U.fromASCIIBytes (BC8.pack s)
158+toASCIIBytes_toString :: U.UUID -> Bool
159+toASCIIBytes_toString uuid =
160+ U.toString uuid == BC8.unpack (U.toASCIIBytes uuid)
162+fromASCIIBytes_toASCIIBytes :: U.UUID -> Bool
163+fromASCIIBytes_toASCIIBytes (BC8.pack . U.toString -> bs) =
164+ Just bs == (U.toASCIIBytes <$> U.fromASCIIBytes bs)
166+toASCIIBytes_fromASCIIBytes :: U.UUID -> Bool
167+toASCIIBytes_fromASCIIBytes uuid =
168+ Just uuid == U.fromASCIIBytes (U.toASCIIBytes uuid)
144main :: IO ()
170main :: IO ()
146 v1s <- replicateM 100 U.nextUUID
172 v1s <- replicateM 100 U.nextUUID
@@ -167,4 +193,10 @@ main = do
167 prop_v5NotNull,
193 prop_v5NotNull,
168 prop_v5Valid
194 prop_v5Valid
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