on d419272 help sign in
typeable/uuid/tests/TestUUID.hs
201 lines 7.0 kB history raw
1{-# LANGUAGE ViewPatterns #-}
2import Control.Monad (replicateM)
3import Data.Bits
4import qualified Data.ByteString.Lazy as BL
5import qualified Data.ByteString.Char8 as BC8
6import Data.Char (ord)
7import Data.Functor ((<$>))
8import Data.List (nub, (\\))
9import Data.Maybe
10import Data.Word
11import qualified Data.UUID as U
12import qualified Data.UUID.V1 as U
13import qualified Data.UUID.V3 as U3
14import qualified Data.UUID.V5 as U5
15import qualified Test.HUnit as H
16import Test.HUnit hiding (Test)
17import Test.QuickCheck hiding ((.&.))
18import Test.Framework (defaultMain, Test)
19import Test.Framework.Providers.HUnit (hUnitTestToTests)
20import Test.Framework.Providers.QuickCheck2 (testProperty)
23isValidVersion :: Int -> U.UUID -> Bool
24isValidVersion v u = lenOK && variantOK && versionOK
25 where bs = U.toByteString u
26 lenOK = BL.length bs == 16
27 variantOK = (BL.index bs 8) .&. 0xc0 == 0x80
28 versionOK = (BL.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
31instance Arbitrary U.UUID where
32 -- the UUID random instance ignores bounds
33 arbitrary = choose (U.nil, U.nil)
36test_null :: H.Test
37test_null = H.TestList [
38 "nil is null" ~: assertBool "" (U.null U.nil),
39 "namespaceDNS is not null" ~: assertBool "" (not $ U.null U3.namespaceDNS)
40 ]
42test_nil :: H.Test
43test_nil = H.TestList [
44 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
45 "nil bytes" ~: U.toByteString U.nil @?= BL.pack (replicate 16 0)
46 ]
48test_conv :: H.Test
49test_conv = H.TestList [
50 "conv bytes to string" ~:
51 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
52 "conv string to bytes" ~:
53 maybe BL.empty (U.toByteString) (U.fromString s16) @?= b16
54 ]
55 where b16 = BL.pack [1..16]
56 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
58test_v1 :: [Maybe U.UUID] -> H.Test
59test_v1 v1s = H.TestList [
60 "V1 unique" ~: nub (v1s \\ nub v1s) @?= [],
61 "V1 not null" ~: H.TestList $ map (testUUID (not . U.null)) v1s,
62 "V1 valid" ~: H.TestList $ map (testUUID (isValidVersion 1)) v1s
63 ]
64 where testUUID :: (U.UUID -> Bool) -> Maybe U.UUID -> H.Test
65 testUUID p u = maybe False p u ~? show u
67test_v3 :: H.Test
68test_v3 = H.TestList [
69 "V3 computation" ~:
70 U3.generateNamed U3.namespaceDNS name @?= uV3
71 ]
72 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
73 uV3 = fromJust $ U.fromString "3d813cbb-47fb-32ba-91df-831e1593ac29"
75test_v5 :: H.Test
76test_v5 = H.TestList [
77 "V5 computation" ~:
78 U5.generateNamed U5.namespaceDNS name @?= uV5
79 ]
80 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
81 uV5 = fromJust $ U.fromString "21f7f8de-8051-5b89-8680-0195ef798b6a"
83prop_stringRoundTrip :: Test
84prop_stringRoundTrip = testProperty "String round trip" stringRoundTrip
85 where stringRoundTrip :: U.UUID -> Bool
86 stringRoundTrip u = maybe False (== u) $ U.fromString (U.toString u)
88prop_byteStringRoundTrip :: Test
89prop_byteStringRoundTrip = testProperty "ByteString round trip" byteStringRoundTrip
90 where byteStringRoundTrip :: U.UUID -> Bool
91 byteStringRoundTrip u = maybe False (== u)
92 $ U.fromByteString (U.toByteString u)
94prop_stringLength :: Test
95prop_stringLength = testProperty "String length" stringLength
96 where stringLength :: U.UUID -> Bool
97 stringLength u = length (U.toString u) == 36
99prop_byteStringLength :: Test
100prop_byteStringLength = testProperty "ByteString length" byteStringLength
101 where byteStringLength :: U.UUID -> Bool
102 byteStringLength u = BL.length (U.toByteString u) == 16
104prop_randomsDiffer :: Test
105prop_randomsDiffer = testProperty "Randoms differ" randomsDiffer
106 where randomsDiffer :: (U.UUID, U.UUID) -> Bool
107 randomsDiffer (u1, u2) = u1 /= u2
109prop_randomNotNull :: Test
110prop_randomNotNull = testProperty "Random not null" randomNotNull
111 where randomNotNull :: U.UUID -> Bool
112 randomNotNull = not. U.null
114prop_randomsValid :: Test
115prop_randomsValid = testProperty "Random valid" randomsValid
116 where randomsValid :: U.UUID -> Bool
117 randomsValid = isValidVersion 4
119prop_v3NotNull :: Test
120prop_v3NotNull = testProperty "V3 not null" v3NotNull
121 where v3NotNull :: [Word8] -> Bool
122 v3NotNull = not . U.null . U3.generateNamed U3.namespaceDNS
124prop_v3Valid :: Test
125prop_v3Valid = testProperty "V3 valid" v3Valid
126 where v3Valid :: [Word8] -> Bool
127 v3Valid = isValidVersion 3 . U3.generateNamed U3.namespaceDNS
129prop_v5NotNull :: Test
130prop_v5NotNull = testProperty "V5 not null" v5NotNull
131 where v5NotNull :: [Word8] -> Bool
132 v5NotNull = not . U.null . U5.generateNamed U5.namespaceDNS
134prop_v5Valid :: Test
135prop_v5Valid = testProperty "V5 valid" v5Valid
136 where v5Valid :: [Word8] -> Bool
137 v5Valid = isValidVersion 5 . U5.generateNamed U5.namespaceDNS
139prop_readShowRoundTrip :: Test
140prop_readShowRoundTrip = testProperty "Read/Show round-trip" prop
141 where -- we're using 'Maybe UUID' to add a bit of
142 -- real-world complexity.
143 prop :: U.UUID -> Bool
144 prop uuid = read (show (Just uuid)) == Just uuid
146-- Mostly going to test for wrong UUIDs
147fromASCIIBytes_fromString1 :: String -> Bool
148fromASCIIBytes_fromString1 s =
149 if all (\c -> ord c < 256) s
150 then U.fromString s == U.fromASCIIBytes (BC8.pack s)
151 else True
153fromASCIIBytes_fromString2 :: U.UUID -> Bool
154fromASCIIBytes_fromString2 (U.toString -> s) =
155 U.fromString s == U.fromASCIIBytes (BC8.pack s)
157toASCIIBytes_toString :: U.UUID -> Bool
158toASCIIBytes_toString uuid =
159 U.toString uuid == BC8.unpack (U.toASCIIBytes uuid)
161fromASCIIBytes_toASCIIBytes :: U.UUID -> Bool
162fromASCIIBytes_toASCIIBytes (BC8.pack . U.toString -> bs) =
163 Just bs == (U.toASCIIBytes <$> U.fromASCIIBytes bs)
165toASCIIBytes_fromASCIIBytes :: U.UUID -> Bool
166toASCIIBytes_fromASCIIBytes uuid =
167 Just uuid == U.fromASCIIBytes (U.toASCIIBytes uuid)
169main :: IO ()
170main = do
171 v1s <- replicateM 100 U.nextUUID
172 defaultMain $
173 concat $
174 [ hUnitTestToTests $ H.TestList [
175 test_null,
176 test_nil,
177 test_conv,
178 test_v1 v1s,
179 test_v3,
180 test_v5
181 ]
182 , [ prop_stringRoundTrip,
183 prop_readShowRoundTrip,
184 prop_byteStringRoundTrip,
185 prop_stringLength,
186 prop_byteStringLength,
187 prop_randomsDiffer,
188 prop_randomNotNull,
189 prop_randomsValid,
190 prop_v3NotNull,
191 prop_v3Valid,
192 prop_v5NotNull,
193 prop_v5Valid
194 ]
195 , [ testProperty "fromASCIIBytes_fromString1" fromASCIIBytes_fromString1
196 , testProperty "fromASCIIBytes_fromString2" fromASCIIBytes_fromString2
197 , testProperty "fromASCIIBytes_toString" toASCIIBytes_toString
198 , testProperty "fromASCIIBytes_toASCIIBytes" fromASCIIBytes_toASCIIBytes
199 , testProperty "toASCIIBytes_fromASCIIBytes" toASCIIBytes_fromASCIIBytes
200 ]
201 ]