on cee7ffa help sign in
typeable/uuid/tests/TestUUID.hs
168 lines 5.6 kB history raw
1import Control.Monad (replicateM)
2import Data.Bits
3import qualified Data.ByteString.Lazy as B
4import Data.Char (ord)
5import Data.List (nub, (\\))
6import Data.Maybe
7import Data.Word
8import qualified Data.UUID as U
9import qualified Data.UUID.V1 as U
10import qualified Data.UUID.V3 as U3
11import qualified Data.UUID.V5 as U5
12import Test.HUnit
13import Test.QuickCheck hiding ((.&.))
14import System.IO
15import System.Random
18isValidVersion :: Int -> U.UUID -> Bool
19isValidVersion v u = lenOK && variantOK && versionOK
20 where bs = U.toByteString u
21 lenOK = B.length bs == 16
22 variantOK = (B.index bs 8) .&. 0xc0 == 0x80
23 versionOK = (B.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
26instance Arbitrary U.UUID where
27 -- the UUID random instance ignores bounds
28 arbitrary = choose (U.nil, U.nil)
30-- instance Arbitrary Word8 where
31-- arbitrary = (fromIntegral . fst . randomR (0,255::Int)) `fmap` rand
34test_null :: Test
35test_null = TestList [
36 "nil is null" ~: assertBool "" (U.null U.nil),
37 "namespaceDNS is not null" ~: assertBool "" (not $ U.null U3.namespaceDNS)
38 ]
40test_nil :: Test
41test_nil = TestList [
42 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
43 "nil bytes" ~: U.toByteString U.nil @?= B.pack (replicate 16 0)
44 ]
46test_conv :: Test
47test_conv = TestList [
48 "conv bytes to string" ~:
49 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
50 "conv string to bytes" ~:
51 maybe B.empty (U.toByteString) (U.fromString s16) @?= b16
52 ]
53 where b16 = B.pack [1..16]
54 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
56test_v1 :: [Maybe U.UUID] -> Test
57test_v1 v1s = TestList [
58 "V1 unique" ~: nub (v1s \\ nub v1s) @?= [],
59 "V1 not null" ~: TestList $ map (testUUID (not . U.null)) v1s,
60 "V1 valid" ~: TestList $ map (testUUID (isValidVersion 1)) v1s
61 ]
62 where testUUID :: (U.UUID -> Bool) -> Maybe U.UUID -> Test
63 testUUID p u = maybe False p u ~? show u
65test_v3 :: Test
66test_v3 = TestList [
67 "V3 computation" ~:
68 U3.generateNamed U3.namespaceDNS name @?= uV3
69 ]
70 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
71 uV3 = fromJust $ U.fromString "3d813cbb-47fb-32ba-91df-831e1593ac29"
73test_v5 :: Test
74test_v5 = TestList [
75 "V5 computation" ~:
76 U5.generateNamed U5.namespaceDNS name @?= uV5
77 ]
78 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
79 uV5 = fromJust $ U.fromString "21f7f8de-8051-5b89-8680-0195ef798b6a"
81prop_stringRoundTrip :: Property
82prop_stringRoundTrip = label "String round trip" stringRoundTrip
83 where stringRoundTrip :: U.UUID -> Bool
84 stringRoundTrip u = maybe False (== u) $ U.fromString (U.toString u)
86prop_byteStringRoundTrip :: Property
87prop_byteStringRoundTrip = label "ByteString round trip" byteStringRoundTrip
88 where byteStringRoundTrip :: U.UUID -> Bool
89 byteStringRoundTrip u = maybe False (== u)
90 $ U.fromByteString (U.toByteString u)
92prop_stringLength :: Property
93prop_stringLength = label "String length" stringLength
94 where stringLength :: U.UUID -> Bool
95 stringLength u = length (U.toString u) == 36
97prop_byteStringLength :: Property
98prop_byteStringLength = label "ByteString length" byteStringLength
99 where byteStringLength :: U.UUID -> Bool
100 byteStringLength u = B.length (U.toByteString u) == 16
102prop_randomsDiffer :: Property
103prop_randomsDiffer = label "Randoms differ" randomsDiffer
104 where randomsDiffer :: (U.UUID, U.UUID) -> Bool
105 randomsDiffer (u1, u2) = u1 /= u2
107prop_randomNotNull :: Property
108prop_randomNotNull = label "Random not null" randomNotNull
109 where randomNotNull :: U.UUID -> Bool
110 randomNotNull = not. U.null
112prop_randomsValid :: Property
113prop_randomsValid = label "Random valid" randomsValid
114 where randomsValid :: U.UUID -> Bool
115 randomsValid = isValidVersion 4
117prop_v3NotNull :: Property
118prop_v3NotNull = label "V3 not null" v3NotNull
119 where v3NotNull :: [Word8] -> Bool
120 v3NotNull = not . U.null . U3.generateNamed U3.namespaceDNS
122prop_v3Valid :: Property
123prop_v3Valid = label "V3 valid" v3Valid
124 where v3Valid :: [Word8] -> Bool
125 v3Valid = isValidVersion 3 . U3.generateNamed U3.namespaceDNS
127prop_v5NotNull :: Property
128prop_v5NotNull = label "V5 not null" v5NotNull
129 where v5NotNull :: [Word8] -> Bool
130 v5NotNull = not . U.null . U5.generateNamed U5.namespaceDNS
132prop_v5Valid :: Property
133prop_v5Valid = label "V5 valid" v5Valid
134 where v5Valid :: [Word8] -> Bool
135 v5Valid = isValidVersion 5 . U5.generateNamed U5.namespaceDNS
137prop_readShowRoundTrip :: Property
138prop_readShowRoundTrip = label "Read/Show round-trip" prop
139 where -- we're using 'Maybe UUID' to add a bit of
140 -- real-world complexity.
141 prop :: U.UUID -> Bool
142 prop uuid = read (show (Just uuid)) == Just uuid
144main :: IO ()
145main = do
146 v1s <- replicateM 100 U.nextUUID
147 runTestText (putTextToHandle stderr False) (TestList [
148 test_null,
149 test_nil,
150 test_conv,
151 test_v1 v1s,
152 test_v3,
153 test_v5
154 ])
155 mapM_ quickCheck $ [
156 prop_stringRoundTrip,
157 prop_readShowRoundTrip,
158 prop_byteStringRoundTrip,
159 prop_stringLength,
160 prop_byteStringLength,
161 prop_randomsDiffer,
162 prop_randomNotNull,
163 prop_randomsValid,
164 prop_v3NotNull,
165 prop_v3Valid,
166 prop_v5NotNull,
167 prop_v5Valid
168 ]