on 8ee7175 help sign in
typeable/uuid/tests/TestUUID.hs
162 lines 5.3 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
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 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
35test_null :: Test
36test_null = TestList [
37 "nil is null" ~: assertBool "" (U.null U.nil),
38 "namespaceDNS is not null" ~: assertBool "" (not $ U.null U3.namespaceDNS)
39 ]
41test_nil :: Test
42test_nil = TestList [
43 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
44 "nil bytes" ~: U.toByteString U.nil @?= B.pack (replicate 16 0)
45 ]
47test_conv :: Test
48test_conv = TestList [
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
53 ]
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
62 ]
63 where testUUID :: (U.UUID -> Bool) -> Maybe U.UUID -> Test
64 testUUID p u = maybe False p u ~? show u
66test_v3 :: Test
67test_v3 = TestList [
68 "V3 computation" ~:
69 U3.generateNamed U3.namespaceDNS name @?= uV3
70 ]
71 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
72 uV3 = fromJust $ U.fromString "3d813cbb-47fb-32ba-91df-831e1593ac29"
74test_v5 :: Test
75test_v5 = TestList [
76 "V5 computation" ~:
77 U5.generateNamed U5.namespaceDNS name @?= uV5
78 ]
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
139main :: IO ()
140main = do
141 v1s <- replicateM 100 U.nextUUID
142 runTestText (putTextToHandle stderr False) (TestList [
143 test_null,
144 test_nil,
145 test_conv,
146 test_v1 v1s,
147 test_v3,
148 test_v5
149 ])
150 mapM_ quickCheck $ [
151 prop_stringRoundTrip,
152 prop_byteStringRoundTrip,
153 prop_stringLength,
154 prop_byteStringLength,
155 prop_randomsDiffer,
156 prop_randomNotNull,
157 prop_randomsValid,
158 prop_v3NotNull,
159 prop_v3Valid,
160 prop_v5NotNull,
161 prop_v5Valid
162 ]