module Test.Cardano.Data (
  expectValidMap,
  genNonEmptyMap,
) where

import Control.Monad
import qualified Data.Map.Internal.Debug as Map
import qualified Data.Map.Strict as Map hiding (showTree)
import Test.Hspec
import Test.QuickCheck

expectValidMap :: HasCallStack => (Ord k, Show k, Show a) => Map.Map k a -> Expectation
expectValidMap :: forall k a.
(HasCallStack, Ord k, Show k, Show a) =>
Map k a -> Expectation
expectValidMap Map k a
m =
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall k a. Ord k => Map k a -> Bool
Map.valid Map k a
m) forall a b. (a -> b) -> a -> b
$
    HasCallStack => String -> Expectation
expectationFailure forall a b. (a -> b) -> a -> b
$
      [String] -> String
unlines
        [ String
"Interal strucutre of a map is invalid:"
        , String
"Keys are ordered: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall k a. Ord k => Map k a -> Bool
Map.ordered Map k a
m)
        , String
"Tree is balanced: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall k a. Map k a -> Bool
Map.balanced Map k a
m)
        , String
"Sizes are valid: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall k a. Map k a -> Bool
Map.validsize Map k a
m)
        , forall k a. (Show k, Show a) => Map k a -> String
Map.showTree Map k a
m
        ]

genNonEmptyMap :: Ord k => Gen k -> Gen v -> Gen (Map.Map k v)
genNonEmptyMap :: forall k v. Ord k => Gen k -> Gen v -> Gen (Map k v)
genNonEmptyMap Gen k
genKey Gen v
genVal = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Gen a -> Gen [a]
listOf1 ((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen k
genKey forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen v
genVal)