on master help sign in
unit tests for V1 UUIDs
commita12d7bd82785ec3e6a6de69ad21d9191346d1ab3 authorMark Lentczner <markl@glyphic.com> dateMon 28 Dec 2009, 06:10 (17 years ago) parente219414
2 files changed+17−4
Data/UUID/V1.hs+4−4 tests/TestUUID.hs+13
Data/UUID/V1.hs+4−4
@@ -55,9 +55,9 @@ nextUUID = do
5555word64ToTimePieces :: Word64 -> (Word32, Word16, Word16)
5656word64ToTimePieces w = (lo, mi, hi)
5757 where
58− lo = fromIntegral $ (w `shiftL` 32) `shiftR` 32
59− mi = fromIntegral $ (w `shiftL` 16) `shiftR` 48
60− hi = (fromIntegral $ w `shiftR` 48) `setBit` 15
58+ lo = fromIntegral $ (w)
59+ mi = fromIntegral $ (w `shiftR` 32)
60+ hi = fromIntegral $ (w `shiftR` 48 .&. 0x0fff .|. 0x1000)
6161
6262
6363stepTime = do
@@ -66,7 +66,7 @@ stepTime = do
6666 if h1 > h0
6767 then do
6868 writeIORef state $ State 0 h1
69− return $ Just (0, h1)
69+ return $ Just (c0, h1)
7070 else do
7171 let
7272 c1 = succ c0
tests/TestUUID.hs+13
@@ -1,9 +1,12 @@
1+import Control.Monad (replicateM)
12import Data.Bits
23import qualified Data.ByteString.Lazy as B
34import Data.Char (ord)
5+import Data.List (nub, (\\))
46import Data.Maybe
57import Data.Word
68import qualified Data.UUID as U
9+import qualified Data.UUID.V1 as U
710import qualified Data.UUID.V5 as U
811import Test.HUnit
912import Test.QuickCheck
@@ -47,6 +50,14 @@ test_conv = TestList [
4750 where b16 = B.pack [1..16]
4851 s16 = "01020304-0506-0708-090a-0b0c0d0e0f10"
4952
53+test_v1 v1s = TestList [
54+ "V1 unique" ~: nub (v1s \\ nub v1s) @?= [],
55+ "V1 not null" ~: TestList $ map (testUUID (not . U.null)) v1s,
56+ "V1 valid" ~: TestList $ map (testUUID (isValidVersion 1)) v1s
57+ ]
58+ where testUUID :: (U.UUID -> Bool) -> Maybe U.UUID -> Test
59+ testUUID p u = maybe False p u ~? show u
60+
5061test_v5 = TestList [
5162 "V5 computation" ~:
5263 U.generateNamed U.namespaceDNS name @?= uV5
@@ -100,10 +111,12 @@ prop_v5Valid = label "V5 valid" v5Valid
100111
101112main :: IO ()
102113main = do
114+ v1s <- replicateM 100 U.nextUUID
103115 runTestText (putTextToHandle stderr False) (TestList [
104116 test_null,
105117 test_nil,
106118 test_conv,
119+ test_v1 v1s,
107120 test_v5
108121 ])
109122 mapM_ quickCheck $ [