-
Notifications
You must be signed in to change notification settings - Fork 1
/
SegTreeSpec.hs
53 lines (43 loc) · 1.64 KB
/
SegTreeSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module SegTreeSpec where
import Data.Array
import Data.Foldable
import Data.Monoid
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
import SegTree ( adjustST, emptyST, foldRangeST, fromListST )
import Util ( genPossiblyEmptyRange )
spec :: Spec
spec = do
prop "many adjustST then foldRangeST" $
forAll genBounds $ \bnds ->
forAll (pointUpds bnds) $ \ivs -> do
let st = adjustMany (emptyST bnds) ivs
forAll (rangeQry bnds) $ \(i, j) ->
foldRangeST i j st `shouldBe` naive ivs i j
prop "many adjustST then foldrST" $
forAll genBounds $ \bnds ->
forAll (pointUpds bnds) $ \ivs -> do
let st = adjustMany (emptyST bnds) ivs
xs = elems $ accumArray (<>) mempty bnds ivs
toList st `shouldBe` xs
prop "fromListST is same as many adjustST" $
forAll genBounds $ \bnds@(l,h) ->
forAll (vector (h-l+1)) $ \xs -> do
let st = fromListST bnds xs
st' = adjustMany (emptyST bnds) (zip [l..] xs)
toList st `shouldBe` toList st'
where
naive ivs i j = fold [v | (k, v) <- ivs, i <= k && k <= j]
adjustMany = foldl' (\st (i, v) -> adjustST (v <>) i st)
genBounds :: Gen (Int, Int)
genBounds = sized $ \n' -> do
n <- choose (0, n')
l <- arbitrary
pure (l, l+n-1)
pointUpds :: (Int, Int) -> Gen [(Int, Sum Int)]
pointUpds (l, h)
| l == h + 1 = pure []
| otherwise = listOf $ (,) <$> choose (l,h) <*> arbitrary
rangeQry :: (Int, Int) -> Gen (Int, Int)
rangeQry (l, r) = genPossiblyEmptyRange (l - 10, r + 10)