@@ -1,18 +1,11 @@
1{-# LANGUAGE ViewPatterns #-}
1{-# LANGUAGE ViewPatterns #-}
2−import Control.Monad (replicateM)
4import qualified Data.ByteString.Lazy as BL
2import qualified Data.ByteString.Lazy as BL
5import qualified Data.ByteString.Lazy.Char8 as BL8
3import qualified Data.ByteString.Lazy.Char8 as BL8
6import qualified Data.ByteString.Char8 as BC8
4import qualified Data.ByteString.Char8 as BC8
7import Data.Char (ord)
5import Data.Char (ord)
8import Data.Functor ((<$>))
6import Data.Functor ((<$>))
9−import Data.List (nub, (\\))
11import Data.Word
7import Data.Word
12−import qualified Data.UUID as U
8+import qualified Data.UUID.Types as U
13−import qualified Data.UUID.V1 as U
14−import qualified Data.UUID.V3 as U3
15−import qualified Data.UUID.V5 as U5
16import Foreign (alloca, peek, poke)
9import Foreign (alloca, peek, poke)
17import System.IO.Unsafe (unsafePerformIO)
10import System.IO.Unsafe (unsafePerformIO)
18import qualified Test.HUnit as H
11import qualified Test.HUnit as H
@@ -23,14 +16,6 @@ import Test.Framework.Providers.HUnit (hUnitTestToTests)
23import Test.Framework.Providers.QuickCheck2 (testProperty)
16import Test.Framework.Providers.QuickCheck2 (testProperty)
26−isValidVersion :: Int -> U.UUID -> Bool
27−isValidVersion v u = lenOK && variantOK && versionOK
28− where bs = U.toByteString u
29− lenOK = BL.length bs == 16
30− variantOK = (BL.index bs 8) .&. 0xc0 == 0x80
31− versionOK = (BL.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
34instance Arbitrary U.UUID where
19instance Arbitrary U.UUID where
35 -- the UUID random instance ignores bounds
20 -- the UUID random instance ignores bounds
36 arbitrary = choose (U.nil, U.nil)
21 arbitrary = choose (U.nil, U.nil)
@@ -38,8 +23,7 @@ instance Arbitrary U.UUID where
39test_null :: H.Test
24test_null :: H.Test
40test_null = H.TestList [
25test_null = H.TestList [
41− "nil is null" ~: assertBool "" (U.null U.nil),
26+ "nil is null" ~: assertBool "" (U.null U.nil)
42− "namespaceDNS is not null" ~: assertBool "" (not $ U.null U3.namespaceDNS)
45test_nil :: H.Test
29test_nil :: H.Test
@@ -58,32 +42,6 @@ test_conv = H.TestList [
58 where b16 = BL.pack [1..16]
42 where b16 = BL.pack [1..16]
59 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
43 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
61−test_v1 :: [Maybe U.UUID] -> H.Test
62−test_v1 v1s = H.TestList [
63− "V1 unique" ~: nub (v1s \\ nub v1s) @?= [],
64− "V1 not null" ~: H.TestList $ map (testUUID (not . U.null)) v1s,
65− "V1 valid" ~: H.TestList $ map (testUUID (isValidVersion 1)) v1s
67− where testUUID :: (U.UUID -> Bool) -> Maybe U.UUID -> H.Test
68− testUUID p u = maybe False p u ~? show u
71−test_v3 = H.TestList [
73− U3.generateNamed U3.namespaceDNS name @?= uV3
75− where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
76− uV3 = fromJust $ U.fromString "3d813cbb-47fb-32ba-91df-831e1593ac29"
79−test_v5 = H.TestList [
81− U5.generateNamed U5.namespaceDNS name @?= uV5
83− where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
84− uV5 = fromJust $ U.fromString "21f7f8de-8051-5b89-8680-0195ef798b6a"
87-- | Test fromByteString with a fixed-input.
45-- | Test fromByteString with a fixed-input.
88test_fromByteString :: H.Test
46test_fromByteString :: H.Test
89test_fromByteString =
47test_fromByteString =
@@ -131,31 +89,6 @@ prop_randomNotNull = testProperty "Random not null" randomNotNull
131 where randomNotNull :: U.UUID -> Bool
89 where randomNotNull :: U.UUID -> Bool
132 randomNotNull = not. U.null
90 randomNotNull = not. U.null
134−prop_randomsValid :: Test
135−prop_randomsValid = testProperty "Random valid" randomsValid
136− where randomsValid :: U.UUID -> Bool
137− randomsValid = isValidVersion 4
139−prop_v3NotNull :: Test
140−prop_v3NotNull = testProperty "V3 not null" v3NotNull
141− where v3NotNull :: [Word8] -> Bool
142− v3NotNull = not . U.null . U3.generateNamed U3.namespaceDNS
145−prop_v3Valid = testProperty "V3 valid" v3Valid
146− where v3Valid :: [Word8] -> Bool
147− v3Valid = isValidVersion 3 . U3.generateNamed U3.namespaceDNS
149−prop_v5NotNull :: Test
150−prop_v5NotNull = testProperty "V5 not null" v5NotNull
151− where v5NotNull :: [Word8] -> Bool
152− v5NotNull = not . U.null . U5.generateNamed U5.namespaceDNS
155−prop_v5Valid = testProperty "V5 valid" v5Valid
156− where v5Valid :: [Word8] -> Bool
157− v5Valid = isValidVersion 5 . U5.generateNamed U5.namespaceDNS
159prop_readShowRoundTrip :: Test
92prop_readShowRoundTrip :: Test
160prop_readShowRoundTrip = testProperty "Read/Show round-trip" prop
93prop_readShowRoundTrip = testProperty "Read/Show round-trip" prop
161 where -- we're using 'Maybe UUID' to add a bit of
94 where -- we're using 'Maybe UUID' to add a bit of
@@ -210,16 +143,12 @@ prop_storableRoundTrip =
211main :: IO ()
144main :: IO ()
213− v1s <- replicateM 100 U.nextUUID
214 defaultMain $
146 defaultMain $
216 [ hUnitTestToTests $ H.TestList [
148 [ hUnitTestToTests $ H.TestList [
217 test_null,
149 test_null,
218 test_nil,
150 test_nil,
219 test_conv,
151 test_conv,
223 test_fromByteString,
152 test_fromByteString,
224 test_fromWords
153 test_fromWords
@@ -230,12 +159,7 @@ main = do
230 prop_stringLength,
159 prop_stringLength,
231 prop_byteStringLength,
160 prop_byteStringLength,
232 prop_randomsDiffer,
161 prop_randomsDiffer,
233− prop_randomNotNull,
162+ prop_randomNotNull
240 , [ testProperty "fromASCIIBytes_fromString1" fromASCIIBytes_fromString1
164 , [ testProperty "fromASCIIBytes_fromString1" fromASCIIBytes_fromString1
241 , testProperty "fromASCIIBytes_fromString2" fromASCIIBytes_fromString2
165 , testProperty "fromASCIIBytes_fromString2" fromASCIIBytes_fromString2