on e219414 help sign in
typeable/uuid/tests/TestUUID.hs
119 lines 3.9 kB history raw
1import Data.Bits
2import qualified Data.ByteString.Lazy as B
3import Data.Char (ord)
4import Data.Maybe
5import Data.Word
6import qualified Data.UUID as U
7import qualified Data.UUID.V5 as U
8import Test.HUnit
9import Test.QuickCheck
10import System.IO
11import System.Random
14isValidVersion :: Int -> U.UUID -> Bool
15isValidVersion v u = lenOK && variantOK && versionOK
16 where bs = U.toByteString u
17 lenOK = B.length bs == 16
18 variantOK = (B.index bs 8) .&. 0xc0 == 0x80
19 versionOK = (B.index bs 6) .&. 0xf0 == fromIntegral (v `shiftL` 4)
22instance Arbitrary U.UUID where
23 arbitrary = (fst . random) `fmap` rand
24 coarbitrary = undefined
26instance Arbitrary Word8 where
27 arbitrary = (fromIntegral . fst . randomR (0,255::Int)) `fmap` rand
28 coarbitrary = undefined
31test_null = TestList [
32 "nil is null" ~: assertBool "" (U.null U.nil),
33 "namespaceDNS is not null" ~: assertBool "" (not $ U.null U.namespaceDNS)
34 ]
36test_nil = TestList [
37 "nil string" ~: U.toString U.nil @?= "00000000-0000-0000-0000-000000000000",
38 "nil bytes" ~: U.toByteString U.nil @?= B.pack (replicate 16 0)
39 ]
41test_conv = TestList [
42 "conv bytes to string" ~:
43 maybe "" (U.toString) (U.fromByteString b16) @?= s16,
44 "conv string to bytes" ~:
45 maybe B.empty (U.toByteString) (U.fromString s16) @?= b16
46 ]
47 where b16 = B.pack [1..16]
48 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
50test_v5 = TestList [
51 "V5 computation" ~:
52 U.generateNamed U.namespaceDNS name @?= uV5
53 ]
54 where name = map (fromIntegral . ord) "www.widgets.com" :: [Word8]
55 -- uV3 = fromJust $ U.fromString "3d813cbb-47fb-32ba-91df-831e1593ac29"
56 uV5 = fromJust $ U.fromString "21f7f8de-8051-5b89-8680-0195ef798b6a"
58prop_stringRoundTrip = label "String round trip" stringRoundTrip
59 where stringRoundTrip :: U.UUID -> Bool
60 stringRoundTrip u = maybe False (== u) $ U.fromString (U.toString u)
62prop_byteStringRoundTrip = label "ByteString round trip" byteStringRoundTrip
63 where byteStringRoundTrip :: U.UUID -> Bool
64 byteStringRoundTrip u = maybe False (== u)
65 $ U.fromByteString (U.toByteString u)
67prop_stringLength = label "String length" stringLength
68 where stringLength :: U.UUID -> Bool
69 stringLength u = length (U.toString u) == 36
71prop_byteStringLength = label "ByteString length" byteStringLength
72 where byteStringLength :: U.UUID -> Bool
73 byteStringLength u = B.length (U.toByteString u) == 16
75prop_randomsDiffer :: Property
76prop_randomsDiffer = label "Randoms differ" randomsDiffer
77 where randomsDiffer :: (U.UUID, U.UUID) -> Bool
78 randomsDiffer (u1, u2) = u1 /= u2
80prop_randomNotNull :: Property
81prop_randomNotNull = label "Random not null" randomNotNull
82 where randomNotNull :: U.UUID -> Bool
83 randomNotNull = not. U.null
85prop_randomsValid :: Property
86prop_randomsValid = label "Random valid" randomsValid
87 where randomsValid :: U.UUID -> Bool
88 randomsValid = isValidVersion 4
90prop_v5NotNull :: Property
91prop_v5NotNull = label "V5 not null" v5NotNull
92 where v5NotNull :: [Word8] -> Bool
93 v5NotNull = not . U.null . U.generateNamed U.namespaceDNS
95prop_v5Valid :: Property
96prop_v5Valid = label "V5 valid" v5Valid
97 where v5Valid :: [Word8] -> Bool
98 v5Valid = isValidVersion 5 . U.generateNamed U.namespaceDNS
101main :: IO ()
102main = do
103 runTestText (putTextToHandle stderr False) (TestList [
104 test_null,
105 test_nil,
106 test_conv,
107 test_v5
108 ])
109 mapM_ quickCheck $ [
110 prop_stringRoundTrip,
111 prop_byteStringRoundTrip,
112 prop_stringLength,
113 prop_byteStringLength,
114 prop_randomsDiffer,
115 prop_randomNotNull,
116 prop_randomsValid,
117 prop_v5NotNull,
118 prop_v5Valid
119 ]