Skip to content

Commit 073d676

Browse files
committed
WIP: Tests.Properties.Substrings: add genOrdSubseq
1 parent 865ada4 commit 073d676

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/Tests/Properties/Substrings.hs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import qualified Data.Text.Internal.Lazy as TL (Text(..))
2020
import qualified Data.Text.Internal.Lazy.Fusion as SL
2121
import qualified Data.Text.Lazy as TL
2222
import qualified Tests.SlowFunctions as Slow
23+
import Control.Monad (replicateM)
24+
import Data.List (nub, sort)
2325

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

236+
-- | Generator for substrings that keeps the element order.
237+
-- Aka: "1234567890" -> "245680"
238+
genOrdSubseq :: T.Text -> Gen T.Text
239+
genOrdSubseq txt =
240+
T.pack . transform <$> genTransformMap
241+
where
242+
243+
pickN :: Gen Int
244+
pickN =
245+
choose (0, T.length txt)
246+
247+
pickNs :: Gen [Int]
248+
pickNs =
249+
fmap (sort . nub) $ (`replicateM` pickN) =<< pickN
250+
251+
growInst :: [Bool] -> Int -> [Bool]
252+
growInst ls n =
253+
ls
254+
<> take (length ls - pred n) [True ..]
255+
<> [False]
256+
257+
mkTransformInst :: [Bool] -> [Int] -> [Bool]
258+
mkTransformInst bls [] =
259+
bls
260+
<> take (T.length txt - length bls) [True ..]
261+
mkTransformInst bls (i:is) =
262+
mkTransformInst
263+
(growInst bls i)
264+
is
265+
266+
mkTransformMap :: [a] -> [Int] -> [(a, Bool)]
267+
mkTransformMap ls ixs =
268+
zip ls (mkTransformInst mempty ixs)
269+
270+
genTransformMap :: (Gen [(Char, Bool)])
271+
genTransformMap = fmap (mkTransformMap $ T.unpack txt) pickNs
272+
273+
transform :: [(Char, Bool)] -> [Char]
274+
transform =
275+
foldr (\ (c, b) as -> as <> if b then [c] else mempty) mempty
276+
234277
t_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` T.stripPrefix (packS s)
235278
tl_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` TL.stripPrefix (packS s)
236279

0 commit comments

Comments
 (0)