Skip to content

Commit

Permalink
WIP: Tests.Properties.Substrings: add genOrdSubseq
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Latukha committed Mar 2, 2022
1 parent 865ada4 commit 073d676
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/Tests/Properties/Substrings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import qualified Data.Text.Internal.Lazy as TL (Text(..))
import qualified Data.Text.Internal.Lazy.Fusion as SL
import qualified Data.Text.Lazy as TL
import qualified Tests.SlowFunctions as Slow
import Control.Monad (replicateM)
import Data.List (nub, sort)

s_take n = L.take n `eqP` (unpackS . S.take n)
s_take_s (Small n) = L.take n `eqP` (unpackS . S.unstream . S.take n)
Expand Down Expand Up @@ -231,6 +233,47 @@ tl_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packS s)
t_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packS s)
tl_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packS s)

-- | Generator for substrings that keeps the element order.
-- Aka: "1234567890" -> "245680"
genOrdSubseq :: T.Text -> Gen T.Text
genOrdSubseq txt =
T.pack . transform <$> genTransformMap
where

pickN :: Gen Int
pickN =
choose (0, T.length txt)

pickNs :: Gen [Int]
pickNs =
fmap (sort . nub) $ (`replicateM` pickN) =<< pickN

growInst :: [Bool] -> Int -> [Bool]
growInst ls n =
ls
<> take (length ls - pred n) [True ..]
<> [False]

mkTransformInst :: [Bool] -> [Int] -> [Bool]
mkTransformInst bls [] =
bls
<> take (T.length txt - length bls) [True ..]
mkTransformInst bls (i:is) =
mkTransformInst
(growInst bls i)
is

mkTransformMap :: [a] -> [Int] -> [(a, Bool)]
mkTransformMap ls ixs =
zip ls (mkTransformInst mempty ixs)

genTransformMap :: (Gen [(Char, Bool)])
genTransformMap = fmap (mkTransformMap $ T.unpack txt) pickNs

transform :: [(Char, Bool)] -> [Char]
transform =
foldr (\ (c, b) as -> as <> if b then [c] else mempty) mempty

t_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` T.stripPrefix (packS s)
tl_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` TL.stripPrefix (packS s)

Expand Down

0 comments on commit 073d676

Please sign in to comment.