{-# LANGUAGE CPP              #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Test.QuickCheck.Instances.UUID () where

import Prelude ()
import Test.QuickCheck.Instances.CustomPrelude

import Data.Word (Word64)

import Test.QuickCheck
import Test.QuickCheck.Gen (chooseUpTo)

import qualified Data.UUID.Types as UUID

-------------------------------------------------------------------------------
-- uuid
-------------------------------------------------------------------------------

uuidFromWords64 :: (Word64, Word64) -> UUID.UUID
uuidFromWords64 :: (Word64, Word64) -> UUID
uuidFromWords64 (Word64
a,Word64
b) = Word64 -> Word64 -> UUID
UUID.fromWords64 Word64
a Word64
b

uniformWord64 :: Gen Word64
uniformWord64 :: Gen Word64
uniformWord64 = Word64 -> Gen Word64
chooseUpTo Word64
forall a. Bounded a => a
maxBound

-- | Uniform distribution.
instance Arbitrary UUID.UUID where
    arbitrary :: Gen UUID
arbitrary = Word64 -> Word64 -> UUID
UUID.fromWords64 (Word64 -> Word64 -> UUID) -> Gen Word64 -> Gen (Word64 -> UUID)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Word64
uniformWord64 Gen (Word64 -> UUID) -> Gen Word64 -> Gen UUID
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Word64
uniformWord64
    shrink :: UUID -> [UUID]
shrink = ((Word64, Word64) -> UUID) -> [(Word64, Word64)] -> [UUID]
forall a b. (a -> b) -> [a] -> [b]
map (Word64, Word64) -> UUID
uuidFromWords64 ([(Word64, Word64)] -> [UUID])
-> (UUID -> [(Word64, Word64)]) -> UUID -> [UUID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word64, Word64) -> [(Word64, Word64)]
forall a. Arbitrary a => a -> [a]
shrink ((Word64, Word64) -> [(Word64, Word64)])
-> (UUID -> (Word64, Word64)) -> UUID -> [(Word64, Word64)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> (Word64, Word64)
UUID.toWords64

instance CoArbitrary UUID.UUID where
    coarbitrary :: forall b. UUID -> Gen b -> Gen b
coarbitrary = (Word32, Word32, Word32, Word32) -> Gen b -> Gen b
forall b. (Word32, Word32, Word32, Word32) -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ((Word32, Word32, Word32, Word32) -> Gen b -> Gen b)
-> (UUID -> (Word32, Word32, Word32, Word32))
-> UUID
-> Gen b
-> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> (Word32, Word32, Word32, Word32)
UUID.toWords

instance Function UUID.UUID where
    function :: forall b. (UUID -> b) -> UUID :-> b
function = (UUID -> (Word64, Word64))
-> ((Word64, Word64) -> UUID) -> (UUID -> b) -> UUID :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap UUID -> (Word64, Word64)
UUID.toWords64 (Word64, Word64) -> UUID
uuidFromWords64