@@ -2,6 +2,7 @@
3{-# OPTIONS_GHC -fno-warn-orphans #-}
3{-# OPTIONS_GHC -fno-warn-orphans #-}
5+import Data.Binary (encode)
5import qualified Data.ByteString.Char8 as BC8
6import qualified Data.ByteString.Char8 as BC8
6import qualified Data.ByteString.Lazy as BL
7import qualified Data.ByteString.Lazy as BL
7import qualified Data.ByteString.Lazy.Char8 as BL8
8import qualified Data.ByteString.Lazy.Char8 as BL8
@@ -9,7 +10,8 @@ import Data.Char (ord)
9import Data.Functor ((<$>))
10import Data.Functor ((<$>))
10import qualified Data.UUID.Types as U
11import qualified Data.UUID.Types as U
11import Data.Word
12import Data.Word
12−import Foreign (alloca, peek, poke)
13+import Foreign (alloca, castPtr, peek, poke,
13import System.IO.Unsafe (unsafePerformIO)
15import System.IO.Unsafe (unsafePerformIO)
15import Test.QuickCheck (Arbitrary (arbitrary), choose)
17import Test.QuickCheck (Arbitrary (arbitrary), choose)
@@ -18,6 +20,8 @@ import Test.Tasty (TestTree, defaultMain, testGroup)
18import Test.Tasty.HUnit (assertBool, testCase, (@=?), (@?=))
20import Test.Tasty.HUnit (assertBool, testCase, (@=?), (@?=))
19import Test.Tasty.QuickCheck (testProperty)
21import Test.Tasty.QuickCheck (testProperty)
22instance Arbitrary U.UUID where
26instance Arbitrary U.UUID where
23 -- the UUID random instance ignores bounds
27 -- the UUID random instance ignores bounds
@@ -51,13 +55,32 @@ test_fromByteString :: Test
51test_fromByteString =
55test_fromByteString =
52 testCase "UUID fromByteString" $
56 testCase "UUID fromByteString" $
53 Just inputUUID @=?
57 Just inputUUID @=?
54− U.fromByteString (BL8.pack "\165\202\133f\217\197H5\153\200\225\241>s\181\226")
58+ U.fromByteString (BL8.pack "\xa5\xca\x85\x66\xd9\xc5\x48\x35\x99\xc8\xe1\xf1\x3e\x73\xb5\xe2")
56-- | Test fromWords with a fixed-input
60-- | Test fromWords with a fixed-input
57test_fromWords :: Test
61test_fromWords :: Test
58test_fromWords =
62test_fromWords =
59 testCase "UUID fromWords" $
63 testCase "UUID fromWords" $
60− inputUUID @=? U.fromWords 2781513062 3653584949 2580079089 1047770594
64+ inputUUID @=? U.fromWords 0xa5ca8566 0xd9c54835 0x99c8e1f1 0x3e73b5e2
67+test_Storeable :: Test
69+ testCase "UUID Storeable(poke)" $
70+ case targetByteOrder of
71+ LittleEndian -> BC8.pack "\x66\x85\xca\xa5\xc5\xd9\x35\x48\x99\xc8\xe1\xf1\x3e\x73\xb5\xe2" @=? pokeAsBS inputUUID
72+ BigEndian -> BC8.pack "\xa5\xca\x85\x66\xd9\xc5\x48\x35\x99\xc8\xe1\xf1\x3e\x73\xb5\xe2" @=? pokeAsBS inputUUID
74+ pokeAsBS :: U.UUID -> BC8.ByteString
75+ pokeAsBS uuid = unsafePerformIO $ do
78+ BC8.packCStringLen (castPtr ptr, sizeOf uuid)
82+ testCase "UUID Binary(encode)" $
83+ (BL8.pack "\xa5\xca\x85\x66\xd9\xc5\x48\x35\x99\xc8\xe1\xf1\x3e\x73\xb5\xe2") @=? encode inputUUID
62inputUUID :: U.UUID
85inputUUID :: U.UUID
63inputUUID = read "a5ca8566-d9c5-4835-99c8-e1f13e73b5e2"
86inputUUID = read "a5ca8566-d9c5-4835-99c8-e1f13e73b5e2"
@@ -155,7 +178,9 @@ main = do
155 test_nil,
178 test_nil,
156 test_conv,
179 test_conv,
157 test_fromByteString,
180 test_fromByteString,
158− test_fromWords
181+ test_fromWords,
160 , [ prop_stringRoundTrip,
185 , [ prop_stringRoundTrip,
161 prop_readShowRoundTrip,
186 prop_readShowRoundTrip,