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

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

import Data.Hashable (Hashable)

import Test.QuickCheck

import qualified Data.HashMap.Lazy as HML
import qualified Data.HashSet      as HS

-------------------------------------------------------------------------------
-- unordered-containers
-------------------------------------------------------------------------------

instance (Hashable a, Eq a, Arbitrary a) => Arbitrary (HS.HashSet a) where
    arbitrary :: Gen (HashSet a)
arbitrary = [a] -> HashSet a
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HS.fromList ([a] -> HashSet a) -> Gen [a] -> Gen (HashSet a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen [a]
forall a. Arbitrary a => Gen a
arbitrary
    shrink :: HashSet a -> [HashSet a]
shrink HashSet a
hashset = [a] -> HashSet a
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HS.fromList ([a] -> HashSet a) -> [[a]] -> [HashSet a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> [[a]]
forall a. Arbitrary a => a -> [a]
shrink (HashSet a -> [a]
forall a. HashSet a -> [a]
HS.toList HashSet a
hashset)

instance CoArbitrary a => CoArbitrary (HS.HashSet a) where
    coarbitrary :: forall b. HashSet a -> Gen b -> Gen b
coarbitrary = [a] -> Gen b -> Gen b
forall b. [a] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([a] -> Gen b -> Gen b)
-> (HashSet a -> [a]) -> HashSet a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashSet a -> [a]
forall a. HashSet a -> [a]
HS.toList

instance (Hashable a, Eq a, Function a) => Function (HS.HashSet a) where
    function :: forall b. (HashSet a -> b) -> HashSet a :-> b
function = (HashSet a -> [a])
-> ([a] -> HashSet a) -> (HashSet a -> b) -> HashSet a :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap HashSet a -> [a]
forall a. HashSet a -> [a]
HS.toList [a] -> HashSet a
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HS.fromList

instance (Hashable k, Eq k, Arbitrary k) => Arbitrary1 (HML.HashMap k) where
    liftArbitrary :: forall a. Gen a -> Gen (HashMap k a)
liftArbitrary Gen a
arb =
        [(k, a)] -> HashMap k a
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HML.fromList ([(k, a)] -> HashMap k a) -> Gen [(k, a)] -> Gen (HashMap k a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (k, a) -> Gen [(k, a)]
forall a. Gen a -> Gen [a]
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary (Gen k -> Gen a -> Gen (k, a)
forall a b. Gen a -> Gen b -> Gen (a, b)
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 Gen k
forall a. Arbitrary a => Gen a
arbitrary Gen a
arb)
    liftShrink :: forall a. (a -> [a]) -> HashMap k a -> [HashMap k a]
liftShrink a -> [a]
shr HashMap k a
m =
        [(k, a)] -> HashMap k a
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HML.fromList ([(k, a)] -> HashMap k a) -> [[(k, a)]] -> [HashMap k a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((k, a) -> [(k, a)]) -> [(k, a)] -> [[(k, a)]]
forall a. (a -> [a]) -> [a] -> [[a]]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink ((k -> [k]) -> (a -> [a]) -> (k, a) -> [(k, a)]
forall a b. (a -> [a]) -> (b -> [b]) -> (a, b) -> [(a, b)]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 k -> [k]
forall a. Arbitrary a => a -> [a]
shrink a -> [a]
shr) (HashMap k a -> [(k, a)]
forall k v. HashMap k v -> [(k, v)]
HML.toList HashMap k a
m)

instance (Hashable k, Eq k, Arbitrary k, Arbitrary v) => Arbitrary (HML.HashMap k v) where
    arbitrary :: Gen (HashMap k v)
arbitrary = Gen (HashMap k v)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
    shrink :: HashMap k v -> [HashMap k v]
shrink = HashMap k v -> [HashMap k v]
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

instance (CoArbitrary k, CoArbitrary v) => CoArbitrary (HML.HashMap k v) where
    coarbitrary :: forall b. HashMap k v -> Gen b -> Gen b
coarbitrary = [(k, v)] -> Gen b -> Gen b
forall b. [(k, v)] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([(k, v)] -> Gen b -> Gen b)
-> (HashMap k v -> [(k, v)]) -> HashMap k v -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap k v -> [(k, v)]
forall k v. HashMap k v -> [(k, v)]
HML.toList

instance (Hashable k, Eq k, Function k, Function v) => Function (HML.HashMap k v) where
    function :: forall b. (HashMap k v -> b) -> HashMap k v :-> b
function = (HashMap k v -> [(k, v)])
-> ([(k, v)] -> HashMap k v)
-> (HashMap k v -> b)
-> HashMap k v :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap HashMap k v -> [(k, v)]
forall k v. HashMap k v -> [(k, v)]
HML.toList [(k, v)] -> HashMap k v
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HML.fromList