Skip to content

Commit 26bfefc

Browse files
committed
Put generator and shrinker test utilities in their own module
Just a small preparatory refactoring, since we'll be importing these test utilities in following commits
1 parent e256e9b commit 26bfefc

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

lsm-tree.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ test-suite lsm-tree-test
384384
Test.Database.LSMTree.StateMachine.Op
385385
Test.Database.LSMTree.UnitTests
386386
Test.System.Posix.Fcntl.NoCache
387+
Test.Util.Arbitrary
387388
Test.Util.FS
388389
Test.Util.Orphans
389390
Test.Util.PrettyProxy

test/Test/Database/LSMTree/Generators.hs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33

44
module Test.Database.LSMTree.Generators (
55
tests
6-
, prop_arbitraryAndShrinkPreserveInvariant
7-
, prop_forAllArbitraryAndShrinkPreserveInvariant
8-
, deepseqInvariant
96
) where
107

11-
import Control.DeepSeq (NFData, deepseq)
128
import Data.Bifoldable (bifoldMap)
139
import Data.Coerce (coerce)
1410
import qualified Data.Map.Strict as Map
@@ -26,9 +22,10 @@ import Database.LSMTree.Internal.RawBytes (RawBytes (..))
2622
import Database.LSMTree.Internal.Serialise
2723

2824
import qualified Test.QuickCheck as QC
29-
import Test.QuickCheck (Arbitrary (..), Gen, Property, Testable (..))
25+
import Test.QuickCheck (Property)
3026
import Test.Tasty (TestTree, localOption, testGroup)
3127
import Test.Tasty.QuickCheck (QuickCheckMaxSize (..), testProperty)
28+
import Test.Util.Arbitrary
3229

3330
tests :: TestTree
3431
tests = testGroup "Test.Database.LSMTree.Generators" [
@@ -54,27 +51,6 @@ tests = testGroup "Test.Database.LSMTree.Generators" [
5451
]
5552
]
5653

57-
prop_arbitraryAndShrinkPreserveInvariant ::
58-
forall a. (Arbitrary a, Show a) => (a -> Bool) -> [TestTree]
59-
prop_arbitraryAndShrinkPreserveInvariant =
60-
prop_forAllArbitraryAndShrinkPreserveInvariant arbitrary shrink
61-
62-
prop_forAllArbitraryAndShrinkPreserveInvariant ::
63-
forall a. Show a => Gen a -> (a -> [a]) -> (a -> Bool) -> [TestTree]
64-
prop_forAllArbitraryAndShrinkPreserveInvariant gen shr inv =
65-
[ testProperty "Arbitrary satisfies invariant" $
66-
property $ QC.forAllShrink gen shr inv
67-
, testProperty "Shrinking satisfies invariant" $
68-
property $ QC.forAll gen $ \x ->
69-
case shr x of
70-
[] -> QC.label "no shrinks" $ property True
71-
xs -> QC.forAll (QC.growingElements xs) inv
72-
]
73-
74-
-- | Trivial invariant, but checks that the value is finite
75-
deepseqInvariant :: NFData a => a -> Bool
76-
deepseqInvariant x = x `deepseq` True
77-
7854
prop_packRawBytesPinnedOrUnpinned :: Bool -> [Word8] -> Bool
7955
prop_packRawBytesPinnedOrUnpinned pinned ws =
8056
packRawBytesPinnedOrUnpinned pinned ws == RawBytes (VP.fromList ws)

test/Test/Database/LSMTree/Internal/Lookup.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ import qualified System.FS.API as FS
6666
import System.FS.API (Handle (..), mkFsPath)
6767
import qualified System.FS.BlockIO.API as FS
6868
import System.FS.BlockIO.API
69-
import Test.Database.LSMTree.Generators (deepseqInvariant,
70-
prop_arbitraryAndShrinkPreserveInvariant,
71-
prop_forAllArbitraryAndShrinkPreserveInvariant)
7269
import Test.QuickCheck
7370
import Test.Tasty
7471
import Test.Tasty.QuickCheck
72+
import Test.Util.Arbitrary (deepseqInvariant,
73+
prop_arbitraryAndShrinkPreserveInvariant,
74+
prop_forAllArbitraryAndShrinkPreserveInvariant)
7575
import Test.Util.FS (withTempIOHasBlockIO)
7676

7777
tests :: TestTree

test/Test/Util/Arbitrary.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
2+
{-# LANGUAGE TypeApplications #-}
3+
4+
module Test.Util.Arbitrary (
5+
prop_arbitraryAndShrinkPreserveInvariant
6+
, prop_forAllArbitraryAndShrinkPreserveInvariant
7+
, deepseqInvariant
8+
) where
9+
10+
import Control.DeepSeq (NFData, deepseq)
11+
import Test.QuickCheck
12+
import Test.Tasty (TestTree)
13+
import Test.Tasty.QuickCheck (testProperty)
14+
15+
prop_arbitraryAndShrinkPreserveInvariant ::
16+
forall a. (Arbitrary a, Show a) => (a -> Bool) -> [TestTree]
17+
prop_arbitraryAndShrinkPreserveInvariant =
18+
prop_forAllArbitraryAndShrinkPreserveInvariant arbitrary shrink
19+
20+
prop_forAllArbitraryAndShrinkPreserveInvariant ::
21+
forall a. Show a => Gen a -> (a -> [a]) -> (a -> Bool) -> [TestTree]
22+
prop_forAllArbitraryAndShrinkPreserveInvariant gen shr inv =
23+
[ testProperty "Arbitrary satisfies invariant" $
24+
property $ forAllShrink gen shr inv
25+
, testProperty "Shrinking satisfies invariant" $
26+
property $ forAll gen $ \x ->
27+
case shr x of
28+
[] -> label "no shrinks" $ property True
29+
xs -> forAll (growingElements xs) inv
30+
]
31+
32+
-- | Trivial invariant, but checks that the value is finite
33+
deepseqInvariant :: NFData a => a -> Bool
34+
deepseqInvariant x = x `deepseq` True

0 commit comments

Comments
 (0)