Skip to content
Snippets Groups Projects
Commit df8578fb authored by Iustin Pop's avatar Iustin Pop
Browse files

Generalise the test helper 'genUniquesList'


Instead of always using "arbitrary", make it require a generator (that
can then use more complex rules for building the list elements), and
also convert it to use a temporary set instead of list membership.

Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent 1db32048
No related branches found
No related tags found
No related merge requests found
...@@ -63,7 +63,7 @@ prop_findByName = ...@@ -63,7 +63,7 @@ prop_findByName =
forAll (genNode (Just 1) Nothing) $ \node -> forAll (genNode (Just 1) Nothing) $ \node ->
forAll (choose (1, 20)) $ \ cnt -> forAll (choose (1, 20)) $ \ cnt ->
forAll (choose (0, cnt - 1)) $ \ fidx -> forAll (choose (0, cnt - 1)) $ \ fidx ->
forAll (genUniquesList (cnt * 2)) $ \ allnames -> forAll (genUniquesList (cnt * 2) arbitrary) $ \ allnames ->
forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername -> forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername ->
let names = zip (take cnt allnames) (drop cnt allnames) let names = zip (take cnt allnames) (drop cnt allnames)
nl = makeSmallCluster node cnt nl = makeSmallCluster node cnt
......
...@@ -106,7 +106,7 @@ instance Arbitrary Types.IPolicy where ...@@ -106,7 +106,7 @@ instance Arbitrary Types.IPolicy where
istd <- genBiggerISpec imin istd <- genBiggerISpec imin
imax <- genBiggerISpec istd imax <- genBiggerISpec istd
num_tmpl <- choose (0, length allDiskTemplates) num_tmpl <- choose (0, length allDiskTemplates)
dts <- genUniquesList num_tmpl dts <- genUniquesList num_tmpl arbitrary
vcpu_ratio <- choose (1.0, maxVcpuRatio) vcpu_ratio <- choose (1.0, maxVcpuRatio)
spindle_ratio <- choose (1.0, maxSpindleRatio) spindle_ratio <- choose (1.0, maxSpindleRatio)
return Types.IPolicy { Types.iPolicyMinSpec = imin return Types.IPolicy { Types.iPolicyMinSpec = imin
......
...@@ -360,7 +360,7 @@ genEmptyContainer = pure . GenericContainer $ Map.fromList [] ...@@ -360,7 +360,7 @@ genEmptyContainer = pure . GenericContainer $ Map.fromList []
genDiskIndices :: Gen [DiskIndex] genDiskIndices :: Gen [DiskIndex]
genDiskIndices = do genDiskIndices = do
cnt <- choose (0, C.maxDisks) cnt <- choose (0, C.maxDisks)
genUniquesList cnt genUniquesList cnt arbitrary
-- | Generates a list of node names. -- | Generates a list of node names.
genNodeNames :: Gen [String] genNodeNames :: Gen [String]
......
...@@ -187,11 +187,12 @@ genFields = do ...@@ -187,11 +187,12 @@ genFields = do
vectorOf n genName vectorOf n genName
-- | Generates a list of a given size with non-duplicate elements. -- | Generates a list of a given size with non-duplicate elements.
genUniquesList :: (Eq a, Arbitrary a) => Int -> Gen [a] genUniquesList :: (Eq a, Arbitrary a, Ord a) => Int -> Gen a -> Gen [a]
genUniquesList cnt = genUniquesList cnt generator = do
foldM (\lst _ -> do set <- foldM (\set _ -> do
newelem <- arbitrary `suchThat` (`notElem` lst) newelem <- generator `suchThat` (`Set.notMember` set)
return (newelem:lst)) [] [1..cnt] return (Set.insert newelem set)) Set.empty [1..cnt]
return $ Set.toList set
newtype SmallRatio = SmallRatio Double deriving Show newtype SmallRatio = SmallRatio Double deriving Show
instance Arbitrary SmallRatio where instance Arbitrary SmallRatio where
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment