Skip to content

Commit 842b3af

Browse files
committed
QLS: miscellaneous fixes for union generators
1 parent fbd217b commit 842b3af

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

test/Test/Database/LSMTree/StateMachine.hs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ import Data.List.NonEmpty (NonEmpty (..))
9292
import qualified Data.List.NonEmpty as NE
9393
import Data.Map.Strict (Map)
9494
import qualified Data.Map.Strict as Map
95-
import Data.Maybe (catMaybes, fromJust, fromMaybe)
95+
import Data.Maybe (catMaybes, fromJust, fromMaybe, isJust)
9696
import Data.Monoid (First (..))
9797
import Data.Primitive.MutVar
9898
import Data.Set (Set)
9999
import qualified Data.Set as Set
100-
import Data.Typeable (Proxy (..), Typeable, cast)
100+
import Data.Typeable (Proxy (..), Typeable, cast, eqT)
101101
import qualified Data.Vector as V
102102
import qualified Database.LSMTree as R
103103
import Database.LSMTree.Class (LookupResult (..), QueryResult (..))
@@ -1761,16 +1761,17 @@ arbitraryActionWithVars _ label ctx (ModelState st _stats) =
17611761
Map.member (Model.tableID t) (Model.tables st)
17621762
]
17631763

1764-
genUnionedTableVar = QC.elements (unionedTableVars Model.IsUnionDescendant)
1765-
genNonUnionedTableVar = QC.elements (unionedTableVars Model.IsNotUnionDescendant)
1764+
genUnionDescendantTableVars = QC.elements unionDescendantTableVars
1765+
genNotUnionDescendantTableVars = QC.elements notUnionDescendantTableVars
17661766

1767-
-- | Variables for tables that are a (descendant of a) union table, or not.
1768-
unionedTableVars :: Model.IsUnionDescendant -> [Var h (WrapTable h IO k v b)]
1769-
unionedTableVars target =
1770-
[ v
1767+
unionDescendantTableVars, notUnionDescendantTableVars :: [Var h (WrapTable h IO k v b)]
1768+
(unionDescendantTableVars, notUnionDescendantTableVars) = partitionEithers $
1769+
[ case Model.isUnionDescendant t of
1770+
Model.IsUnionDescendant -> Left v
1771+
Model.IsNotUnionDescendant -> Right v
17711772
| v <- tableVars
1772-
, case lookupVar ctx v of
1773-
MTable t -> Model.isUnionDescendant t == target
1773+
, let t = case lookupVar ctx v of
1774+
MTable t' -> t'
17741775
]
17751776

17761777
genCursorVar = QC.elements cursorVars
@@ -1896,47 +1897,82 @@ arbitraryActionWithVars _ label ctx (ModelState st _stats) =
18961897

18971898
-- | Generate table actions that have to do with unions.
18981899
genUnionActions :: [(Int, Gen (Any (LockstepAction (ModelState h))))]
1899-
genUnionActions =
1900+
genUnionActions
1901+
| null tableVars = []
1902+
| otherwise =
19001903
[ (2, fmap Some $ (Action <$> genErrors <*>) $
19011904
Union <$> genTableVar <*> genTableVar)
19021905
| length tableVars <= 5 -- no more than 5 tables at once
19031906
, let genErrors = pure Nothing -- TODO: generate errors
1904-
, False -- TODO: enable once table union is implemented
1907+
-- TODO: this is currently only enabled for the reference
1908+
-- implementation. Enable this unconditionally once table union is
1909+
-- implemented
1910+
, isJust (eqT @h @ModelIO.Table)
19051911
]
19061912
++ [ (2, fmap Some $ (Action <$> genErrors <*>) $
1907-
Unions <$> genUnionsTableVars)
1913+
Unions <$> gen2or3TableVars)
19081914
| length tableVars <= 5 -- no more than 5 tables at once
19091915
, let genErrors = pure Nothing -- TODO: generate errors
1910-
, False -- TODO: enable once table unions is implemented
1916+
-- TODO: this is currently only enabled for the reference
1917+
-- implementation. Enable this unconditionally once table union is
1918+
-- implemented
1919+
, isJust (eqT @h @ModelIO.Table)
19111920
]
19121921
++ [ (2, fmap Some $ (Action <$> genErrors <*>) $
1913-
RemainingUnionDebt <$> genTableVar')
1922+
RemainingUnionDebt <$> genUnionTableVar)
19141923
| let genErrors = pure Nothing -- TODO: generate errors
1915-
, False -- TODO: enable once table unions is implemented
1924+
-- TODO: this is currently only enabled for the reference
1925+
-- implementation. Enable this unconditionally once table union is
1926+
-- implemented
1927+
, isJust (eqT @h @ModelIO.Table)
1928+
19161929
]
19171930
++ [ (8, fmap Some $ (Action <$> genErrors <*>) $
1918-
SupplyUnionCredits <$> genTableVar' <*> genUnionCredits)
1931+
SupplyUnionCredits <$> genUnionTableVar <*> genUnionCredits)
19191932
| let genErrors = pure Nothing -- TODO: generate errors
1920-
, False -- TODO: enable once table unions is implemented
1933+
-- TODO: this is currently only enabled for the reference
1934+
-- implementation. Enable this unconditionally once table union is
1935+
-- implemented
1936+
, isJust (eqT @h @ModelIO.Table)
19211937
]
19221938
++ [ (2, fmap Some $ (Action <$> genErrors <*>) $
1923-
SupplyPortionOfDebt <$> genTableVar' <*> genPortion)
1939+
SupplyPortionOfDebt <$> genUnionTableVar <*> genPortion)
19241940
| let genErrors = pure Nothing -- TODO: generate errors
1925-
, False -- TODO: enable once table unions is implemented
1941+
-- TODO: this is currently only enabled for the reference
1942+
-- implementation. Enable this unconditionally once table union is
1943+
-- implemented
1944+
, isJust (eqT @h @ModelIO.Table)
19261945
]
19271946
where
1947+
-- Generate at least a 2-way union, and at most a 3-way union.
1948+
--
1949+
-- Unit tests for 0-way and 1-way unions are included in the UnitTests
1950+
-- module. n-way unions for n>3 lead to larger unions, which are less likely
1951+
-- to be finished before the end of an action sequence.
1952+
gen2or3TableVars :: Gen (NonEmpty (Var h (WrapTable h IO k v b)))
1953+
gen2or3TableVars = do
1954+
tableVar1 <- genTableVar
1955+
tableVar2 <- genTableVar
1956+
mtableVar3 <- QC.oneof [pure Nothing, Just <$> genTableVar]
1957+
pure $ NE.fromList $ catMaybes [
1958+
Just tableVar1, Just tableVar2, mtableVar3
1959+
]
1960+
19281961
-- TODO: tweak distribution once table unions are implemented
1929-
genTableVar' = QC.frequency [
1962+
genUnionTableVar = QC.frequency $
19301963
-- The interesting cases to test for are when tables are union
19311964
-- tables.
1932-
(9, genUnionedTableVar)
1965+
[ (9, genUnionDescendantTableVars)
1966+
| not (null unionDescendantTableVars)
1967+
]
19331968
-- For non-union tables, querying the union debt or supplying union
19341969
-- credits are no-ops, so we generate such tables only rarely.
19351970
--
19361971
-- TODO: replace union actions on non-union tables with a few unit
19371972
-- tests?
1938-
, (1, genNonUnionedTableVar)
1939-
]
1973+
++ [ (1, genNotUnionDescendantTableVars)
1974+
| not (null notUnionDescendantTableVars)
1975+
]
19401976

19411977
-- TODO: tweak distribution once table unions are implemented
19421978
genUnionCredits = QC.frequency [
@@ -2008,20 +2044,6 @@ arbitraryActionWithVars _ label ctx (ModelState st _stats) =
20082044
genBlob :: Gen (Maybe b)
20092045
genBlob = QC.arbitrary
20102046

2011-
-- Generate at least a 2-way union, and at most a 3-way union.
2012-
--
2013-
-- Unit tests for 0-way and 1-way unions are included in the UnitTests
2014-
-- module. n-way unions for n>3 lead to larger unions, which are less likely
2015-
-- to be finished before the end of an action sequence.
2016-
genUnionsTableVars :: Gen (NonEmpty (Var h (WrapTable h IO k v b)))
2017-
genUnionsTableVars = do
2018-
tableVar1 <- genTableVar
2019-
tableVar2 <- genTableVar
2020-
mtableVar3 <- QC.liftArbitrary genTableVar
2021-
pure $ NE.fromList $ catMaybes [
2022-
Just tableVar1, Just tableVar2, mtableVar3
2023-
]
2024-
20252047
shrinkActionWithVars ::
20262048
forall h a. (
20272049
Eq (Class.TableConfig h)

0 commit comments

Comments
 (0)