module Data.CQRS.Test.Internal.Utils
( chunkRandomly
, chunkSized
, chooseRandom
, randomByteString
) where
import Control.Monad.IO.Class (liftIO)
import Control.Monad (replicateM)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.List (sort)
import System.Random (randomR, randomRIO, getStdRandom, random)
chunkRandomly :: Int -> [a] -> IO [[a]]
chunkRandomly n xs = do
let lengthXs = length xs
splitIndices <- fmap sort $ replicateM (n 1) $ getStdRandom $ randomR (0, lengthXs 1)
return $ map chunk $ pairs $ [0] ++ splitIndices ++ [lengthXs]
where
pairs is = zip is (tail is)
chunk (i, j) = take (j i) $ drop i xs
chunkSized :: Int -> [a] -> IO [[a]]
chunkSized avgSize xs = do
n <- liftIO $ getStdRandom $ randomR (0, (length xs 1) `div` avgSize)
liftIO $ chunkRandomly n xs
randomByteString :: Int -> IO ByteString
randomByteString n = do
w8 <- replicateM n $ getStdRandom random
return $ B.pack w8
chooseRandom :: [a] -> IO a
chooseRandom xs = do
i <- randomRIO (0, length xs 1)
return $ xs !! i