Skip to content

Commit 747f012

Browse files
authored
Merge pull request #579 from IntersectMBO/jeltsch/unbiased-raw-bytes-generation
Remove bias from generation of `RawBytes` values
2 parents 02e3bf6 + 3128a4b commit 747f012

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src-extras/Database/LSMTree/Extras/Generators.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import Control.DeepSeq (NFData)
4242
import Control.Exception (assert)
4343
import Data.Coerce (coerce)
4444
import Data.Containers.ListUtils (nubOrd)
45+
import Data.Function ((&))
4546
import Data.List (sort)
4647
import qualified Data.Primitive.ByteArray as BA
4748
import qualified Data.Vector.Primitive as VP
@@ -423,7 +424,12 @@ chunkSizeInvariant (ChunkSize csize) = chunkSizeLB <= csize && csize <= chunkSiz
423424
-------------------------------------------------------------------------------}
424425

425426
instance Arbitrary RawBytes where
426-
arbitrary = genRawBytes >>= genSlice
427+
arbitrary = do
428+
QC.NonNegative (QC.Small prefixLength) <- arbitrary
429+
QC.NonNegative (QC.Small payloadLength) <- arbitrary
430+
QC.NonNegative (QC.Small suffixLength) <- arbitrary
431+
base <- genRawBytesN (prefixLength + payloadLength + suffixLength)
432+
return (base & RB.drop prefixLength & RB.take payloadLength)
427433
shrink rb = shrinkSlice rb ++ shrinkRawBytes rb
428434

429435
genRawBytesN :: Int -> Gen RawBytes

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,22 @@ tests = testGroup "Test.Database.LSMTree.Internal.Serialise" [
5555
Eq and Ord laws
5656
-------------------------------------------------------------------------------}
5757

58+
withFirstKeyLengthInfo :: SerialisedKey -> Property -> Property
59+
withFirstKeyLengthInfo firstKey
60+
= collect ("Length of first key is " ++ show (sizeofKey firstKey))
61+
5862
propEqReflexivity :: SerialisedKey -> Property
5963
propEqReflexivity k = k === k
6064

6165
propEqSymmetry :: SerialisedKey -> SerialisedKey -> Property
6266
propEqSymmetry k1 k2 = (k1 == k2) === (k2 == k1)
6367

64-
propEqTransitivity :: SerialisedKey -> SerialisedKey -> SerialisedKey -> Property
65-
propEqTransitivity k1 k2 k3 = mapSize (const 5) $ k1 == k2 && k2 == k3 ==> k1 === k3
68+
propEqTransitivity :: Property
69+
propEqTransitivity = mapSize (const 3) $ withDiscardRatio 1000 $ untunedProp
70+
where
71+
untunedProp :: SerialisedKey -> SerialisedKey -> SerialisedKey -> Property
72+
untunedProp k1 k2 k3 = withFirstKeyLengthInfo k1 $
73+
k1 == k2 && k2 == k3 ==> k1 === k3
6674

6775
propEqNegation :: SerialisedKey -> SerialisedKey -> Property
6876
propEqNegation k1 k2 = (k1 /= k2) === not (k1 == k2)
@@ -76,5 +84,9 @@ propOrdTransitivity k1 k2 k3 = k1 <= k2 && k2 <= k3 ==> k1 <= k3
7684
propOrdReflexivity :: SerialisedKey -> Property
7785
propOrdReflexivity k = property $ k <= k
7886

79-
propOrdAntiSymmetry :: SerialisedKey -> SerialisedKey -> Property
80-
propOrdAntiSymmetry k1 k2 = mapSize (const 5) $ k1 <= k2 && k2 <= k1 ==> k1 === k2
87+
propOrdAntiSymmetry :: Property
88+
propOrdAntiSymmetry = mapSize (const 4) $ withDiscardRatio 100 $ untunedProp
89+
where
90+
untunedProp :: SerialisedKey -> SerialisedKey -> Property
91+
untunedProp k1 k2 = withFirstKeyLengthInfo k1 $
92+
k1 <= k2 && k2 <= k1 ==> k1 === k2

0 commit comments

Comments
 (0)