1import Control.Monad (replicateM)
3import qualified Data.ByteString.Lazy as B
5import Data.List (nub, (\\))
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
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 arbitrary = (fst . random) `fmap` rand
28 coarbitrary = undefined
30instance Arbitrary Word8 where
31 arbitrary = (fromIntegral . fst . randomR (0,255::Int)) `fmap` rand
32 coarbitrary = undefined
37 "nil is null" ~: assertBool "" (U.null U.nil),
38 "namespaceDNS is not null" ~: assertBool "" (not $ U.null U3.namespaceDNS)
43 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
44 "nil bytes" ~: U.toByteString U.nil @?= B.pack (replicate 16 0)
49 "conv bytes to string" ~:
50 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
51 "conv string to bytes" ~:
52 maybe B.empty (U.toByteString) (U.fromString s16) @?= b16
54 where b16 = B.pack [1..16]
55 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
57test_v1 :: [Maybe U.UUID] -> Test
58test_v1 v1s = TestList [
59 "V1 unique" ~: nub (v1s \\ nub v1s) @?= [],
60 "V1 not null" ~: TestList $ map (testUUID (not . U.null)) v1s,
61 "V1 valid" ~: TestList $ map (testUUID (isValidVersion 1)) v1s
63 where testUUID :: (U.UUID -> Bool) -> Maybe U.UUID -> Test
64 testUUID p u = maybe False p u ~? show u
69 U3.generateNamed U3.namespaceDNS name @?= uV3
71 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
72 uV3 = fromJust $ U.fromString "3d813cbb-47fb-32ba-91df-831e1593ac29"
77 U5.generateNamed U5.namespaceDNS name @?= uV5
79 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
80 uV5 = fromJust $ U.fromString "21f7f8de-8051-5b89-8680-0195ef798b6a"
82prop_stringRoundTrip :: Property
83prop_stringRoundTrip = label "String round trip" stringRoundTrip
84 where stringRoundTrip :: U.UUID -> Bool
85 stringRoundTrip u = maybe False (== u) $ U.fromString (U.toString u)
87prop_byteStringRoundTrip :: Property
88prop_byteStringRoundTrip = label "ByteString round trip" byteStringRoundTrip
89 where byteStringRoundTrip :: U.UUID -> Bool
90 byteStringRoundTrip u = maybe False (== u)
91 $ U.fromByteString (U.toByteString u)
93prop_stringLength :: Property
94prop_stringLength = label "String length" stringLength
95 where stringLength :: U.UUID -> Bool
96 stringLength u = length (U.toString u) == 36
98prop_byteStringLength :: Property
99prop_byteStringLength = label "ByteString length" byteStringLength
100 where byteStringLength :: U.UUID -> Bool
101 byteStringLength u = B.length (U.toByteString u) == 16
103prop_randomsDiffer :: Property
104prop_randomsDiffer = label "Randoms differ" randomsDiffer
105 where randomsDiffer :: (U.UUID, U.UUID) -> Bool
106 randomsDiffer (u1, u2) = u1 /= u2
108prop_randomNotNull :: Property
109prop_randomNotNull = label "Random not null" randomNotNull
110 where randomNotNull :: U.UUID -> Bool
111 randomNotNull = not. U.null
113prop_randomsValid :: Property
114prop_randomsValid = label "Random valid" randomsValid
115 where randomsValid :: U.UUID -> Bool
116 randomsValid = isValidVersion 4
118prop_v3NotNull :: Property
119prop_v3NotNull = label "V3 not null" v3NotNull
120 where v3NotNull :: [Word8] -> Bool
121 v3NotNull = not . U.null . U3.generateNamed U3.namespaceDNS
123prop_v3Valid :: Property
124prop_v3Valid = label "V3 valid" v3Valid
125 where v3Valid :: [Word8] -> Bool
126 v3Valid = isValidVersion 3 . U3.generateNamed U3.namespaceDNS
128prop_v5NotNull :: Property
129prop_v5NotNull = label "V5 not null" v5NotNull
130 where v5NotNull :: [Word8] -> Bool
131 v5NotNull = not . U.null . U5.generateNamed U5.namespaceDNS
133prop_v5Valid :: Property
134prop_v5Valid = label "V5 valid" v5Valid
135 where v5Valid :: [Word8] -> Bool
136 v5Valid = isValidVersion 5 . U5.generateNamed U5.namespaceDNS
141 v1s <- replicateM 100 $ U.nextUUID $ U.MAC 0 0 0 0 0 0
142 runTestText (putTextToHandle stderr False) (TestList [
151 prop_stringRoundTrip,
152 prop_byteStringRoundTrip,
154 prop_byteStringLength,
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list