on 7e2d0cd help sign in
typeable/uuid/tests/TestUUID.hs
202 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)
21import System.IO
24isValidVersion :: Int -> U.UUID -> Bool
25isValidVersion v u = lenOK && variantOK && versionOK
26 where bs = U.toByteString u
27 lenOK = BL.length bs == 16
28 variantOK = (BL.index bs 8) .&. 0xc0 == 0x80
29 versionOK = (BL.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
32instance Arbitrary U.UUID where
33 -- the UUID random instance ignores bounds
34 arbitrary = choose (U.nil, U.nil)
37test_null :: H.Test
38test_null = H.TestList [
39 "nil is null" ~: assertBool "" (U.null U.nil),
40 "namespaceDNS is not null" ~: assertBool "" (not $ U.null U3.namespaceDNS)
41 ]
43test_nil :: H.Test
44test_nil = H.TestList [
45 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
46 "nil bytes" ~: U.toByteString U.nil @?= BL.pack (replicate 16 0)
47 ]
49test_conv :: H.Test
50test_conv = H.TestList [
51 "conv bytes to string" ~:
52 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
53 "conv string to bytes" ~:
54 maybe BL.empty (U.toByteString) (U.fromString s16) @?= b16
55 ]
56 where b16 = BL.pack [1..16]
57 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
59test_v1 :: [Maybe U.UUID] -> H.Test
60test_v1 v1s = H.TestList [
61 "V1 unique" ~: nub (v1s \\ nub v1s) @?= [],
62 "V1 not null" ~: H.TestList $ map (testUUID (not . U.null)) v1s,
63 "V1 valid" ~: H.TestList $ map (testUUID (isValidVersion 1)) v1s
64 ]
65 where testUUID :: (U.UUID -> Bool) -> Maybe U.UUID -> H.Test
66 testUUID p u = maybe False p u ~? show u
68test_v3 :: H.Test
69test_v3 = H.TestList [
70 "V3 computation" ~:
71 U3.generateNamed U3.namespaceDNS name @?= uV3
72 ]
73 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
74 uV3 = fromJust $ U.fromString "3d813cbb-47fb-32ba-91df-831e1593ac29"
76test_v5 :: H.Test
77test_v5 = H.TestList [
78 "V5 computation" ~:
79 U5.generateNamed U5.namespaceDNS name @?= uV5
80 ]
81 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
82 uV5 = fromJust $ U.fromString "21f7f8de-8051-5b89-8680-0195ef798b6a"
84prop_stringRoundTrip :: Test
85prop_stringRoundTrip = testProperty "String round trip" stringRoundTrip
86 where stringRoundTrip :: U.UUID -> Bool
87 stringRoundTrip u = maybe False (== u) $ U.fromString (U.toString u)
89prop_byteStringRoundTrip :: Test
90prop_byteStringRoundTrip = testProperty "ByteString round trip" byteStringRoundTrip
91 where byteStringRoundTrip :: U.UUID -> Bool
92 byteStringRoundTrip u = maybe False (== u)
93 $ U.fromByteString (U.toByteString u)
95prop_stringLength :: Test
96prop_stringLength = testProperty "String length" stringLength
97 where stringLength :: U.UUID -> Bool
98 stringLength u = length (U.toString u) == 36
100prop_byteStringLength :: Test
101prop_byteStringLength = testProperty "ByteString length" byteStringLength
102 where byteStringLength :: U.UUID -> Bool
103 byteStringLength u = BL.length (U.toByteString u) == 16
105prop_randomsDiffer :: Test
106prop_randomsDiffer = testProperty "Randoms differ" randomsDiffer
107 where randomsDiffer :: (U.UUID, U.UUID) -> Bool
108 randomsDiffer (u1, u2) = u1 /= u2
110prop_randomNotNull :: Test
111prop_randomNotNull = testProperty "Random not null" randomNotNull
112 where randomNotNull :: U.UUID -> Bool
113 randomNotNull = not. U.null
115prop_randomsValid :: Test
116prop_randomsValid = testProperty "Random valid" randomsValid
117 where randomsValid :: U.UUID -> Bool
118 randomsValid = isValidVersion 4
120prop_v3NotNull :: Test
121prop_v3NotNull = testProperty "V3 not null" v3NotNull
122 where v3NotNull :: [Word8] -> Bool
123 v3NotNull = not . U.null . U3.generateNamed U3.namespaceDNS
125prop_v3Valid :: Test
126prop_v3Valid = testProperty "V3 valid" v3Valid
127 where v3Valid :: [Word8] -> Bool
128 v3Valid = isValidVersion 3 . U3.generateNamed U3.namespaceDNS
130prop_v5NotNull :: Test
131prop_v5NotNull = testProperty "V5 not null" v5NotNull
132 where v5NotNull :: [Word8] -> Bool
133 v5NotNull = not . U.null . U5.generateNamed U5.namespaceDNS
135prop_v5Valid :: Test
136prop_v5Valid = testProperty "V5 valid" v5Valid
137 where v5Valid :: [Word8] -> Bool
138 v5Valid = isValidVersion 5 . U5.generateNamed U5.namespaceDNS
140prop_readShowRoundTrip :: Test
141prop_readShowRoundTrip = testProperty "Read/Show round-trip" prop
142 where -- we're using 'Maybe UUID' to add a bit of
143 -- real-world complexity.
144 prop :: U.UUID -> Bool
145 prop uuid = read (show (Just uuid)) == Just uuid
147-- Mostly going to test for wrong UUIDs
148fromASCIIBytes_fromString1 :: String -> Bool
149fromASCIIBytes_fromString1 s =
150 if all (\c -> ord c < 256) s
151 then U.fromString s == U.fromASCIIBytes (BC8.pack s)
152 else True
154fromASCIIBytes_fromString2 :: U.UUID -> Bool
155fromASCIIBytes_fromString2 (U.toString -> s) =
156 U.fromString s == U.fromASCIIBytes (BC8.pack s)
158toASCIIBytes_toString :: U.UUID -> Bool
159toASCIIBytes_toString uuid =
160 U.toString uuid == BC8.unpack (U.toASCIIBytes uuid)
162fromASCIIBytes_toASCIIBytes :: U.UUID -> Bool
163fromASCIIBytes_toASCIIBytes (BC8.pack . U.toString -> bs) =
164 Just bs == (U.toASCIIBytes <$> U.fromASCIIBytes bs)
166toASCIIBytes_fromASCIIBytes :: U.UUID -> Bool
167toASCIIBytes_fromASCIIBytes uuid =
168 Just uuid == U.fromASCIIBytes (U.toASCIIBytes uuid)
170main :: IO ()
171main = do
172 v1s <- replicateM 100 U.nextUUID
173 defaultMain $
174 concat $
175 [ hUnitTestToTests $ H.TestList [
176 test_null,
177 test_nil,
178 test_conv,
179 test_v1 v1s,
180 test_v3,
181 test_v5
182 ]
183 , [ prop_stringRoundTrip,
184 prop_readShowRoundTrip,
185 prop_byteStringRoundTrip,
186 prop_stringLength,
187 prop_byteStringLength,
188 prop_randomsDiffer,
189 prop_randomNotNull,
190 prop_randomsValid,
191 prop_v3NotNull,
192 prop_v3Valid,
193 prop_v5NotNull,
194 prop_v5Valid
195 ]
196 , [ testProperty "fromASCIIBytes_fromString1" fromASCIIBytes_fromString1
197 , testProperty "fromASCIIBytes_fromString2" fromASCIIBytes_fromString2
198 , testProperty "fromASCIIBytes_toString" toASCIIBytes_toString
199 , testProperty "fromASCIIBytes_toASCIIBytes" fromASCIIBytes_toASCIIBytes
200 , testProperty "toASCIIBytes_fromASCIIBytes" toASCIIBytes_fromASCIIBytes
201 ]
202 ]